Skip to content

Commit 6683a8b

Browse files
刘斌6agentzh
刘斌6
authored andcommitted
refactor: simplifies the fix in commit 4739266.
basically, change of peers in balancer_by_lua* would lead to bogus $upstream_addr values which were always the last peer's name. Signed-off-by: Yichun Zhang (agentzh) <[email protected]>
1 parent df5bf1d commit 6683a8b

File tree

1 file changed

+3
-27
lines changed

1 file changed

+3
-27
lines changed

src/ngx_http_lua_balancer.c

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct ngx_http_lua_balancer_peer_data_s {
2929
struct sockaddr *sockaddr;
3030
socklen_t socklen;
3131

32-
ngx_str_t host;
32+
ngx_str_t *host;
3333
in_port_t port;
3434

3535
int last_peer_state;
@@ -255,7 +255,6 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
255255
{
256256
lua_State *L;
257257
ngx_int_t rc;
258-
ngx_str_t *peer_name;
259258
ngx_http_request_t *r;
260259
ngx_http_lua_ctx_t *ctx;
261260
ngx_http_lua_srv_conf_t *lscf;
@@ -325,30 +324,7 @@ ngx_http_lua_balancer_get_peer(ngx_peer_connection_t *pc, void *data)
325324
pc->socklen = bp->socklen;
326325
pc->cached = 0;
327326
pc->connection = NULL;
328-
329-
/*
330-
* our dynamic balancers have no real peer actually, it just creates
331-
* one virtual peer when necessary. because the
332-
* peer_name is returned as a pointer, its content can change after
333-
* invoking balancer_by_lua*. if we just save the pointer,
334-
* nginx might always use the last peer_name. To address this issue,
335-
* we always create a copy of bp->host and save it as pc->name.
336-
*/
337-
338-
peer_name = ngx_palloc(r->pool, sizeof(ngx_str_t));
339-
if (peer_name == NULL) {
340-
return NGX_ERROR;
341-
}
342-
343-
peer_name->data = ngx_palloc(r->pool, bp->host.len);
344-
if (peer_name->data == NULL) {
345-
return NGX_ERROR;
346-
}
347-
348-
ngx_memcpy(peer_name->data, bp->host.data, bp->host.len);
349-
peer_name->len = bp->host.len;
350-
351-
pc->name = peer_name;
327+
pc->name = bp->host;
352328

353329
bp->rrp.peers->single = 0;
354330

@@ -549,7 +525,7 @@ ngx_http_lua_ffi_balancer_set_current_peer(ngx_http_request_t *r,
549525
if (url.addrs && url.addrs[0].sockaddr) {
550526
bp->sockaddr = url.addrs[0].sockaddr;
551527
bp->socklen = url.addrs[0].socklen;
552-
bp->host = url.addrs[0].name;
528+
bp->host = &url.addrs[0].name;
553529

554530
} else {
555531
*err = "no host allowed";

0 commit comments

Comments
 (0)