From a0d0527d51dbb1cb9af76e999a43dbf989dd1245 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Wed, 24 Dec 2008 17:06:19 +0000 Subject: [PATCH] Keep a thread around to timeout alive connections even if no new requests are coming in. --- gateway.c | 31 ++++++++++++++++++++++--------- gateway.h | 6 +++++- jcc.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 11 deletions(-) diff --git a/gateway.c b/gateway.c index 5570f1fe..73c8a3a9 100644 --- a/gateway.c +++ b/gateway.c @@ -1,4 +1,4 @@ -const char gateway_rcs[] = "$Id: gateway.c,v 1.45 2008/12/04 18:17:07 fabiankeil Exp $"; +const char gateway_rcs[] = "$Id: gateway.c,v 1.46 2008/12/13 11:07:23 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/gateway.c,v $ @@ -34,6 +34,10 @@ const char gateway_rcs[] = "$Id: gateway.c,v 1.45 2008/12/04 18:17:07 fabiankeil * * Revisions : * $Log: gateway.c,v $ + * Revision 1.46 2008/12/13 11:07:23 fabiankeil + * Remove duplicated debugging checks + * in connection_destination_matches(). + * * Revision 1.45 2008/12/04 18:17:07 fabiankeil * Fix some cparser warnings. * @@ -619,12 +623,15 @@ static int connection_destination_matches(const struct reusable_connection *conn * * Parameters : none * - * Returns : void + * Returns : Number of connections that are still alive. * *********************************************************************/ -static void close_unusable_connections(void) +int close_unusable_connections(void) { unsigned int slot = 0; + int connections_alive = 0; + + privoxy_mutex_lock(&connection_reuse_mutex); for (slot = 0; slot < SZ(reusable_connection); slot++) { @@ -643,10 +650,8 @@ static void close_unusable_connections(void) reusable_connection[slot].sfd, keep_alive_timeout); close_socket(reusable_connection[slot].sfd); mark_connection_closed(&reusable_connection[slot]); - continue; } - - if (!socket_is_still_usable(reusable_connection[slot].sfd)) + else if (!socket_is_still_usable(reusable_connection[slot].sfd)) { log_error(LOG_LEVEL_CONNECT, "The connection to %s:%d in slot %d is no longer usable. " @@ -655,10 +660,18 @@ static void close_unusable_connections(void) reusable_connection[slot].sfd); close_socket(reusable_connection[slot].sfd); mark_connection_closed(&reusable_connection[slot]); - continue; + } + else + { + connections_alive++; } } } + + privoxy_mutex_unlock(&connection_reuse_mutex); + + return connections_alive; + } @@ -743,10 +756,10 @@ static jb_socket get_reusable_connection(const struct http_request *http, jb_socket sfd = JB_INVALID_SOCKET; unsigned int slot = 0; - privoxy_mutex_lock(&connection_reuse_mutex); - close_unusable_connections(); + privoxy_mutex_lock(&connection_reuse_mutex); + for (slot = 0; slot < SZ(reusable_connection); slot++) { if (!reusable_connection[slot].in_use diff --git a/gateway.h b/gateway.h index 4b054ca7..73a60bcd 100644 --- a/gateway.h +++ b/gateway.h @@ -1,6 +1,6 @@ #ifndef GATEWAY_H_INCLUDED #define GATEWAY_H_INCLUDED -#define GATEWAY_H_VERSION "$Id: gateway.h,v 1.10 2008/10/09 18:21:41 fabiankeil Exp $" +#define GATEWAY_H_VERSION "$Id: gateway.h,v 1.11 2008/11/13 09:08:42 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/gateway.h,v $ @@ -36,6 +36,9 @@ * * Revisions : * $Log: gateway.h,v $ + * Revision 1.11 2008/11/13 09:08:42 fabiankeil + * Add new config option: keep-alive-timeout. + * * Revision 1.10 2008/10/09 18:21:41 fabiankeil * Flush work-in-progress changes to keep outgoing connections * alive where possible. Incomplete and mostly #ifdef'd out. @@ -121,6 +124,7 @@ extern void forget_connection(jb_socket sfd); extern void remember_connection(jb_socket sfd, const struct http_request *http, const struct forward_spec *fwd); +extern int close_unusable_connections(void); #endif /* FEATURE_CONNECTION_KEEP_ALIVE */ diff --git a/jcc.c b/jcc.c index 59086d55..f9d39e6a 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.213 2008/12/15 18:45:51 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.214 2008/12/20 14:53:55 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -33,6 +33,11 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.213 2008/12/15 18:45:51 fabiankeil Exp $" * * Revisions : * $Log: jcc.c,v $ + * Revision 1.214 2008/12/20 14:53:55 fabiankeil + * Add config option socket-timeout to control the time + * Privoxy waits for data to arrive on a socket. Useful + * in case of stale ssh tunnels or when fuzz-testing. + * * Revision 1.213 2008/12/15 18:45:51 fabiankeil * When logging crunches, log the whole URL, so one can easily * differentiate between vanilla HTTP and CONNECT requests. @@ -3207,6 +3212,34 @@ static void chat(struct client_state *csp) } +/********************************************************************* + * + * Function : wait_for_alive_connections + * + * Description : Waits for alive connections to timeout. + * + * Parameters : N/A + * + * Returns : N/A + * + *********************************************************************/ +static void wait_for_alive_connections() +{ + int connections_alive = close_unusable_connections(); + + while (0 < connections_alive) + { + log_error(LOG_LEVEL_CONNECT, + "Waiting for %d connections to timeout.", + connections_alive); + sleep(60); + connections_alive = close_unusable_connections(); + } + + log_error(LOG_LEVEL_CONNECT, "No connections to wait for left."); + +} + /********************************************************************* * * Function : serve @@ -3232,10 +3265,22 @@ static void serve(struct client_state *csp) if (csp->sfd != JB_INVALID_SOCKET) { #ifdef FEATURE_CONNECTION_KEEP_ALIVE + static int monitor_thread_running = 0; + if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE) && (csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE)) { remember_connection(csp->sfd, csp->http, forward_url(csp, csp->http)); + privoxy_mutex_lock(&connection_reuse_mutex); + if (!monitor_thread_running) + { + monitor_thread_running = 1; + privoxy_mutex_unlock(&connection_reuse_mutex); + wait_for_alive_connections(); + privoxy_mutex_lock(&connection_reuse_mutex); + monitor_thread_running = 0; + } + privoxy_mutex_unlock(&connection_reuse_mutex); } else { -- 2.39.2