# HG changeset patch # User Maxim Dounin # Date 1224774675 -14400 # Node ID 42e64900483bb252e1a6d97588be11cf6215a819 # Parent 8545bbda9e4b872eb1387c87f51b5c2d683972c4 Keepalive: always save connections to cache. Free last recently used one if there is not enough room. diff --git a/ngx_http_upstream_keepalive_module.c b/ngx_http_upstream_keepalive_module.c --- a/ngx_http_upstream_keepalive_module.c +++ b/ngx_http_upstream_keepalive_module.c @@ -224,17 +224,29 @@ ngx_http_upstream_free_keepalive_peer(ng "free keepalive peer"); if (!(state & NGX_PEER_FAILED) - && pc->connection != NULL - && !ngx_queue_empty(&kp->conf->free)) + && pc->connection != NULL) { c = pc->connection; - q = ngx_queue_head(&kp->conf->free); - ngx_queue_remove(q); + if (ngx_queue_empty(&kp->conf->free)) { + + q = ngx_queue_last(&kp->conf->cache); + ngx_queue_remove(q); + + item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, + queue); + + ngx_close_connection(item->connection); - item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, queue); + } else { + q = ngx_queue_head(&kp->conf->free); + ngx_queue_remove(q); + + item = ngx_queue_data(q, ngx_http_upstream_keepalive_cache_t, + queue); + } + item->connection = c; - ngx_queue_insert_head(&kp->conf->cache, q); pc->connection = NULL;