예제 #1
0
    @Override
    public void run() {
      try {
        long idleTime = (System.currentTimeMillis() - socket.getLastAccess()) / 1000;
        if (idleTime >= timeout) {
          if (!socket.getSocket().isClosed()) {
            logger.debug(
                "Connection {}:{} has been idle for {} seconds",
                socket.getSocket().getInetAddress(),
                socket.getSocket().getPort(),
                idleTime);
            logger.debug(
                "Closing connection {}:{}",
                socket.getSocket().getInetAddress(),
                socket.getSocket().getPort());
            socket.close();
          }
        }

      } catch (IOException e) {
        logger.error(
            "Error closing connection {}:{}",
            socket.getSocket().getInetAddress(),
            socket.getSocket().getPort());
      }
    }
예제 #2
0
  @Override
  public boolean postProcess(HttpContext ctx) {
    ReusableSocket socket = ctx.getSocket();
    HttpRequest request = ctx.getRequest();
    HttpResponse response = ctx.getResponse();

    if (applicable(ctx)) {
      logger.debug("Checking keep-alive behaviour");
      String connection = request.getHeader(Constants.HEADERS.CONNECTION);
      boolean keepAlive = connection != null && connection.equalsIgnoreCase("Keep-Alive");

      ServerConfig serverConfig = ServerConfig.getInstance();
      Version version = Version.getVersion(serverConfig.httpVersion());

      // keep alive behaviour is protocol version specific
      switch (version) {
        case VERSION_0_9:
          break;
        case VERSION_1_0:
          if (keepAlive) {
            logger.debug("{} Keeping connection alive", version);
            socket.setPersist(true);
          }
          break;
        case VERSION_1_1:
          if (keepAlive) {
            if (max == -1) { // if max is not set then always persist
              socket.setPersist(true);
            } else if (socket.getCount()
                >= max) { // if max is set, persist only if max is not reached
              logger.debug(
                  "{} Not keeping connection alive because max={} is reached", version, max);
              socket.setPersist(false);
            } else {
              socket.setPersist(true);
            }

            if (socket.isPersist()) {
              if (timeout
                  == -1) { // if timeout is not set then always persist till server keepAliveTimeout
                // occurs
                int to = serverConfig.keepAliveTimeout();
                logger.debug(
                    "{} Keeping connection alive. Will timeout after {} seconds", version, to);
                socket.setPersist(true);
                try {
                  socket.getSocket().setSoTimeout(to);
                } catch (SocketException e) {
                }
                worker.schedule(new KeepAliveTimeoutCleaner(socket, to), to, TimeUnit.SECONDS);
              } else { // if timeout is set, persist only if timeout is not reached
                logger.debug(
                    "{} Keeping connection alive. Will timeout after {} seconds", version, timeout);
                socket.setPersist(true);
                try {
                  socket.getSocket().setSoTimeout(timeout);
                } catch (SocketException e) {
                }
                worker.schedule(
                    new KeepAliveTimeoutCleaner(socket, timeout), timeout, TimeUnit.SECONDS);
              }
            }
          }
          break;
        default:
          break;
      }
    }

    if (socket.isPersist()) {
      logger.debug("Requesting client to keep connection alive");
      response.addHeader(Constants.HEADERS.CONNECTION, "Keep-Alive");
    } else {
      logger.debug("Requesting client to close the connection");
      response.addHeader(Constants.HEADERS.CONNECTION, "Close");
    }

    return false;
  }