Skip to content

Commit

Permalink
feature: support lua balancer set proxy bind dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
ytlm committed Jun 27, 2024
1 parent f9add68 commit 7c922ab
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/ngx/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local ngx_lua_ffi_balancer_set_more_tries
local ngx_lua_ffi_balancer_get_last_failure
local ngx_lua_ffi_balancer_set_timeouts -- used by both stream and http
local ngx_lua_ffi_balancer_set_upstream_tls
local ngx_lua_ffi_balancer_set_proxy_bind


if subsystem == 'http' then
Expand All @@ -44,6 +45,9 @@ if subsystem == 'http' then
char **err);
int ngx_http_lua_ffi_balancer_set_upstream_tls(ngx_http_request_t *r,
int on, char **err);

int ngx_http_lua_ffi_balancer_set_proxy_bind(ngx_http_request_t *r,
const unsigned char *addr, size_t addr_len, char **err);
]]

ngx_lua_ffi_balancer_set_current_peer =
Expand All @@ -61,6 +65,9 @@ if subsystem == 'http' then
ngx_lua_ffi_balancer_set_upstream_tls =
C.ngx_http_lua_ffi_balancer_set_upstream_tls

ngx_lua_ffi_balancer_set_proxy_bind =
C.ngx_http_lua_ffi_balancer_set_proxy_bind

elseif subsystem == 'stream' then
ffi.cdef[[
int ngx_stream_lua_ffi_balancer_set_current_peer(
Expand All @@ -75,6 +82,9 @@ elseif subsystem == 'stream' then

int ngx_stream_lua_ffi_balancer_set_timeouts(ngx_stream_lua_request_t *r,
long connect_timeout, long timeout, char **err);

int ngx_stream_lua_ffi_balancer_set_proxy_bind(ngx_stream_lua_request_t *r,
const unsigned char *addr, size_t addr_len, char **err);
]]

ngx_lua_ffi_balancer_set_current_peer =
Expand All @@ -97,6 +107,9 @@ elseif subsystem == 'stream' then
timeout, err)
end

ngx_lua_ffi_balancer_set_proxy_bind =
C.ngx_stream_lua_ffi_balancer_set_proxy_bind

else
error("unknown subsystem: " .. subsystem)
end
Expand Down Expand Up @@ -260,4 +273,19 @@ if subsystem == 'http' then
end


function _M.set_proxy_bind(addr)
local r = get_request()
if not r then
error("no request found")
end

local rc = ngx_lua_ffi_balancer_set_proxy_bind(r, addr, #addr, errmsg)
if rc == FFI_OK then
return true
end

return nil, ffi_str(errmsg[0])
end


return _M

0 comments on commit 7c922ab

Please sign in to comment.