Skip to content

Commit 929f64e

Browse files
committed
feature: implemented the ngx.balancer Lua module to support dynamic nginx stream upstream balancers written in Lua.
The ngx.balancer modlue is expected to be used in stream_ngx_lua's balancer_by_lua* context.
1 parent bd7ab97 commit 929f64e

File tree

5 files changed

+453
-0
lines changed

5 files changed

+453
-0
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ install: all
1414
$(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/resty/core/
1515
$(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/ngx/
1616
$(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/ngx/ssl
17+
$(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/ngx/balancer
1718
$(INSTALL) lib/resty/*.lua $(DESTDIR)$(LUA_LIB_DIR)/resty/
1819
$(INSTALL) lib/resty/core/*.lua $(DESTDIR)$(LUA_LIB_DIR)/resty/core/
1920
$(INSTALL) lib/ngx/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/
2021
$(INSTALL) lib/ngx/ssl/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/ssl/
22+
$(INSTALL) lib/ngx/balancer/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/balancer/
2123

2224
test: all
2325
PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t

lib/ngx/balancer.lua

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
-- Copyright (C) Yichun Zhang (agentzh)
22

33

4+
if ngx.config.subsystem == "stream" then
5+
return require "ngx.balancer.stream"
6+
end
7+
8+
49
local ffi = require "ffi"
510
local base = require "resty.core.base"
611

lib/ngx/balancer/stream.lua

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
-- Copyright (C) Yichun Zhang (agentzh)
2+
3+
4+
local ffi = require "ffi"
5+
local base = require "resty.core.base"
6+
7+
8+
local C = ffi.C
9+
local ffi_str = ffi.string
10+
local errmsg = base.get_errmsg_ptr()
11+
local FFI_OK = base.FFI_OK
12+
local FFI_ERROR = base.FFI_ERROR
13+
local int_out = ffi.new("int[1]")
14+
local getfenv = getfenv
15+
local error = error
16+
local type = type
17+
local tonumber = tonumber
18+
19+
20+
ffi.cdef[[
21+
int ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *s,
22+
const unsigned char *addr, size_t addr_len, int port, char **err);
23+
24+
int ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *s,
25+
int count, char **err);
26+
27+
int ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s,
28+
int *status, char **err);
29+
30+
int ngx_stream_lua_ffi_balancer_set_timeouts(ngx_stream_session_t *s,
31+
long connect_timeout, long send_timeout,
32+
long read_timeout, char **err);
33+
]]
34+
35+
36+
local _M = { version = base.version }
37+
38+
39+
function _M.set_current_peer(addr, port)
40+
local s = getfenv(0).__ngx_sess
41+
if not s then
42+
return error("no request found")
43+
end
44+
45+
if not port then
46+
port = 0
47+
elseif type(port) ~= "number" then
48+
port = tonumber(port)
49+
end
50+
51+
local rc = C.ngx_stream_lua_ffi_balancer_set_current_peer(s, addr, #addr,
52+
port, errmsg)
53+
if rc == FFI_OK then
54+
return true
55+
end
56+
57+
return nil, ffi_str(errmsg[0])
58+
end
59+
60+
61+
function _M.set_more_tries(count)
62+
local s = getfenv(0).__ngx_sess
63+
if not s then
64+
return error("no request found")
65+
end
66+
67+
local rc = C.ngx_stream_lua_ffi_balancer_set_more_tries(s, count, errmsg)
68+
if rc == FFI_OK then
69+
if errmsg[0] == nil then
70+
return true
71+
end
72+
return true, ffi_str(errmsg[0]) -- return the warning
73+
end
74+
75+
return nil, ffi_str(errmsg[0])
76+
end
77+
78+
79+
return _M

lib/resty/core/base.lua

+8
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ if not pcall(ffi.typeof, "ngx_http_request_t") then
7979
end
8080

8181

82+
if not pcall(ffi.typeof, "ngx_stream_session_t") then
83+
ffi.cdef[[
84+
struct ngx_stream_session_s;
85+
typedef struct ngx_stream_session_s ngx_stream_session_t;
86+
]]
87+
end
88+
89+
8290
if not pcall(ffi.typeof, "ngx_http_lua_ffi_str_t") then
8391
ffi.cdef[[
8492
typedef struct {

0 commit comments

Comments
 (0)