Skip to content

Commit 799d9b1

Browse files
feature: add balancer.bind_to_local_addr for stream module.
Co-author: ytlm <[email protected]>
1 parent fb70407 commit 799d9b1

File tree

2 files changed

+137
-27
lines changed

2 files changed

+137
-27
lines changed

lib/ngx/balancer.lua

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ local FFI_OK = base.FFI_OK
1313
local FFI_ERROR = base.FFI_ERROR
1414
local int_out = ffi.new("int[1]")
1515
local get_request = base.get_request
16+
local get_string_buf = base.get_string_buf
17+
local get_size_ptr = base.get_size_ptr
18+
1619
local error = error
1720
local type = type
1821
local tonumber = tonumber
1922
local max = math.max
23+
2024
local subsystem = ngx.config.subsystem
2125
local ngx_lua_ffi_balancer_set_current_peer
2226
local ngx_lua_ffi_balancer_enable_keepalive
@@ -93,6 +97,10 @@ elseif subsystem == 'stream' then
9397

9498
int ngx_stream_lua_ffi_balancer_set_timeouts(ngx_stream_lua_request_t *r,
9599
long connect_timeout, long timeout, char **err);
100+
101+
int ngx_stream_lua_ffi_balancer_bind_to_local_addr(
102+
ngx_stream_lua_request_t *r, const char *addr, size_t addr_len,
103+
char *errbuf, size_t *errbuf_size);
96104
]]
97105

98106
ngx_lua_ffi_balancer_set_current_peer =
@@ -115,6 +123,9 @@ elseif subsystem == 'stream' then
115123
timeout, err)
116124
end
117125

126+
ngx_lua_ffi_balancer_bind_to_local_addr =
127+
C.ngx_stream_lua_ffi_balancer_bind_to_local_addr
128+
118129
else
119130
error("unknown subsystem: " .. subsystem)
120131
end
@@ -362,37 +373,29 @@ if subsystem == 'http' then
362373
end
363374
end
364375

365-
if subsystem == "http" then
366-
function _M.bind_to_local_addr(addr)
367-
local r = get_request()
368-
if not r then
369-
error("no request found")
370-
end
371-
372-
if type(addr) ~= "string" then
373-
error("bad argument #1 to 'bind_to_local_addr' "
374-
.. "(string expected, got " .. type(addr) .. ")")
375-
end
376-
377-
local errbuf_size = 1024
378-
local errbuf = get_string_buf(errbuf_size)
379-
local sizep = get_size_ptr()
380-
sizep[0] = errbuf_size
381-
local rc = ngx_lua_ffi_balancer_bind_to_local_addr(r, addr, #addr,
382-
errbuf,
383-
sizep)
384-
if rc == FFI_OK then
385-
return true
386-
end
376+
function _M.bind_to_local_addr(addr)
377+
local r = get_request()
378+
if not r then
379+
error("no request found")
380+
end
387381

388-
return nil, ffi_str(errbuf, sizep[0])
382+
if type(addr) ~= "string" then
383+
error("bad argument #1 to 'bind_to_local_addr' "
384+
.. "(string expected, got " .. type(addr) .. ")")
389385
end
390386

391-
else
392-
function _M.bind_to_local_addr(addr)
393-
error("'bind_to_local_addr' not yet implemented in " .. subsystem ..
394-
" subsystem", 2)
387+
local errbuf_size = 1024
388+
local errbuf = get_string_buf(errbuf_size)
389+
local sizep = get_size_ptr()
390+
sizep[0] = errbuf_size
391+
local rc = ngx_lua_ffi_balancer_bind_to_local_addr(r, addr, #addr,
392+
errbuf,
393+
sizep)
394+
if rc == FFI_OK then
395+
return true
395396
end
397+
398+
return nil, ffi_str(errbuf, sizep[0])
396399
end
397400

398401

t/stream/balancer-bind.t

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
use lib '.';
3+
use t::TestCore::Stream;
4+
5+
repeat_each(2);
6+
7+
plan tests => repeat_each() * (blocks() * 4);
8+
9+
$ENV{TEST_NGINX_LUA_PACKAGE_PATH} = "$t::TestCore::Stream::lua_package_path";
10+
11+
no_long_string();
12+
run_tests();
13+
14+
__DATA__
15+
16+
=== TEST 1: balancer
17+
--- stream_config
18+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
19+
upstream backend {
20+
server 0.0.0.1:1234 down;
21+
balancer_by_lua_block {
22+
local b = require "ngx.balancer"
23+
assert(b.set_current_peer("127.0.0.1", 12345))
24+
}
25+
}
26+
server {
27+
listen 127.0.0.1:12345;
28+
content_by_lua_block {
29+
ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port)
30+
}
31+
}
32+
--- stream_server_config
33+
proxy_pass backend;
34+
--- request
35+
GET /t
36+
--- response_body eval
37+
[
38+
qr/127.0.0.1/,
39+
]
40+
--- error_code: 200
41+
--- no_error_log
42+
[error]
43+
[warn]
44+
45+
46+
47+
=== TEST 2: balancer with bind_to_local_addr (addr)
48+
--- stream_config
49+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
50+
upstream backend {
51+
server 0.0.0.1:1234 down;
52+
balancer_by_lua_block {
53+
local b = require "ngx.balancer"
54+
assert(b.set_current_peer("127.0.0.1", 12345))
55+
assert(b.bind_to_local_addr("127.0.0.4"))
56+
}
57+
}
58+
server {
59+
listen 127.0.0.1:12345;
60+
content_by_lua_block {
61+
ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port)
62+
}
63+
}
64+
--- stream_server_config
65+
proxy_pass backend;
66+
--- request
67+
GET /t
68+
--- response_body eval
69+
[
70+
qr/127.0.0.4/,
71+
]
72+
--- error_code: 200
73+
--- no_error_log
74+
[error]
75+
[warn]
76+
77+
78+
79+
=== TEST 3: balancer with bind_to_local_addr (addr and port)
80+
--- stream_config
81+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
82+
upstream backend {
83+
server 0.0.0.1:1234 down;
84+
balancer_by_lua_block {
85+
local b = require "ngx.balancer"
86+
assert(b.set_current_peer("127.0.0.1", 12345))
87+
assert(b.bind_to_local_addr("127.0.0.8:23456"))
88+
}
89+
}
90+
server {
91+
listen 127.0.0.1:12345;
92+
content_by_lua_block {
93+
ngx.print(ngx.var.remote_addr, ":", ngx.var.remote_port)
94+
}
95+
}
96+
--- stream_server_config
97+
proxy_pass backend;
98+
--- request
99+
GET /t
100+
--- response_body eval
101+
[
102+
qr/127.0.0.8/,
103+
]
104+
--- error_code: 200
105+
--- no_error_log
106+
[error]
107+
[warn]

0 commit comments

Comments
 (0)