Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: implemented the ngx.balancer Lua module to support dynamic nginx stream upstream balancers written in Lua. #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ install:
- git clone https://github.com/openresty/nginx-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 balancer https://github.com/doujiang24/stream-lua-nginx-module.git ../stream-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
Expand All @@ -68,7 +69,7 @@ script:
- export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH
- export TEST_NGINX_RESOLVER=8.8.4.4
- export NGX_BUILD_CC=$CC
- ngx-build $NGINX_VERSION --with-ipv6 --with-http_realip_module --with-http_ssl_module --with-cc-opt="-I$OPENSSL_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB" --add-module=../ndk-nginx-module --add-module=../echo-nginx-module --add-module=../headers-more-nginx-module --add-module=../lua-nginx-module --with-debug > build.log 2>&1 || (cat build.log && exit 1)
- ngx-build $NGINX_VERSION --with-ipv6 --with-http_realip_module --with-http_ssl_module --with-cc-opt="-I$OPENSSL_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB" --add-module=../ndk-nginx-module --add-module=../echo-nginx-module --add-module=../headers-more-nginx-module --add-module=../lua-nginx-module --add-module=../stream-lua-nginx-module --with-stream --with-stream_ssl_module --with-debug > build.log 2>&1 || (cat build.log && exit 1)
- nginx -V
- ldd `which nginx`|grep -E 'luajit|ssl|pcre'
- prove -r t
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ install: all
$(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/resty/core/
$(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/ngx/
$(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/ngx/ssl
$(INSTALL) -d $(DESTDIR)$(LUA_LIB_DIR)/ngx/balancer
$(INSTALL) lib/resty/*.lua $(DESTDIR)$(LUA_LIB_DIR)/resty/
$(INSTALL) lib/resty/core/*.lua $(DESTDIR)$(LUA_LIB_DIR)/resty/core/
$(INSTALL) lib/ngx/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/
$(INSTALL) lib/ngx/ssl/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/ssl/
$(INSTALL) lib/ngx/balancer/*.lua $(DESTDIR)$(LUA_LIB_DIR)/ngx/balancer/

test: all
PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t
Expand Down
5 changes: 5 additions & 0 deletions lib/ngx/balancer.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
-- Copyright (C) Yichun Zhang (agentzh)


if ngx.config.subsystem == "stream" then
return require "ngx.balancer.stream"
end


local ffi = require "ffi"
local base = require "resty.core.base"

Expand Down
79 changes: 79 additions & 0 deletions lib/ngx/balancer/stream.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
-- Copyright (C) Yichun Zhang (agentzh)


local ffi = require "ffi"
local base = require "resty.core.base"


local C = ffi.C
local ffi_str = ffi.string
local errmsg = base.get_errmsg_ptr()
local FFI_OK = base.FFI_OK
local FFI_ERROR = base.FFI_ERROR
local int_out = ffi.new("int[1]")
local getfenv = getfenv
local error = error
local type = type
local tonumber = tonumber


ffi.cdef[[
int ngx_stream_lua_ffi_balancer_set_current_peer(ngx_stream_session_t *s,
const unsigned char *addr, size_t addr_len, int port, char **err);

int ngx_stream_lua_ffi_balancer_set_more_tries(ngx_stream_session_t *s,
int count, char **err);

int ngx_stream_lua_ffi_balancer_get_last_failure(ngx_stream_session_t *s,
int *status, char **err);

int ngx_stream_lua_ffi_balancer_set_timeouts(ngx_stream_session_t *s,
long connect_timeout, long send_timeout,
long read_timeout, char **err);
]]


local _M = { version = base.version }


function _M.set_current_peer(addr, port)
local s = getfenv(0).__ngx_sess
if not s then
return error("no request found")
end

if not port then
port = 0
elseif type(port) ~= "number" then
port = tonumber(port)
end

local rc = C.ngx_stream_lua_ffi_balancer_set_current_peer(s, addr, #addr,
port, errmsg)
if rc == FFI_OK then
return true
end

return nil, ffi_str(errmsg[0])
end


function _M.set_more_tries(count)
local s = getfenv(0).__ngx_sess
if not s then
return error("no request found")
end

local rc = C.ngx_stream_lua_ffi_balancer_set_more_tries(s, count, errmsg)
if rc == FFI_OK then
if errmsg[0] == nil then
return true
end
return true, ffi_str(errmsg[0]) -- return the warning
end

return nil, ffi_str(errmsg[0])
end


return _M
8 changes: 8 additions & 0 deletions lib/resty/core/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ if not pcall(ffi.typeof, "ngx_http_request_t") then
end


if not pcall(ffi.typeof, "ngx_stream_session_t") then
ffi.cdef[[
struct ngx_stream_session_s;
typedef struct ngx_stream_session_s ngx_stream_session_t;
]]
end


if not pcall(ffi.typeof, "ngx_http_lua_ffi_str_t") then
ffi.cdef[[
typedef struct {
Expand Down
Loading