Skip to content

Commit b8a4b2c

Browse files
author
wangyao
authored
feature: add argument 'connections' for process.enable_privileged_agent (#330)
1 parent 880317d commit b8a4b2c

File tree

4 files changed

+359
-7
lines changed

4 files changed

+359
-7
lines changed

lib/ngx/process.lua

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ local ngx_lua_ffi_master_pid
3737

3838
if subsystem == 'http' then
3939
ffi.cdef[[
40-
int ngx_http_lua_ffi_enable_privileged_agent(char **err);
40+
int ngx_http_lua_ffi_enable_privileged_agent(char **err,
41+
unsigned int connections);
4142
int ngx_http_lua_ffi_get_process_type(void);
4243
void ngx_http_lua_ffi_process_signal_graceful_exit(void);
4344
int ngx_http_lua_ffi_master_pid(void);
@@ -52,7 +53,8 @@ if subsystem == 'http' then
5253

5354
else
5455
ffi.cdef[[
55-
int ngx_stream_lua_ffi_enable_privileged_agent(char **err);
56+
int ngx_stream_lua_ffi_enable_privileged_agent(char **err,
57+
unsigned int connections);
5658
int ngx_stream_lua_ffi_get_process_type(void);
5759
void ngx_stream_lua_ffi_process_signal_graceful_exit(void);
5860
int ngx_stream_lua_ffi_master_pid(void);
@@ -73,12 +75,19 @@ function _M.type()
7375
end
7476

7577

76-
function _M.enable_privileged_agent()
78+
function _M.enable_privileged_agent(connections)
7779
if ngx_phase() ~= "init" then
7880
return nil, "API disabled in the current context"
7981
end
8082

81-
local rc = ngx_lua_ffi_enable_privileged_agent(errmsg)
83+
connections = connections or 512
84+
85+
if type(connections) ~= "number" or connections < 0 then
86+
return nil, "bad 'connections' argument: " ..
87+
"number expected and greater than 0"
88+
end
89+
90+
local rc = ngx_lua_ffi_enable_privileged_agent(errmsg, connections)
8291

8392
if rc == FFI_ERROR then
8493
return nil, ffi_str(errmsg[0])

lib/ngx/process.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ ngx.say("process type:", process.type()) -- RESPONSE: worker
115115

116116
enable_privileged_agent
117117
-----------------------
118-
**syntax:** *ok, err = process_module.enable_privileged_agent()*
118+
**syntax:** *ok, err = process_module.enable_privileged_agent(connections)*
119119

120120
**context:** *init_by_lua&#42;*
121121

@@ -129,6 +129,8 @@ The `init_worker_by_lua*` directive handler still runs in the privileged agent p
129129
use the [type](#type) function provided by this module to check if the current process is a privileged
130130
agent.
131131

132+
The argument connections sets the maximum number of simultaneous connections that can be opened by privileged agent process.
133+
132134
In case of failures, returns `nil` and a string describing the error.
133135

134136
[Back to TOC](#table-of-contents)
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
# vim:set ft= ts=4 sw=4 et fdm=marker:
2+
3+
use lib '.';
4+
use t::TestCore;
5+
6+
master_process_enabled(1);
7+
8+
repeat_each(1);
9+
10+
plan tests => repeat_each() * (blocks() * 5);
11+
12+
13+
$ENV{TEST_NGINX_LUA_PACKAGE_PATH} = $t::TestCore::lua_package_path;
14+
$ENV{TEST_NGINX_RANDOM_PORT} = Test::Nginx::Util::server_port();
15+
16+
run_tests();
17+
18+
__DATA__
19+
20+
=== TEST 1: specify connections to enable_privileged_agent
21+
--- http_config
22+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
23+
24+
init_by_lua_block {
25+
local process = require "ngx.process"
26+
local ok, err = process.enable_privileged_agent(10)
27+
if not ok then
28+
ngx.log(ngx.ERR, "enable_privileged_agent failed: ", err)
29+
end
30+
}
31+
32+
init_worker_by_lua_block {
33+
local base = require "resty.core.base"
34+
local typ = (require "ngx.process").type()
35+
36+
if typ == "privileged agent" then
37+
ngx.log(ngx.WARN, "process type: ", typ)
38+
ngx.timer.at(0, function()
39+
local tcpsock = ngx.socket.tcp()
40+
local ok, err = tcpsock:connect("127.0.0.1", $TEST_NGINX_RANDOM_PORT)
41+
42+
if ok then
43+
ngx.log(ngx.INFO, "connect ok ")
44+
else
45+
ngx.log(ngx.INFO, "connect failed " .. tostring(err))
46+
end
47+
end)
48+
end
49+
}
50+
--- config
51+
location = /t {
52+
content_by_lua_block {
53+
ngx.sleep(0.1)
54+
local typ = require "ngx.process".type()
55+
ngx.say("type: ", typ)
56+
}
57+
}
58+
--- request
59+
GET /t
60+
--- response_body
61+
type: worker
62+
--- error_log
63+
connect ok
64+
--- no_error_log
65+
connect failed
66+
enable_privileged_agent failed
67+
--- skip_nginx: 5: < 1.11.2
68+
--- wait: 0.2
69+
70+
71+
72+
=== TEST 2: connections exceed limit
73+
the real connections you can create is always less than you set.
74+
timer will take fake connections.
75+
--- http_config
76+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
77+
78+
init_by_lua_block {
79+
local process = require "ngx.process"
80+
local ok, err = process.enable_privileged_agent(10)
81+
if not ok then
82+
ngx.log(ngx.ERR, "enable_privileged_agent failed: ", err)
83+
end
84+
}
85+
86+
init_worker_by_lua_block {
87+
local base = require "resty.core.base"
88+
local typ = (require "ngx.process").type()
89+
90+
if typ == "privileged agent" then
91+
ngx.log(ngx.WARN, "process type: ", typ)
92+
93+
ngx.timer.at(0, function()
94+
95+
for i = 1, 10 do
96+
local tcpsock = ngx.socket.tcp()
97+
local ok, err = tcpsock:connect("127.0.0.1", $TEST_NGINX_RANDOM_PORT)
98+
99+
if ok then
100+
ngx.log(ngx.INFO, "connect ok ")
101+
else
102+
ngx.log(ngx.INFO, "connect failed " .. tostring(err))
103+
end
104+
end
105+
106+
end)
107+
end
108+
}
109+
--- config
110+
location = /t {
111+
content_by_lua_block {
112+
ngx.sleep(0.1)
113+
local typ = require "ngx.process".type()
114+
ngx.say("type: ", typ)
115+
}
116+
}
117+
--- request
118+
GET /t
119+
--- response_body
120+
type: worker
121+
--- error_log
122+
connect failed
123+
worker_connections are not enough
124+
--- no_error_log
125+
enable_privileged_agent failed
126+
--- skip_nginx: 5: < 1.11.2
127+
--- wait: 0.2
128+
129+
130+
131+
=== TEST 3: enable_privileged_agent with bad connections
132+
connections < 0
133+
--- http_config
134+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
135+
136+
init_by_lua_block {
137+
local process = require "ngx.process"
138+
local ok, err = process.enable_privileged_agent(-1)
139+
if not ok then
140+
ngx.log(ngx.ERR, "enable_privileged_agent failed: ", err)
141+
end
142+
}
143+
144+
init_worker_by_lua_block {
145+
local base = require "resty.core.base"
146+
local typ = (require "ngx.process").type()
147+
148+
if typ == "privileged agent" then
149+
ngx.log(ngx.WARN, "process type: ", typ)
150+
151+
ngx.timer.at(0, function()
152+
153+
for i = 1, 10 do
154+
local tcpsock = ngx.socket.tcp()
155+
local ok, err = tcpsock:connect("127.0.0.1", $TEST_NGINX_RANDOM_PORT)
156+
157+
if ok then
158+
ngx.log(ngx.INFO, "connect ok ")
159+
else
160+
ngx.log(ngx.INFO, "connect failed " .. tostring(err))
161+
end
162+
end
163+
164+
end)
165+
end
166+
}
167+
--- config
168+
location = /t {
169+
content_by_lua_block {
170+
ngx.sleep(0.1)
171+
local typ = require "ngx.process".type()
172+
ngx.say("type: ", typ)
173+
}
174+
}
175+
--- request
176+
GET /t
177+
--- response_body
178+
type: worker
179+
--- error_log
180+
enable_privileged_agent failed: bad 'connections' argument
181+
--- no_error_log
182+
connect ok
183+
connect failed
184+
--- skip_nginx: 5: < 1.11.2
185+
--- wait: 0.2
186+
187+
188+
189+
=== TEST 4: enable_privileged_agent with bad connections
190+
connections is not a number
191+
--- http_config
192+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
193+
194+
init_by_lua_block {
195+
local process = require "ngx.process"
196+
local ok, err = process.enable_privileged_agent("10")
197+
if not ok then
198+
ngx.log(ngx.ERR, "enable_privileged_agent failed: ", err)
199+
end
200+
}
201+
202+
init_worker_by_lua_block {
203+
local base = require "resty.core.base"
204+
local typ = (require "ngx.process").type()
205+
206+
if typ == "privileged agent" then
207+
ngx.log(ngx.WARN, "process type: ", typ)
208+
209+
ngx.timer.at(0, function()
210+
211+
for i = 1, 10 do
212+
local tcpsock = ngx.socket.tcp()
213+
local ok, err = tcpsock:connect("127.0.0.1", $TEST_NGINX_RANDOM_PORT)
214+
215+
if ok then
216+
ngx.log(ngx.INFO, "connect ok ")
217+
else
218+
ngx.log(ngx.INFO, "connect failed " .. tostring(err))
219+
end
220+
end
221+
222+
end)
223+
end
224+
}
225+
--- config
226+
location = /t {
227+
content_by_lua_block {
228+
ngx.sleep(0.1)
229+
local typ = require "ngx.process".type()
230+
ngx.say("type: ", typ)
231+
}
232+
}
233+
--- request
234+
GET /t
235+
--- response_body
236+
type: worker
237+
--- error_log
238+
enable_privileged_agent failed: bad 'connections' argument
239+
--- no_error_log
240+
connect ok
241+
connect failed
242+
--- skip_nginx: 5: < 1.11.2
243+
--- wait: 0.2
244+
245+
246+
247+
=== TEST 5: enable_privileged_agent with bad connections
248+
connections = 0
249+
--- http_config
250+
lua_package_path "$TEST_NGINX_LUA_PACKAGE_PATH";
251+
252+
init_by_lua_block {
253+
local process = require "ngx.process"
254+
local ok, err = process.enable_privileged_agent(0)
255+
if not ok then
256+
ngx.log(ngx.ERR, "enable_privileged_agent failed: ", err)
257+
end
258+
}
259+
260+
init_worker_by_lua_block {
261+
local base = require "resty.core.base"
262+
local typ = (require "ngx.process").type()
263+
264+
if typ == "privileged agent" then
265+
ngx.log(ngx.WARN, "process type: ", typ)
266+
267+
ngx.timer.at(0, function()
268+
269+
for i = 1, 10 do
270+
local tcpsock = ngx.socket.tcp()
271+
local ok, err = tcpsock:connect("127.0.0.1", $TEST_NGINX_RANDOM_PORT)
272+
273+
if ok then
274+
ngx.log(ngx.INFO, "connect ok ")
275+
else
276+
ngx.log(ngx.INFO, "connect failed " .. tostring(err))
277+
end
278+
end
279+
280+
end)
281+
end
282+
}
283+
--- config
284+
location = /t {
285+
content_by_lua_block {
286+
ngx.sleep(0.1)
287+
local typ = require "ngx.process".type()
288+
ngx.say("type: ", typ)
289+
}
290+
}
291+
--- request
292+
GET /t
293+
--- response_body
294+
type: worker
295+
--- grep_error_log eval
296+
qr/privileged agent process \d+ exited with fatal code 2 and cannot be respawned/
297+
--- grep_error_log_out eval
298+
qr/privileged agent process \d+ exited with fatal code 2 and cannot be respawned/
299+
--- no_error_log
300+
process type: privileged agent
301+
connect ok
302+
--- skip_nginx: 5: < 1.11.2
303+
--- wait: 0.2

0 commit comments

Comments
 (0)