Take the latency into account when evaluating whether or not to reuse a connection.
authorFabian Keil <fk@fabiankeil.de>
Mon, 13 Jul 2009 17:12:28 +0000 (17:12 +0000)
committerFabian Keil <fk@fabiankeil.de>
Mon, 13 Jul 2009 17:12:28 +0000 (17:12 +0000)
gateway.c
jcc.c
project.h

index 855ebf2..301dd1a 100644 (file)
--- a/gateway.c
+++ b/gateway.c
@@ -1,4 +1,4 @@
-const char gateway_rcs[] = "$Id: gateway.c,v 1.55 2009/07/05 12:01:45 fabiankeil Exp $";
+const char gateway_rcs[] = "$Id: gateway.c,v 1.56 2009/07/11 14:49:09 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/gateway.c,v $
@@ -228,6 +228,8 @@ void remember_connection(const struct client_state *csp, const struct forward_sp
    reusable_connection[slot].port = http->port;
    reusable_connection[slot].in_use = 0;
    reusable_connection[slot].timestamp = connection->timestamp;
+   reusable_connection->request_sent = connection->request_sent;
+   reusable_connection->response_received = connection->response_received;
    reusable_connection[slot].keep_alive_timeout = connection->keep_alive_timeout;
 
    assert(NULL != fwd);
@@ -289,6 +291,8 @@ void mark_connection_closed(struct reusable_connection *closed_connection)
    freez(closed_connection->host);
    closed_connection->port = 0;
    closed_connection->timestamp = 0;
+   closed_connection->request_sent = 0;
+   closed_connection->response_received = 0;
    closed_connection->keep_alive_timeout = 0;
    closed_connection->forwarder_type = SOCKS_NONE;
    freez(closed_connection->gateway_host);
@@ -419,16 +423,19 @@ int close_unusable_connections(void)
          && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
       {
          time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
+         time_t latency = reusable_connection[slot].response_received -
+            reusable_connection[slot].request_sent;
 
-         if (reusable_connection[slot].keep_alive_timeout < time_open)
+         if (reusable_connection[slot].keep_alive_timeout < time_open + latency)
          {
             log_error(LOG_LEVEL_CONNECT,
                "The connection to %s:%d in slot %d timed out. "
-               "Closing socket %d. Timeout is: %d.",
+               "Closing socket %d. Timeout is: %d. Assumed latency: %d",
                reusable_connection[slot].host,
                reusable_connection[slot].port, slot,
                reusable_connection[slot].sfd,
-               reusable_connection[slot].keep_alive_timeout);
+               reusable_connection[slot].keep_alive_timeout,
+               latency);
             close_socket(reusable_connection[slot].sfd);
             mark_connection_closed(&reusable_connection[slot]);
          }
diff --git a/jcc.c b/jcc.c
index eb2e7ab..a9da886 100644 (file)
--- a/jcc.c
+++ b/jcc.c
@@ -1,4 +1,4 @@
-const char jcc_rcs[] = "$Id: jcc.c,v 1.268 2009/07/13 17:05:36 fabiankeil Exp $";
+const char jcc_rcs[] = "$Id: jcc.c,v 1.269 2009/07/13 17:08:41 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
@@ -1750,6 +1750,8 @@ static void chat(struct client_state *csp)
 
    log_error(LOG_LEVEL_CONNECT, "to %s successful", http->hostport);
 
+   csp->server_connection.request_sent = time(NULL);
+
    /* we're finished with the client's header */
    freez(hdr);
 
@@ -2205,6 +2207,8 @@ static void chat(struct client_state *csp)
                log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
             }
 
+            csp->server_connection.response_received = time(NULL);
+
             if (crunch_response_triggered(csp, crunchers_light))
             {
                /*
@@ -2340,6 +2344,8 @@ static void serve(struct client_state *csp)
 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
    static int monitor_thread_running = 0;
    int continue_chatting = 0;
+   unsigned int latency = 0;
+
    do
    {
       chat(csp);
@@ -2350,17 +2356,19 @@ static void serve(struct client_state *csp)
          && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
          && (csp->cfd != JB_INVALID_SOCKET)
          && (csp->sfd != JB_INVALID_SOCKET)
-         && socket_is_still_usable(csp->sfd);
+         && socket_is_still_usable(csp->sfd)
+         && (latency < csp->server_connection.keep_alive_timeout);
 
       if (continue_chatting)
       {
+         unsigned int client_timeout = (unsigned)csp->server_connection.keep_alive_timeout - latency;
          log_error(LOG_LEVEL_CONNECT,
             "Waiting for the next client request. "
             "Keeping the server socket %d to %s open.",
             csp->sfd, csp->server_connection.host);
 
          if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
-            && data_is_available(csp->cfd, (int)csp->server_connection.keep_alive_timeout)
+            && data_is_available(csp->cfd, (int)client_timeout)
             && socket_is_still_usable(csp->cfd))
          {
             log_error(LOG_LEVEL_CONNECT, "Client request arrived in "
index 0353de7..885acfe 100644 (file)
--- a/project.h
+++ b/project.h
@@ -1,7 +1,7 @@
 #ifndef PROJECT_H_INCLUDED
 #define PROJECT_H_INCLUDED
 /** Version string. */
-#define PROJECT_H_VERSION "$Id: project.h,v 1.142 2009/06/11 11:49:11 fabiankeil Exp $"
+#define PROJECT_H_VERSION "$Id: project.h,v 1.143 2009/06/28 14:31:43 fabiankeil Exp $"
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/project.h,v $
@@ -639,7 +639,11 @@ struct reusable_connection
 {
    jb_socket sfd;
    int       in_use;
-   time_t    timestamp;
+   time_t    timestamp; /* XXX: rename? */
+
+   time_t    request_sent;
+   time_t    response_received;
+
    /*
     * Number of seconds after which this
     * connection will no longer be reused.