Skip to content

Commit 992e888

Browse files
committed
feature: ngx.balancer: added new API functions set_timeouts() for setting per-session connect/send/read timeouts for the current upstream request and subsequent retries. thanks Jianhao Dai for the patch in openresty#30.
1 parent 5ced690 commit 992e888

File tree

4 files changed

+464
-1
lines changed

4 files changed

+464
-1
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ install:
4747
- git clone https://github.com/openresty/lua-resty-lrucache.git
4848
- git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module
4949
- git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git
50+
- git clone https://github.com/openresty/mockeagain.git
5051

5152
script:
5253
- cd luajit2/
@@ -59,8 +60,11 @@ script:
5960
- ./config shared --prefix=$OPENSSL_PREFIX -DPURIFY > build.log 2>&1 || (cat build.log && exit 1)
6061
- make -j$JOBS > build.log 2>&1 || (cat build.log && exit 1)
6162
- sudo make PATH=$PATH install_sw > build.log 2>&1 || (cat build.log && exit 1)
62-
- cd ..
63+
- cd ../mockeagain/ && make CC=$CC -j$JOBS && cd ..
6364
- export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:$PATH
65+
- export LD_PRELOAD=$PWD/mockeagain/mockeagain.so
66+
- export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH
67+
- export TEST_NGINX_RESOLVER=8.8.4.4
6468
- export NGX_BUILD_CC=$CC
6569
- 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)
6670
- nginx -V

lib/ngx/balancer.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ int ngx_http_lua_ffi_balancer_set_more_tries(ngx_http_request_t *r,
2626

2727
int ngx_http_lua_ffi_balancer_get_last_failure(ngx_http_request_t *r,
2828
int *status, char **err);
29+
30+
int ngx_http_lua_ffi_balancer_set_timeouts(ngx_http_request_t *r,
31+
long connect_timeout, long send_timeout,
32+
long read_timeout, char **err);
2933
]]
3034

3135

@@ -101,4 +105,41 @@ function _M.get_last_failure()
101105
end
102106

103107

108+
function _M.set_timeouts(connect_timeout, send_timeout, read_timeout)
109+
local r = getfenv(0).__ngx_req
110+
if not r then
111+
return error("no request found")
112+
end
113+
114+
if not connect_timeout then
115+
connect_timeout = 0
116+
elseif type(connect_timeout) ~= "number" or connect_timeout <= 0 then
117+
return error("bad connect timeout")
118+
end
119+
120+
if not send_timeout then
121+
send_timeout = 0
122+
elseif type(send_timeout) ~= "number" or send_timeout <= 0 then
123+
return error("bad send timeout")
124+
end
125+
126+
if not read_timeout then
127+
read_timeout = 0
128+
elseif type(read_timeout) ~= "number" or read_timeout <= 0 then
129+
return error("bad read timeout")
130+
end
131+
132+
local rc =
133+
C.ngx_http_lua_ffi_balancer_set_timeouts(r, connect_timeout,
134+
send_timeout, read_timeout,
135+
errmsg)
136+
137+
if rc == FFI_OK then
138+
return true
139+
end
140+
141+
return false, ffi_str(errmsg[0])
142+
end
143+
144+
104145
return _M

lib/ngx/balancer.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Table of Contents
1414
* [set_current_peer](#set_current_peer)
1515
* [set_more_tries](#set_more_tries)
1616
* [get_last_failure](#get_last_failure)
17+
* [set_timeouts](#set_timeouts)
1718
* [Community](#community)
1819
* [English Mailing List](#english-mailing-list)
1920
* [Chinese Mailing List](#chinese-mailing-list)
@@ -156,6 +157,31 @@ method always returns a single `nil` value.
156157

157158
[Back to TOC](#table-of-contents)
158159

160+
set_timeouts
161+
------------
162+
**syntax:** `ok, err = balancer.set_timeouts(connect_timeout, send_timeout, read_timeout)`
163+
164+
**context:** *balancer_by_lua&#42;*
165+
166+
Sets the upstream timeout (connect, send and read) in milliseconds for the current and any
167+
subsequent backend requests (which might be a retry).
168+
169+
If you want to inherit the timeout value of the global `nginx.conf` configuration (like `proxy_connect_timeout`), then
170+
just specify the `nil` value for the corresponding argument (like the `connect_timeout` argument).
171+
172+
Zero and negative timeout values are not allowed.
173+
174+
Returns `true` when the operation is successul; returns `nil` and a string describing the error
175+
otherwise.
176+
177+
This only affects the current downstream request. It is not a global change.
178+
179+
For the best performance, you should use the [OpenResty](https://openresty.org/) bundle.
180+
181+
This function was first added in the `0.1.7` version of this library.
182+
183+
[Back to TOC](#table-of-contents)
184+
159185
Community
160186
=========
161187

0 commit comments

Comments
 (0)