11diff --git src/ngx_http_lua_balancer.c src/ngx_http_lua_balancer.c
2- index ae0f1380..3faa0269 100644
2+ index ae0f1380..3b4587f8 100644
33--- src/ngx_http_lua_balancer.c
44+++ src/ngx_http_lua_balancer.c
55@@ -31,10 +31,39 @@ typedef struct {
@@ -84,7 +84,7 @@ index ae0f1380..3faa0269 100644
8484 static ngx_int_t ngx_http_lua_balancer_init(ngx_conf_t *cf,
8585 ngx_http_upstream_srv_conf_t *us);
8686 static ngx_int_t ngx_http_lua_balancer_init_peer(ngx_http_request_t *r,
87- @@ -87,17 +115,30 @@ static ngx_int_t ngx_http_lua_balancer_by_chunk(lua_State *L,
87+ @@ -87,17 +115,28 @@ static ngx_int_t ngx_http_lua_balancer_by_chunk(lua_State *L,
8888 ngx_http_request_t *r);
8989 static void ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc,
9090 void *data, ngx_uint_t state);
@@ -102,8 +102,8 @@ index ae0f1380..3faa0269 100644
102102 static void ngx_http_lua_balancer_close_handler(ngx_event_t *ev);
103103- static ngx_connection_t *ngx_http_lua_balancer_get_cached_item(
104104- ngx_http_lua_srv_conf_t *lscf, ngx_peer_connection_t *pc, ngx_str_t *name);
105- static ngx_uint_t ngx_http_lua_balancer_calc_hash(ngx_str_t *name,
106- struct sockaddr *sockaddr, socklen_t socklen, ngx_addr_t *local);
105+ - static ngx_uint_t ngx_http_lua_balancer_calc_hash(ngx_str_t *name,
106+ - struct sockaddr *sockaddr, socklen_t socklen, ngx_addr_t *local);
107107
108108
109109+ #define ngx_http_lua_balancer_keepalive_is_enabled(bp) \
@@ -117,7 +117,7 @@ index ae0f1380..3faa0269 100644
117117 static struct sockaddr *ngx_http_lua_balancer_default_server_sockaddr;
118118
119119
120- @@ -181,7 +222 ,7 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
120+ @@ -181,7 +220 ,7 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
121121
122122 dd("enter");
123123
@@ -126,15 +126,15 @@ index ae0f1380..3faa0269 100644
126126 if (cmd->post == NULL) {
127127 return NGX_CONF_ERROR;
128128 }
129- @@ -246,6 +287 ,7 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
129+ @@ -246,6 +285 ,7 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
130130 ngx_memzero(us, sizeof(ngx_http_upstream_server_t));
131131 ngx_memzero(&url, sizeof(ngx_url_t));
132132
133133+ /* just an invalid address as a place holder*/
134134 ngx_str_set(&url.url, "0.0.0.1");
135135 url.default_port = 80;
136136
137- @@ -261,8 +303 ,6 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
137+ @@ -261,8 +301 ,6 @@ ngx_http_lua_balancer_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
138138 }
139139
140140 if (uscf->peer.init_upstream) {
@@ -143,7 +143,7 @@ index ae0f1380..3faa0269 100644
143143
144144 lscf->balancer.original_init_upstream = uscf->peer.init_upstream;
145145
146- @@ -305,7 +345 ,7 @@ ngx_http_lua_balancer_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
146+ @@ -305,7 +343 ,7 @@ ngx_http_lua_balancer_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
147147 us->peer.init = ngx_http_lua_balancer_init_peer;
148148
149149 /* allocate cache items and add to free queue */
@@ -152,23 +152,23 @@ index ae0f1380..3faa0269 100644
152152 cached = ngx_pcalloc(cf->pool,
153153 sizeof(ngx_http_lua_balancer_ka_item_t)
154154 * lscf->balancer.max_cached);
155- @@ -387,6 +427 ,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
155+ @@ -387,6 +425 ,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
156156 void *pdata;
157157 lua_State *L;
158158 ngx_int_t rc;
159159+ ngx_queue_t *q;
160160 ngx_connection_t *c;
161161 ngx_http_request_t *r;
162162 #if (NGX_HTTP_SSL)
163- @@ -394,6 +435 ,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
163+ @@ -394,6 +433 ,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
164164 #endif
165165 ngx_http_lua_ctx_t *ctx;
166166 ngx_http_lua_srv_conf_t *lscf;
167167+ ngx_http_lua_balancer_keepalive_item_t *item;
168168 ngx_http_lua_balancer_peer_data_t *bp = data;
169169
170170 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
171- @@ -425,9 +467 ,12 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
171+ @@ -425,9 +465 ,12 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
172172
173173 ctx->context = NGX_HTTP_LUA_CONTEXT_BALANCER;
174174
@@ -181,7 +181,7 @@ index ae0f1380..3faa0269 100644
181181 bp->keepalive_requests = 0;
182182 bp->keepalive_timeout = 0;
183183 bp->keepalive = 0;
184- @@ -465,10 +510 ,10 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
184+ @@ -465,10 +508 ,10 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
185185 pc->local = bp->local;
186186 }
187187
@@ -194,7 +194,7 @@ index ae0f1380..3faa0269 100644
194194 pc->cached = 0;
195195 pc->connection = NULL;
196196
197- @@ -476,27 +521 ,59 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
197+ @@ -476,27 +519 ,59 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
198198 r->upstream->peer.tries += bp->more_tries;
199199 }
200200
@@ -267,7 +267,7 @@ index ae0f1380..3faa0269 100644
267267 }
268268
269269 return NGX_OK;
270- @@ -577,14 +654 ,12 @@ static void
270+ @@ -577,14 +652 ,12 @@ static void
271271 ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
272272 ngx_uint_t state)
273273 {
@@ -284,7 +284,7 @@ index ae0f1380..3faa0269 100644
284284
285285 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
286286 "lua balancer: free peer, tries: %ui", pc->tries);
287- @@ -592,14 +667 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
287+ @@ -592,14 +665 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
288288 u = bp->request->upstream;
289289 c = pc->connection;
290290
@@ -303,7 +303,7 @@ index ae0f1380..3faa0269 100644
303303 if (state & NGX_PEER_FAILED
304304 || c == NULL
305305 || c->read->eof
306- @@ -633,42 +710 ,41 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
306+ @@ -633,42 +708 ,41 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
307307 goto invalid;
308308 }
309309
@@ -365,7 +365,7 @@ index ae0f1380..3faa0269 100644
365365
366366 if (c->write->timer_set) {
367367 ngx_del_timer(c->write);
368- @@ -684,42 +760 ,6 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
368+ @@ -684,42 +758 ,6 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
369369 c->write->log = ngx_cycle->log;
370370 c->pool->log = ngx_cycle->log;
371371
@@ -408,7 +408,7 @@ index ae0f1380..3faa0269 100644
408408 if (c->read->ready) {
409409 ngx_http_lua_balancer_close_handler(c->read);
410410 }
411- @@ -728,9 +768 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
411+ @@ -728,9 +766 ,16 @@ ngx_http_lua_balancer_free_peer(ngx_peer_connection_t *pc, void *data,
412412
413413 invalid:
414414
@@ -428,7 +428,7 @@ index ae0f1380..3faa0269 100644
428428 }
429429
430430 return;
431- @@ -740,6 +787 ,123 @@ invalid:
431+ @@ -740,6 +785 ,123 @@ invalid:
432432 }
433433
434434
@@ -552,7 +552,7 @@ index ae0f1380..3faa0269 100644
552552 static void
553553 ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
554554 ngx_uint_t type)
555- @@ -755,6 +919 ,10 @@ ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
555+ @@ -755,6 +917 ,10 @@ ngx_http_lua_balancer_notify_peer(ngx_peer_connection_t *pc, void *data,
556556 static void
557557 ngx_http_lua_balancer_close(ngx_connection_t *c)
558558 {
@@ -563,7 +563,7 @@ index ae0f1380..3faa0269 100644
563563 #if (NGX_HTTP_SSL)
564564 if (c->ssl) {
565565 c->ssl->no_wait_shutdown = 1;
566- @@ -762,9 +930 ,6 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
566+ @@ -762,9 +928 ,6 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
567567
568568 if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
569569 c->ssl->handler = ngx_http_lua_balancer_close;
@@ -573,7 +573,7 @@ index ae0f1380..3faa0269 100644
573573 return;
574574 }
575575 }
576- @@ -773,8 +938 ,12 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
576+ @@ -773,8 +936 ,12 @@ ngx_http_lua_balancer_close(ngx_connection_t *c)
577577 ngx_destroy_pool(c->pool);
578578 ngx_close_connection(c);
579579
@@ -588,7 +588,7 @@ index ae0f1380..3faa0269 100644
588588 }
589589
590590
591- @@ -789,7 +958 ,7 @@ ngx_http_lua_balancer_dummy_handler(ngx_event_t *ev)
591+ @@ -789,7 +956 ,7 @@ ngx_http_lua_balancer_dummy_handler(ngx_event_t *ev)
592592 static void
593593 ngx_http_lua_balancer_close_handler(ngx_event_t *ev)
594594 {
@@ -597,7 +597,7 @@ index ae0f1380..3faa0269 100644
597597
598598 int n;
599599 char buf[1];
600- @@ -820,8 +989 ,10 @@ close:
600+ @@ -820,8 +987 ,10 @@ close:
601601 ngx_http_lua_balancer_close(c);
602602
603603 ngx_queue_remove(&item->queue);
@@ -610,7 +610,7 @@ index ae0f1380..3faa0269 100644
610610 }
611611
612612
613- @@ -832,7 +1003 ,7 @@ ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc, void *data)
613+ @@ -832,7 +1001 ,7 @@ ngx_http_lua_balancer_set_session(ngx_peer_connection_t *pc, void *data)
614614 {
615615 ngx_http_lua_balancer_peer_data_t *bp = data;
616616
@@ -619,7 +619,7 @@ index ae0f1380..3faa0269 100644
619619 /* TODO */
620620 return NGX_OK;
621621 }
622- @@ -846,7 +1017 ,7 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
622+ @@ -846,7 +1015 ,7 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
623623 {
624624 ngx_http_lua_balancer_peer_data_t *bp = data;
625625
@@ -628,7 +628,7 @@ index ae0f1380..3faa0269 100644
628628 /* TODO */
629629 return;
630630 }
631- @@ -859,9 +1030 ,8 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
631+ @@ -859,9 +1028 ,8 @@ ngx_http_lua_balancer_save_session(ngx_peer_connection_t *pc, void *data)
632632
633633 int
634634 ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
@@ -640,7 +640,7 @@ index ae0f1380..3faa0269 100644
640640 {
641641 ngx_url_t url;
642642 ngx_http_lua_ctx_t *ctx;
643- @@ -920,32 +1090 ,15 @@ ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
643+ @@ -920,32 +1088 ,15 @@ ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
644644 if (url.addrs && url.addrs[0].sockaddr) {
645645 bp->sockaddr = url.addrs[0].sockaddr;
646646 bp->socklen = url.addrs[0].socklen;
@@ -676,7 +676,7 @@ index ae0f1380..3faa0269 100644
676676
677677 return NGX_OK;
678678 }
679- @@ -1050,11 +1203 ,15 @@ ngx_http_lua_ffi_balancer_enable_keepalive(ngx_http_request_t *r,
679+ @@ -1050,11 +1201 ,15 @@ ngx_http_lua_ffi_balancer_enable_keepalive(ngx_http_request_t *r,
680680
681681 bp = (ngx_http_lua_balancer_peer_data_t *) u->peer.data;
682682
@@ -693,10 +693,11 @@ index ae0f1380..3faa0269 100644
693693 bp->keepalive_timeout = (ngx_msec_t) timeout;
694694 bp->keepalive_requests = (ngx_uint_t) max_requests;
695695 bp->keepalive = 1;
696- @@ -1376,58 +1533,6 @@ ngx_http_lua_balancer_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
696+ @@ -1375,138 +1530,4 @@ ngx_http_lua_balancer_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
697+ return NGX_CONF_OK;
697698 }
698699
699-
700+ -
700701- #if (NGX_HTTP_SSL)
701702- static ngx_int_t
702703- ngx_http_lua_upstream_get_ssl_name(ngx_http_request_t *r,
@@ -749,13 +750,21 @@ index ae0f1380..3faa0269 100644
749750- #endif
750751-
751752-
752- static ngx_uint_t
753- ngx_http_lua_balancer_calc_hash(ngx_str_t *name,
754- struct sockaddr *sockaddr, socklen_t socklen, ngx_addr_t *local)
755- @@ -1443,70 +1548,4 @@ ngx_http_lua_balancer_calc_hash(ngx_str_t *name,
756- return hash;
757- }
758-
753+ - static ngx_uint_t
754+ - ngx_http_lua_balancer_calc_hash(ngx_str_t *name,
755+ - struct sockaddr *sockaddr, socklen_t socklen, ngx_addr_t *local)
756+ - {
757+ - ngx_uint_t hash;
758+ -
759+ - hash = ngx_hash_key_lc(name->data, name->len);
760+ - hash ^= ngx_hash_key((u_char *) sockaddr, socklen);
761+ - if (local != NULL) {
762+ - hash ^= ngx_hash_key((u_char *) local->sockaddr, local->socklen);
763+ - }
764+ -
765+ - return hash;
766+ - }
767+ -
759768-
760769- static ngx_connection_t *
761770- ngx_http_lua_balancer_get_cached_item(ngx_http_lua_srv_conf_t *lscf,
0 commit comments