diff --git a/.travis.yml b/.travis.yml index ada732b5f..5adbde7f5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -70,7 +70,7 @@ install: - git clone https://github.com/openresty/openresty.git ../openresty - git clone https://github.com/openresty/openresty-devel-utils.git - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module - - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module + - git clone -b feature/balancer_proxy_bind https://github.com/ytlm/lua-nginx-module.git ../lua-nginx-module - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module - git clone https://github.com/openresty/lua-resty-lrucache.git @@ -79,7 +79,7 @@ install: - git clone https://github.com/openresty/set-misc-nginx-module.git ../set-misc-nginx-module - git clone https://github.com/openresty/mockeagain.git - git clone https://github.com/openresty/test-nginx.git - - git clone https://github.com/openresty/stream-lua-nginx-module.git ../stream-lua-nginx-module + - git clone -b feature/balancer_proxy_bind https://github.com/ytlm/stream-lua-nginx-module.git ../stream-lua-nginx-module script: - cd luajit2/ @@ -107,7 +107,7 @@ script: - if [ "$OPENSSL_VER" = "1.1.0l" ] || [ "$answer" = "N" ]; then add_http3_module=""; fi - if [ "$answer" = "N" ] || [ "$USE_PCRE2" = "Y" ]; then disable_pcre2=""; fi - if [ "$USE_PCRE2" = "Y" ]; then PCRE_INC=$PCRE2_INC; PCRE_LIB=$PCRE2_LIB; fi - - ngx-build $NGINX_VERSION --with-ipv6 $disable_pcre2 $add_http3_module --with-http_v2_module --with-http_realip_module --with-http_ssl_module --with-pcre-jit --with-cc-opt="-I$OPENSSL_INC -I$PCRE_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB -L$PCRE_LIB -Wl,-rpath,$PCRE_LIB" --add-module=../ndk-nginx-module --add-module=../echo-nginx-module --add-module=../set-misc-nginx-module --add-module=../headers-more-nginx-module --add-module=../lua-nginx-module --with-debug --with-stream_ssl_module --with-stream --with-ipv6 --add-module=../stream-lua-nginx-module > build.log 2>&1 || (cat build.log && exit 1) + - ngx-build $NGINX_VERSION $disable_pcre2 $add_http3_module --with-http_v2_module --with-http_realip_module --with-http_ssl_module --with-pcre-jit --with-cc-opt="-I$OPENSSL_INC -I$PCRE_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB -L$PCRE_LIB -Wl,-rpath,$PCRE_LIB" --add-module=../ndk-nginx-module --add-module=../echo-nginx-module --add-module=../set-misc-nginx-module --add-module=../headers-more-nginx-module --add-module=../lua-nginx-module --with-debug --with-stream_ssl_module --with-stream --with-ipv6 --add-module=../stream-lua-nginx-module > build.log 2>&1 || (cat build.log && exit 1) - nginx -V - ldd `which nginx`|grep -E 'luajit|ssl|pcre' - prove -I. -Itest-nginx/lib -j$JOBS -r t diff --git a/lib/ngx/balancer.lua b/lib/ngx/balancer.lua index 5bf48a46c..909de491c 100644 --- a/lib/ngx/balancer.lua +++ b/lib/ngx/balancer.lua @@ -93,6 +93,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_bind_to_local_addr(ngx_stream_lua_request_t *r, + const unsigned char *addr, size_t addr_len, char **err); ]] ngx_lua_ffi_balancer_set_current_peer = @@ -115,6 +118,9 @@ elseif subsystem == 'stream' then timeout, err) end + ngx_lua_ffi_balancer_bind_to_local_addr = + C.ngx_stream_lua_ffi_balancer_bind_to_local_addr + else error("unknown subsystem: " .. subsystem) end @@ -362,38 +368,29 @@ if subsystem == 'http' then end end -if subsystem == "http" then - function _M.bind_to_local_addr(addr) - local r = get_request() - if not r then - error("no request found") - end - - if type(addr) ~= "string" then - error("bad argument #1 to 'bind_to_local_addr' " - .. "(string expected, got " .. type(addr) .. ")") - end - - local errbuf_size = 1024 - local errbuf = get_string_buf(errbuf_size) - local sizep = get_size_ptr() - sizep[0] = errbuf_size - local rc = ngx_lua_ffi_balancer_bind_to_local_addr(r, addr, #addr, - errbuf, - sizep) - if rc == FFI_OK then - return true - end +function _M.bind_to_local_addr(addr) + local r = get_request() + if not r then + error("no request found") + end - return nil, ffi_str(errbuf, sizep[0]) + if type(addr) ~= "string" then + error("bad argument #1 to 'bind_to_local_addr' " + .. "(string expected, got " .. type(addr) .. ")") end -else - function _M.bind_to_local_addr(addr) - error("'bind_to_local_addr' not yet implemented in " .. subsystem .. - " subsystem", 2) + local errbuf_size = 1024 + local errbuf = get_string_buf(errbuf_size) + local sizep = get_size_ptr() + sizep[0] = errbuf_size + local rc = ngx_lua_ffi_balancer_bind_to_local_addr(r, addr, #addr, + errbuf, + sizep) + if rc == FFI_OK then + return true end -end + return nil, ffi_str(errbuf, sizep[0]) +end return _M diff --git a/t/stream/upstream-balancer-set-proxy-bind.t b/t/stream/upstream-balancer-set-proxy-bind.t new file mode 100644 index 000000000..5124863a5 --- /dev/null +++ b/t/stream/upstream-balancer-set-proxy-bind.t @@ -0,0 +1,119 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: +use lib '.'; +use t::TestCore::Stream; + +repeat_each(2); + +plan tests => repeat_each() * (blocks() * 4); + +$ENV{TEST_NGINX_LUA_PACKAGE_PATH} = "$t::TestCore::Stream::lua_package_path"; + +no_long_string(); +run_tests(); + +__DATA__ + +=== TEST 1: balancer +--- stream_config + lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; + + upstream backend { + server 0.0.0.1:1234 down; + balancer_by_lua_block { + local b = require "ngx.balancer" + assert(b.set_current_peer("127.0.0.1", 12345)) + } + } + + server { + listen 127.0.0.1:12345; + content_by_lua_block { + ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port) + } + } +--- stream_server_config + proxy_pass backend; + +--- request + GET /t +--- response_body eval +[ +qr/127.0.0.1/, +] +--- error_code: 200 +--- no_error_log +[error] +[warn] + + + +=== TEST 2: balancer with set_proxy_bind (addr) +--- stream_config + lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; + + upstream backend { + server 0.0.0.1:1234 down; + balancer_by_lua_block { + local b = require "ngx.balancer" + assert(b.set_current_peer("127.0.0.1", 12345)) + + assert(b.set_proxy_bind("127.0.0.4")) + } + } + + server { + listen 127.0.0.1:12345; + content_by_lua_block { + ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port) + } + } +--- stream_server_config + proxy_pass backend; + +--- request + GET /t +--- response_body eval +[ +qr/127.0.0.4/, +] +--- error_code: 200 +--- no_error_log +[error] +[warn] + + + +=== TEST 3: balancer with set_proxy_bind (addr and port) +--- stream_config + lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; + + upstream backend { + server 0.0.0.1:1234 down; + balancer_by_lua_block { + local b = require "ngx.balancer" + assert(b.set_current_peer("127.0.0.1", 12345)) + + assert(b.set_proxy_bind("127.0.0.8:23456")) + } + } + + server { + listen 127.0.0.1:12345; + content_by_lua_block { + ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port) + } + } +--- stream_server_config + proxy_pass backend; + +--- request + GET /t +--- response_body eval +[ +qr/127.0.0.8/, +] +--- error_code: 200 +--- no_error_log +[error] +[warn] + diff --git a/t/upstream-balancer-set-proxy-bind.t b/t/upstream-balancer-set-proxy-bind.t new file mode 100644 index 000000000..9357ca81a --- /dev/null +++ b/t/upstream-balancer-set-proxy-bind.t @@ -0,0 +1,125 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: +use lib '.'; +use t::TestCore; + +#worker_connections(1014); +#master_on(); +#workers(2); +#log_level('warn'); + +repeat_each(2); + +plan tests => repeat_each() * (blocks() * 4); + +$ENV{TEST_NGINX_LUA_PACKAGE_PATH} = "$t::TestCore::lua_package_path"; + +#worker_connections(1024); +#no_diff(); +no_long_string(); +run_tests(); + +__DATA__ + +=== TEST 1: balancer +--- http_config + lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; + + upstream backend { + server 0.0.0.1 down; + balancer_by_lua_block { + local b = require "ngx.balancer" + assert(b.set_current_peer(ngx.var.server_addr, ngx.var.server_port)) + } + } +--- config + location = /t { + proxy_pass http://backend/echo; + } + + location = /echo { + content_by_lua_block { + ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port) + } + } +--- request + GET /t +--- response_body eval +[ +qr/127.0.0.1/, +] +--- error_code: 200 +--- no_error_log +[error] +[warn] + + + +=== TEST 2: balancer with set_proxy_bind (addr) +--- http_config + lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; + + upstream backend { + server 0.0.0.1 down; + balancer_by_lua_block { + local b = require "ngx.balancer" + assert(b.set_current_peer(ngx.var.server_addr, ngx.var.server_port)) + + assert(b.set_proxy_bind("127.0.0.4")) + } + } +--- config + location = /t { + proxy_pass http://backend/echo; + } + + location = /echo { + content_by_lua_block { + ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port) + } + } +--- request + GET /t +--- response_body eval +[ +qr/127.0.0.4/, +] +--- error_code: 200 +--- no_error_log +[error] +[warn] + + + +=== TEST 3: balancer with set_proxy_bind (addr and port) +--- http_config + lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH"; + + upstream backend { + server 0.0.0.1 down; + balancer_by_lua_block { + local b = require "ngx.balancer" + assert(b.set_current_peer(ngx.var.server_addr, ngx.var.server_port)) + + assert(b.set_proxy_bind("127.0.0.8:23456")) + } + } +--- config + location = /t { + proxy_pass http://backend/echo; + } + + location = /echo { + content_by_lua_block { + ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port) + } + } +--- request + GET /t +--- response_body eval +[ +qr/127.0.0.8/, +] +--- error_code: 200 +--- no_error_log +[error] +[warn]