11diff --git src/ngx_http_lua_balancer.c src/ngx_http_lua_balancer.c
2- index ae0f1380..acabab11 100644
2+ index ae0f1380..cc407775 100644
33--- src/ngx_http_lua_balancer.c
44+++ src/ngx_http_lua_balancer.c
55@@ -31,10 +31,39 @@ typedef struct {
@@ -42,7 +42,7 @@ index ae0f1380..acabab11 100644
4242 void *data;
4343
4444 ngx_event_get_peer_pt original_get_peer;
45- @@ -45,20 +74,23 @@ struct ngx_http_lua_balancer_peer_data_s {
45+ @@ -45,20 +74,21 @@ struct ngx_http_lua_balancer_peer_data_s {
4646 ngx_event_save_peer_session_pt original_save_session;
4747 #endif
4848
@@ -54,13 +54,11 @@ index ae0f1380..acabab11 100644
5454
5555- ngx_uint_t more_tries;
5656- ngx_uint_t total_tries;
57- + ngx_event_get_peer_pt original_get_peer;
58- + ngx_event_free_peer_pt original_free_peer;
59- +
60- + ngx_str_t *host;
6157
6258- struct sockaddr *sockaddr;
6359- socklen_t socklen;
60+ + ngx_str_t *host;
61+ +
6462 ngx_addr_t *local;
6563
6664- ngx_str_t host;
@@ -74,15 +72,15 @@ index ae0f1380..acabab11 100644
7472
7573 #if !(HAVE_NGX_UPSTREAM_TIMEOUT_FIELDS)
7674 unsigned cloned_upstream_conf:1;
77- @@ -77,6 +109 ,7 @@ static ngx_int_t
75+ @@ -77,6 +107 ,7 @@ static ngx_int_t
7876 ngx_http_lua_upstream_get_ssl_name(ngx_http_request_t *r,
7977 ngx_http_upstream_t *u);
8078 #endif
8179+
8280 static ngx_int_t ngx_http_lua_balancer_init(ngx_conf_t *cf,
8381 ngx_http_upstream_srv_conf_t *us);
8482 static ngx_int_t ngx_http_lua_balancer_init_peer(ngx_http_request_t *r,
85- @@ -87,6 +120 ,13 @@ static ngx_int_t ngx_http_lua_balancer_by_chunk(lua_State *L,
83+ @@ -87,6 +118 ,13 @@ static ngx_int_t ngx_http_lua_balancer_by_chunk(lua_State *L,
8684 ngx_http_request_t *r);
8785 static void ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc,
8886 void *data, ngx_uint_t state);
@@ -96,7 +94,7 @@ index ae0f1380..acabab11 100644
9694 static void ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc,
9795 void *data, ngx_uint_t type);
9896 static void ngx_http_lua_balancer_close(ngx_connection_t *c);
99- @@ -98,6 +138 ,14 @@ static ngx_uint_t ngx_http_lua_balancer_calc_hash(ngx_str_t *name,
97+ @@ -98,6 +136 ,14 @@ static ngx_uint_t ngx_http_lua_balancer_calc_hash(ngx_str_t *name,
10098 struct sockaddr *sockaddr, socklen_t socklen, ngx_addr_t *local);
10199
102100
@@ -111,7 +109,7 @@ index ae0f1380..acabab11 100644
111109 static struct sockaddr *ngx_http_lua_balancer_default_server_sockaddr;
112110
113111
114- @@ -181,7 +229 ,7 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
112+ @@ -181,7 +227 ,7 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
115113
116114 dd("enter");
117115
@@ -120,15 +118,15 @@ index ae0f1380..acabab11 100644
120118 if (cmd->post == NULL) {
121119 return NGX_CONF_ERROR;
122120 }
123- @@ -246,6 +294 ,7 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
121+ @@ -246,6 +292 ,7 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
124122 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
125123 ngx_memzero(&url, sizeof(ngx_url_t));
126124
127125+ /* just an invalid address as a place holder*/
128126 ngx_str_set(&url.url, "0.0.0.1");
129127 url.default_port = 80;
130128
131- @@ -261,8 +310 ,6 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
129+ @@ -261,8 +308 ,6 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
132130 }
133131
134132 if (uscf->peer.init_upstream) {
@@ -137,23 +135,32 @@ index ae0f1380..acabab11 100644
137135
138136 lscf->balancer.original_init_upstream = uscf->peer.init_upstream;
139137
140- @@ -387,6 +434,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
138+ @@ -305,7 +350,7 @@ ngx_http_lua_balancer_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
139+ us->peer.init = ngx_http_lua_balancer_init_peer;
140+
141+ /* allocate cache items and add to free queue */
142+ -
143+ +
144+ cached = ngx_pcalloc(cf->pool,
145+ sizeof(ngx_http_lua_balancer_ka_item_t)
146+ * lscf->balancer.max_cached);
147+ @@ -387,6 +432,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
141148 void *pdata;
142149 lua_State *L;
143150 ngx_int_t rc;
144151+ ngx_queue_t *q;
145152 ngx_connection_t *c;
146153 ngx_http_request_t *r;
147154 #if (NGX_HTTP_SSL)
148- @@ -394,6 +442 ,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
155+ @@ -394,6 +440 ,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
149156 #endif
150157 ngx_http_lua_ctx_t *ctx;
151158 ngx_http_lua_srv_conf_t *lscf;
152159+ ngx_http_lua_balancer_keepalive_item_t *item;
153160 ngx_http_lua_balancer_peer_data_t *bp = data;
154161
155162 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
156- @@ -425,9 +474 ,12 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
163+ @@ -425,9 +472 ,12 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
157164
158165 ctx->context = NGX_HTTP_LUA_CONTEXT_BALANCER;
159166
@@ -166,7 +173,7 @@ index ae0f1380..acabab11 100644
166173 bp->keepalive_requests = 0;
167174 bp->keepalive_timeout = 0;
168175 bp->keepalive = 0;
169- @@ -465,10 +517 ,10 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
176+ @@ -465,10 +515 ,10 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
170177 pc->local = bp->local;
171178 }
172179
@@ -179,7 +186,7 @@ index ae0f1380..acabab11 100644
179186 pc->cached = 0;
180187 pc->connection = NULL;
181188
182- @@ -476,27 +528 ,59 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
189+ @@ -476,27 +526 ,59 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
183190 r->upstream->peer.tries += bp->more_tries;
184191 }
185192
@@ -252,15 +259,18 @@ index ae0f1380..acabab11 100644
252259 }
253260
254261 return NGX_OK;
255- @@ -584,6 +668,7 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
262+ @@ -582,8 +664,9 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
263+ ngx_queue_t *q;
264+ ngx_connection_t *c;
256265 ngx_http_upstream_t *u;
257- ngx_http_lua_balancer_ka_item_t *item;
266+ - ngx_http_lua_balancer_ka_item_t *item;
267+ + ngx_http_lua_balancer_keepalive_item_t *item;
258268 ngx_http_lua_balancer_peer_data_t *bp = data;
259269+ ngx_http_lua_balancer_keepalive_pool_t *cpool;
260270 ngx_http_lua_srv_conf_t *lscf = bp->conf;
261271
262272 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
263- @@ -592,14 +677 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
273+ @@ -592,14 +675 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
264274 u = bp->request->upstream;
265275 c = pc->connection;
266276
@@ -279,7 +289,7 @@ index ae0f1380..acabab11 100644
279289 if (state & NGX_PEER_FAILED
280290 || c == NULL
281291 || c->read->eof
282- @@ -633,42 +720 ,41 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
292+ @@ -633,42 +718 ,41 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
283293 goto invalid;
284294 }
285295
@@ -341,7 +351,7 @@ index ae0f1380..acabab11 100644
341351
342352 if (c->write->timer_set) {
343353 ngx_del_timer(c->write);
344- @@ -684,42 +770 ,6 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
354+ @@ -684,42 +768 ,6 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
345355 c->write->log = ngx_cycle->log;
346356 c->pool->log = ngx_cycle->log;
347357
@@ -384,7 +394,7 @@ index ae0f1380..acabab11 100644
384394 if (c->read->ready) {
385395 ngx_http_lua_balancer_close_handler(c->read);
386396 }
387- @@ -728,9 +778 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
397+ @@ -728,9 +776 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
388398
389399 invalid:
390400
@@ -404,7 +414,7 @@ index ae0f1380..acabab11 100644
404414 }
405415
406416 return;
407- @@ -740,6 +797 ,123 @@ invalid:
417+ @@ -740,6 +795 ,123 @@ invalid:
408418 }
409419
410420
@@ -528,7 +538,7 @@ index ae0f1380..acabab11 100644
528538 static void
529539 ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
530540 ngx_uint_t type)
531- @@ -755,6 +929 ,10 @@ ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
541+ @@ -755,6 +927 ,10 @@ ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
532542 static void
533543 ngx_http_lua_balancer_close(ngx_connection_t *c)
534544 {
@@ -539,7 +549,7 @@ index ae0f1380..acabab11 100644
539549 #if (NGX_HTTP_SSL)
540550 if (c->ssl) {
541551 c->ssl->no_wait_shutdown = 1;
542- @@ -762,9 +940 ,6 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
552+ @@ -762,9 +938 ,6 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
543553
544554 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
545555 c->ssl->handler = ngx_http_lua_balancer_close;
@@ -549,7 +559,7 @@ index ae0f1380..acabab11 100644
549559 return;
550560 }
551561 }
552- @@ -773,8 +948 ,12 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
562+ @@ -773,8 +946 ,12 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
553563 ngx_destroy_pool(c->pool);
554564 ngx_close_connection(c);
555565
@@ -564,19 +574,20 @@ index ae0f1380..acabab11 100644
564574 }
565575
566576
567- @@ -820,8 +999,9 @@ close:
577+ @@ -820,8 +997,10 @@ close:
568578 ngx_http_lua_balancer_close(c);
569579
570580 ngx_queue_remove(&item->queue);
571581- ngx_queue_remove(&item->hnode);
572582- ngx_queue_insert_head(&item->lscf->balancer.free, &item->queue);
583+ + ngx_queue_insert_head(&item->cpool->free, &item->queue);
573584+ if (item->cpool->connections == 0) {
574585+ ngx_http_lua_balancer_free_keepalive_pool(ev->log, item->cpool);
575586+ }
576587 }
577588
578589
579- @@ -832,7 +1012 ,7 @@ ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc, void *data)
590+ @@ -832,7 +1011 ,7 @@ ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc, void *data)
580591 {
581592 ngx_http_lua_balancer_peer_data_t *bp = data;
582593
@@ -585,7 +596,7 @@ index ae0f1380..acabab11 100644
585596 /* TODO */
586597 return NGX_OK;
587598 }
588- @@ -846,7 +1026 ,7 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
599+ @@ -846,7 +1025 ,7 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
589600 {
590601 ngx_http_lua_balancer_peer_data_t *bp = data;
591602
@@ -594,7 +605,7 @@ index ae0f1380..acabab11 100644
594605 /* TODO */
595606 return;
596607 }
597- @@ -860,8 +1040 ,8 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
608+ @@ -860,8 +1039 ,8 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
598609 int
599610 ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
600611 const u_char *addr, size_t addr_len, int port,
@@ -605,7 +616,7 @@ index ae0f1380..acabab11 100644
605616 {
606617 ngx_url_t url;
607618 ngx_http_lua_ctx_t *ctx;
608- @@ -927,25 +1107 ,8 @@ ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
619+ @@ -927,25 +1106 ,8 @@ ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
609620 return NGX_ERROR;
610621 }
611622
@@ -633,7 +644,7 @@ index ae0f1380..acabab11 100644
633644
634645 return NGX_OK;
635646 }
636- @@ -1050,11 +1213 ,15 @@ ngx_http_lua_ffi_balancer_enable_keepalive(ngx_http_request_t *r,
647+ @@ -1050,11 +1212 ,15 @@ ngx_http_lua_ffi_balancer_enable_keepalive(ngx_http_request_t *r,
637648
638649 bp = (ngx_http_lua_balancer_peer_data_t *) u->peer.data;
639650
0 commit comments