11diff --git src/ngx_http_lua_balancer.c src/ngx_http_lua_balancer.c
2- index ae0f1380..3b4587f8 100644
2+ index ae0f1380..1d3fcb74 100644
33--- src/ngx_http_lua_balancer.c
44+++ src/ngx_http_lua_balancer.c
55@@ -31,10 +31,39 @@ typedef struct {
@@ -143,32 +143,75 @@ index ae0f1380..3b4587f8 100644
143143
144144 lscf->balancer.original_init_upstream = uscf->peer.init_upstream;
145145
146- @@ -305,7 +343,7 @@ ngx_http_lua_balancer_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
146+ @@ -290,12 +328,9 @@ ngx_http_lua_balancer_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
147+ ngx_uint_t bucket_cnt;
148+ ngx_queue_t *buckets;
149+ ngx_http_lua_srv_conf_t *lscf;
150+ - ngx_http_lua_balancer_ka_item_t *cached;
151+
152+ lscf = ngx_http_conf_upstream_srv_conf(us, ngx_http_lua_module);
153+
154+ - ngx_conf_init_uint_value(lscf->balancer.max_cached, 32);
155+ -
156+ if (lscf->balancer.original_init_upstream(cf, us) != NGX_OK) {
157+ return NGX_ERROR;
158+ }
159+ @@ -304,38 +339,6 @@ ngx_http_lua_balancer_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
160+
147161 us->peer.init = ngx_http_lua_balancer_init_peer;
148162
149- /* allocate cache items and add to free queue */
163+ - /* allocate cache items and add to free queue */
164+ -
165+ - cached = ngx_pcalloc(cf->pool,
166+ - sizeof(ngx_http_lua_balancer_ka_item_t)
167+ - * lscf->balancer.max_cached);
168+ - if (cached == NULL) {
169+ - return NGX_ERROR;
170+ - }
171+ -
172+ - ngx_queue_init(&lscf->balancer.cache);
173+ - ngx_queue_init(&lscf->balancer.free);
174+ -
175+ - for (i = 0; i < lscf->balancer.max_cached; i++) {
176+ - ngx_queue_insert_head(&lscf->balancer.free, &cached[i].queue);
177+ - cached[i].lscf = lscf;
178+ - }
179+ -
180+ - bucket_cnt = lscf->balancer.max_cached / 2;
181+ - bucket_cnt = bucket_cnt > 0 ? bucket_cnt : 1;
182+ - buckets = ngx_pcalloc(cf->pool, sizeof(ngx_queue_t) * bucket_cnt);
183+ -
184+ - if (buckets == NULL) {
185+ - return NGX_ERROR;
186+ - }
187+ -
188+ - for (i = 0; i < bucket_cnt; i++) {
189+ - ngx_queue_init(&buckets[i]);
190+ - }
191+ -
192+ - lscf->balancer.buckets = buckets;
193+ - lscf->balancer.bucket_cnt = bucket_cnt;
150194-
151- +
152- cached = ngx_pcalloc(cf->pool,
153- sizeof(ngx_http_lua_balancer_ka_item_t)
154- * lscf->balancer.max_cached);
155- @@ -387,6 +425,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
195+ return NGX_OK;
196+ }
197+
198+ @@ -387,6 +390,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
156199 void *pdata;
157200 lua_State *L;
158201 ngx_int_t rc;
159202+ ngx_queue_t *q;
160203 ngx_connection_t *c;
161204 ngx_http_request_t *r;
162205 #if (NGX_HTTP_SSL)
163- @@ -394,6 +433 ,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
206+ @@ -394,6 +398 ,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
164207 #endif
165208 ngx_http_lua_ctx_t *ctx;
166209 ngx_http_lua_srv_conf_t *lscf;
167210+ ngx_http_lua_balancer_keepalive_item_t *item;
168211 ngx_http_lua_balancer_peer_data_t *bp = data;
169212
170213 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
171- @@ -425,9 +465 ,12 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
214+ @@ -425,9 +430 ,12 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
172215
173216 ctx->context = NGX_HTTP_LUA_CONTEXT_BALANCER;
174217
@@ -181,7 +224,7 @@ index ae0f1380..3b4587f8 100644
181224 bp->keepalive_requests = 0;
182225 bp->keepalive_timeout = 0;
183226 bp->keepalive = 0;
184- @@ -465,10 +508 ,10 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
227+ @@ -465,10 +473 ,10 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
185228 pc->local = bp->local;
186229 }
187230
@@ -194,7 +237,7 @@ index ae0f1380..3b4587f8 100644
194237 pc->cached = 0;
195238 pc->connection = NULL;
196239
197- @@ -476,27 +519 ,59 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
240+ @@ -476,27 +484 ,59 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
198241 r->upstream->peer.tries += bp->more_tries;
199242 }
200243
@@ -267,7 +310,7 @@ index ae0f1380..3b4587f8 100644
267310 }
268311
269312 return NGX_OK;
270- @@ -577,14 +652 ,12 @@ static void
313+ @@ -577,14 +617 ,12 @@ static void
271314 ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
272315 ngx_uint_t state)
273316 {
@@ -284,7 +327,7 @@ index ae0f1380..3b4587f8 100644
284327
285328 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
286329 "lua balancer: free peer, tries: %ui", pc->tries);
287- @@ -592,14 +665 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
330+ @@ -592,14 +630 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
288331 u = bp->request->upstream;
289332 c = pc->connection;
290333
@@ -303,7 +346,7 @@ index ae0f1380..3b4587f8 100644
303346 if (state & NGX_PEER_FAILED
304347 || c == NULL
305348 || c->read->eof
306- @@ -633,42 +708 ,41 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
349+ @@ -633,42 +673 ,41 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
307350 goto invalid;
308351 }
309352
@@ -365,7 +408,7 @@ index ae0f1380..3b4587f8 100644
365408
366409 if (c->write->timer_set) {
367410 ngx_del_timer(c->write);
368- @@ -684,42 +758 ,6 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
411+ @@ -684,42 +723 ,6 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
369412 c->write->log = ngx_cycle->log;
370413 c->pool->log = ngx_cycle->log;
371414
@@ -408,7 +451,7 @@ index ae0f1380..3b4587f8 100644
408451 if (c->read->ready) {
409452 ngx_http_lua_balancer_close_handler(c->read);
410453 }
411- @@ -728,9 +766 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
454+ @@ -728,9 +731 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
412455
413456 invalid:
414457
@@ -428,7 +471,7 @@ index ae0f1380..3b4587f8 100644
428471 }
429472
430473 return;
431- @@ -740,6 +785 ,123 @@ invalid:
474+ @@ -740,6 +750 ,123 @@ invalid:
432475 }
433476
434477
@@ -552,7 +595,7 @@ index ae0f1380..3b4587f8 100644
552595 static void
553596 ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
554597 ngx_uint_t type)
555- @@ -755,6 +917 ,10 @@ ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
598+ @@ -755,6 +882 ,10 @@ ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
556599 static void
557600 ngx_http_lua_balancer_close(ngx_connection_t *c)
558601 {
@@ -563,7 +606,7 @@ index ae0f1380..3b4587f8 100644
563606 #if (NGX_HTTP_SSL)
564607 if (c->ssl) {
565608 c->ssl->no_wait_shutdown = 1;
566- @@ -762,9 +928 ,6 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
609+ @@ -762,9 +893 ,6 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
567610
568611 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
569612 c->ssl->handler = ngx_http_lua_balancer_close;
@@ -573,7 +616,7 @@ index ae0f1380..3b4587f8 100644
573616 return;
574617 }
575618 }
576- @@ -773,8 +936 ,12 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
619+ @@ -773,8 +901 ,12 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
577620 ngx_destroy_pool(c->pool);
578621 ngx_close_connection(c);
579622
@@ -588,7 +631,7 @@ index ae0f1380..3b4587f8 100644
588631 }
589632
590633
591- @@ -789,7 +956 ,7 @@ ngx_http_lua_balancer_dummy_handler(ngx_event_t *ev)
634+ @@ -789,7 +921 ,7 @@ ngx_http_lua_balancer_dummy_handler(ngx_event_t *ev)
592635 static void
593636 ngx_http_lua_balancer_close_handler(ngx_event_t *ev)
594637 {
@@ -597,7 +640,7 @@ index ae0f1380..3b4587f8 100644
597640
598641 int n;
599642 char buf[1];
600- @@ -820,8 +987 ,10 @@ close:
643+ @@ -820,8 +952 ,10 @@ close:
601644 ngx_http_lua_balancer_close(c);
602645
603646 ngx_queue_remove(&item->queue);
@@ -610,7 +653,7 @@ index ae0f1380..3b4587f8 100644
610653 }
611654
612655
613- @@ -832,7 +1001 ,7 @@ ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc, void *data)
656+ @@ -832,7 +966 ,7 @@ ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc, void *data)
614657 {
615658 ngx_http_lua_balancer_peer_data_t *bp = data;
616659
@@ -619,7 +662,7 @@ index ae0f1380..3b4587f8 100644
619662 /* TODO */
620663 return NGX_OK;
621664 }
622- @@ -846,7 +1015 ,7 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
665+ @@ -846,7 +980 ,7 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
623666 {
624667 ngx_http_lua_balancer_peer_data_t *bp = data;
625668
@@ -628,7 +671,7 @@ index ae0f1380..3b4587f8 100644
628671 /* TODO */
629672 return;
630673 }
631- @@ -859,9 +1028 ,8 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
674+ @@ -859,9 +993 ,8 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
632675
633676 int
634677 ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
@@ -640,7 +683,7 @@ index ae0f1380..3b4587f8 100644
640683 {
641684 ngx_url_t url;
642685 ngx_http_lua_ctx_t *ctx;
643- @@ -920,32 +1088 ,15 @@ ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
686+ @@ -920,32 +1053 ,15 @@ ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
644687 if (url.addrs && url.addrs[0].sockaddr) {
645688 bp->sockaddr = url.addrs[0].sockaddr;
646689 bp->socklen = url.addrs[0].socklen;
@@ -676,7 +719,7 @@ index ae0f1380..3b4587f8 100644
676719
677720 return NGX_OK;
678721 }
679- @@ -1050,11 +1201 ,15 @@ ngx_http_lua_ffi_balancer_enable_keepalive(ngx_http_request_t *r,
722+ @@ -1050,11 +1166 ,15 @@ ngx_http_lua_ffi_balancer_enable_keepalive(ngx_http_request_t *r,
680723
681724 bp = (ngx_http_lua_balancer_peer_data_t *) u->peer.data;
682725
@@ -693,7 +736,7 @@ index ae0f1380..3b4587f8 100644
693736 bp->keepalive_timeout = (ngx_msec_t) timeout;
694737 bp->keepalive_requests = (ngx_uint_t) max_requests;
695738 bp->keepalive = 1;
696- @@ -1375,138 +1530 ,4 @@ ngx_http_lua_balancer_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
739+ @@ -1375,138 +1495 ,4 @@ ngx_http_lua_balancer_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
697740 return NGX_CONF_OK;
698741 }
699742
0 commit comments