From cfdffec477f319feb8e1cdb48d962954be41bc11 Mon Sep 17 00:00:00 2001 From: ytlm Date: Thu, 27 Jun 2024 09:32:06 +0800 Subject: [PATCH 1/5] feature: support lua balancer set proxy bind dynamic --- src/ngx_stream_lua_balancer.c | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index aafe0eea..c1be23cc 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -752,4 +752,70 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_lua_request_t *r, } +int +ngx_stream_lua_ffi_balancer_set_proxy_bind(ngx_stream_lua_request_t *r, + const u_char *addr, size_t addr_len, char *err) +{ + ngx_int_t rc; + ngx_str_t addr_str; + ngx_addr_t *addr_val; + ngx_stream_lua_ctx_t *ctx; + ngx_stream_upstream_t *u; + + if (r == NULL) { + *err = "no request found"; + return NGX_ERROR; + } + + u = r->session->upstream; + if (u == NULL || u->peer == NULL) { + *err = "no upstream found"; + return NGX_ERROR; + } + + ctx = ngx_stream_lua_get_module_ctx(r, ngx_stream_lua_module); + if (ctx == NULL) { + *err = "no ctx found"; + return NGX_ERROR; + } + + if ((ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) == 0) { + *err = "API disabled in the current context"; + return NGX_ERROR; + } + + addr_val = ngx_pcalloc(r->pool, sizeof(ngx_addr_t)); + if (addr_val == NULL) { + *err = "no memory for addr_val"; + return NGX_ERROR; + } + + addr_str.len = addr_len; + addr_str.data = ngx_palloc(r->pool, addr_len); + if (addr_str.data == NULL) { + *err = "no memory for addr_str"; + return NGX_ERROR; + } + + ngx_memcpy(addr_str.data, addr, addr_len); + + rc = ngx_parse_addr_port(r->pool, addr_val, addr_str.data, addr_str.len); + if (rc == NGX_ERROR) { + *err = "parse addr port failed"; + return NGX_ERROR; + } + + if (rc != NGX_OK) { + *err = "invalid addr"; + return NGX_ERROR; + } + + addr_val->name = addr_str; + + u->peer.local = addr_val; + + return NGX_OK; +} + + /* vi:set ft=c ts=4 sw=4 et fdm=marker: */ From 593bf42f962d656065fad7430cfcac9b6f954cdf Mon Sep 17 00:00:00 2001 From: ytlm Date: Thu, 27 Jun 2024 10:26:23 +0800 Subject: [PATCH 2/5] fix: err param definition --- src/ngx_stream_lua_balancer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index c1be23cc..628e6a86 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -754,7 +754,7 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_lua_request_t *r, int ngx_stream_lua_ffi_balancer_set_proxy_bind(ngx_stream_lua_request_t *r, - const u_char *addr, size_t addr_len, char *err) + const u_char *addr, size_t addr_len, char **err) { ngx_int_t rc; ngx_str_t addr_str; From 71a64430fbfc099a9640531df17ddba48d262cc6 Mon Sep 17 00:00:00 2001 From: ytlm Date: Thu, 27 Jun 2024 10:35:39 +0800 Subject: [PATCH 3/5] fix: ngx_stream_upstream_t peer --- src/ngx_stream_lua_balancer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 628e6a86..02df97e8 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -768,7 +768,7 @@ ngx_stream_lua_ffi_balancer_set_proxy_bind(ngx_stream_lua_request_t *r, } u = r->session->upstream; - if (u == NULL || u->peer == NULL) { + if (u == NULL) { *err = "no upstream found"; return NGX_ERROR; } From 05d16e1962829653841ed9a4d8cbdd82bfeaa3c8 Mon Sep 17 00:00:00 2001 From: ytlm Date: Mon, 1 Jul 2024 15:31:52 +0800 Subject: [PATCH 4/5] fix: same with lua-nginx-module --- src/ngx_stream_lua_balancer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index 02df97e8..d69f4cca 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -753,7 +753,7 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_lua_request_t *r, int -ngx_stream_lua_ffi_balancer_set_proxy_bind(ngx_stream_lua_request_t *r, +ngx_stream_lua_ffi_balancer_bind_to_local_addr(ngx_stream_lua_request_t *r, const u_char *addr, size_t addr_len, char **err) { ngx_int_t rc; From 00fe84479e1a248c8090414072f3d395d6e2403f Mon Sep 17 00:00:00 2001 From: ytlm Date: Mon, 1 Jul 2024 15:42:28 +0800 Subject: [PATCH 5/5] fix: same with lua-nginx-module --- src/ngx_stream_lua_balancer.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/ngx_stream_lua_balancer.c b/src/ngx_stream_lua_balancer.c index d69f4cca..73b714c7 100644 --- a/src/ngx_stream_lua_balancer.c +++ b/src/ngx_stream_lua_balancer.c @@ -754,8 +754,9 @@ ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_lua_request_t *r, int ngx_stream_lua_ffi_balancer_bind_to_local_addr(ngx_stream_lua_request_t *r, - const u_char *addr, size_t addr_len, char **err) + const u_char *addr, size_t addr_len, u_char *errbuf, size_t *errbuf_size) { + u_char *p; ngx_int_t rc; ngx_str_t addr_str; ngx_addr_t *addr_val; @@ -763,37 +764,43 @@ ngx_stream_lua_ffi_balancer_bind_to_local_addr(ngx_stream_lua_request_t *r, ngx_stream_upstream_t *u; if (r == NULL) { - *err = "no request found"; + p = ngx_snprintf(errbuf, *errbuf_size, "no request found"); + *errbuf_size = p - errbuf; return NGX_ERROR; } u = r->session->upstream; if (u == NULL) { - *err = "no upstream found"; + p = ngx_snprintf(errbuf, *errbuf_size, "no upstream found"); + *errbuf_size = p - errbuf; return NGX_ERROR; } ctx = ngx_stream_lua_get_module_ctx(r, ngx_stream_lua_module); if (ctx == NULL) { - *err = "no ctx found"; + p = ngx_snprintf(errbuf, *errbuf_size, "no ctx found"); + *errbuf_size = p - errbuf; return NGX_ERROR; } if ((ctx->context & NGX_STREAM_LUA_CONTEXT_BALANCER) == 0) { - *err = "API disabled in the current context"; + p = ngx_snprintf(errbuf, *errbuf_size, "API disabled in the current context"); + *errbuf_size = p - errbuf; return NGX_ERROR; } addr_val = ngx_pcalloc(r->pool, sizeof(ngx_addr_t)); if (addr_val == NULL) { - *err = "no memory for addr_val"; + p = ngx_snprintf(errbuf, *errbuf_size, "no memory for addr_val"); + *errbuf_size = p - errbuf; return NGX_ERROR; } addr_str.len = addr_len; addr_str.data = ngx_palloc(r->pool, addr_len); if (addr_str.data == NULL) { - *err = "no memory for addr_str"; + p = ngx_snprintf(errbuf, *errbuf_size, "no memory for addr_str"); + *errbuf_size = p - errbuf; return NGX_ERROR; } @@ -801,12 +808,14 @@ ngx_stream_lua_ffi_balancer_bind_to_local_addr(ngx_stream_lua_request_t *r, rc = ngx_parse_addr_port(r->pool, addr_val, addr_str.data, addr_str.len); if (rc == NGX_ERROR) { - *err = "parse addr port failed"; + p = ngx_snprintf(errbuf, *errbuf_size, "parse addr port failed"); + *errbuf_size = p - errbuf; return NGX_ERROR; } if (rc != NGX_OK) { - *err = "invalid addr"; + p = ngx_snprintf(errbuf, *errbuf_size, "invalid addr"); + *errbuf_size = p - errbuf; return NGX_ERROR; }