Skip to content

Commit 9457516

Browse files
committed
feat: implemented the lua_load_resty_core directive which loads resty.core by default. #2ad6242
1 parent 104d107 commit 9457516

File tree

4 files changed

+303
-0
lines changed

4 files changed

+303
-0
lines changed

src/ngx_stream_lua_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ struct ngx_stream_lua_main_conf_s {
178178
ngx_cycle_t *cycle;
179179
ngx_pool_t *pool;
180180

181+
ngx_flag_t load_resty_core;
182+
181183
ngx_int_t max_pending_timers;
182184
ngx_int_t pending_timers;
183185

src/ngx_stream_lua_module.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ static ngx_conf_bitmask_t ngx_stream_lua_ssl_protocols[] = {
8080

8181
static ngx_command_t ngx_stream_lua_cmds[] = {
8282

83+
{ ngx_string("lua_load_resty_core"),
84+
NGX_STREAM_MAIN_CONF|NGX_CONF_FLAG,
85+
ngx_conf_set_flag_slot,
86+
NGX_STREAM_MAIN_CONF_OFFSET,
87+
offsetof(ngx_stream_lua_main_conf_t, load_resty_core),
88+
NULL },
89+
8390
{ ngx_string("lua_max_running_timers"),
8491
NGX_STREAM_MAIN_CONF|NGX_CONF_TAKE1,
8592
ngx_conf_set_num_slot,
@@ -640,6 +647,7 @@ ngx_stream_lua_create_main_conf(ngx_conf_t *cf)
640647
*/
641648

642649
lmcf->pool = cf->pool;
650+
lmcf->load_resty_core = NGX_CONF_UNSET;
643651
lmcf->max_pending_timers = NGX_CONF_UNSET;
644652
lmcf->max_running_timers = NGX_CONF_UNSET;
645653
#if (NGX_PCRE)
@@ -671,6 +679,10 @@ ngx_stream_lua_init_main_conf(ngx_conf_t *cf, void *conf)
671679
{
672680
ngx_stream_lua_main_conf_t *lmcf = conf;
673681

682+
if (lmcf->load_resty_core == NGX_CONF_UNSET) {
683+
lmcf->load_resty_core = 1;
684+
}
685+
674686
#if (NGX_PCRE)
675687
if (lmcf->regex_cache_max_entries == NGX_CONF_UNSET) {
676688
lmcf->regex_cache_max_entries = 1024;

src/ngx_stream_lua_util.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ static int ngx_stream_lua_req_socket(lua_State *L);
129129
#define LUA_PATH_SEP ";"
130130
#endif
131131

132+
133+
#if !defined(LUA_DEFAULT_PATH) && (NGX_DEBUG)
134+
#define LUA_DEFAULT_PATH "../lua-resty-core/lib/?.lua;" \
135+
"../lua-resty-lrucache/lib/?.lua"
136+
#endif
137+
138+
132139
#define AUX_MARK "\1"
133140

134141

@@ -3264,6 +3271,7 @@ ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
32643271
ngx_pool_t *pool, ngx_stream_lua_main_conf_t *lmcf, ngx_log_t *log,
32653272
ngx_pool_cleanup_t **pcln)
32663273
{
3274+
int rc;
32673275
lua_State *L;
32683276
ngx_uint_t i;
32693277
ngx_pool_cleanup_t *cln;
@@ -3332,6 +3340,21 @@ ngx_stream_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
33323340
lua_pop(L, 2);
33333341
}
33343342

3343+
if (lmcf->load_resty_core) {
3344+
lua_getglobal(L, "require");
3345+
lua_pushstring(L, "resty.core");
3346+
3347+
rc = lua_pcall(L, 1, 1, 0);
3348+
if (rc != 0) {
3349+
ngx_log_error(NGX_LOG_ERR, log, 0,
3350+
"lua_load_resty_core failed to load the resty.core "
3351+
"module from https://github.com/openresty/lua-resty"
3352+
"-core; ensure you are using an OpenResty release "
3353+
"from https://openresty.org/en/download.html "
3354+
"(rc: %i, reason: %s)", rc, lua_tostring(L, -1));
3355+
}
3356+
}
3357+
33353358
return L;
33363359
}
33373360

t/161-load-resty-core.t

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
use Test::Nginx::Socket::Lua::Stream;
2+
3+
repeat_each(2);
4+
5+
plan tests => repeat_each() * (blocks() * 3);
6+
7+
add_block_preprocessor(sub {
8+
my $block = shift;
9+
10+
if (!defined $block->no_error_log) {
11+
$block->set_value("no_error_log", "[error]");
12+
}
13+
});
14+
15+
no_long_string();
16+
run_tests();
17+
18+
__DATA__
19+
20+
=== TEST 1: lua_load_resty_core is enabled by default
21+
--- stream_server_config
22+
content_by_lua_block {
23+
local loaded_resty_core = package.loaded["resty.core"]
24+
local resty_core = require "resty.core"
25+
26+
ngx.say("resty.core loaded: ", loaded_resty_core == resty_core)
27+
}
28+
--- stream_response
29+
resty.core loaded: true
30+
31+
32+
33+
=== TEST 2: lua_load_resty_core can be disabled
34+
--- stream_config
35+
lua_load_resty_core off;
36+
--- stream_server_config
37+
content_by_lua_block {
38+
local loaded_resty_core = package.loaded["resty.core"]
39+
40+
ngx.say("resty.core loaded: ", loaded_resty_core ~= nil)
41+
}
42+
--- stream_response
43+
resty.core loaded: false
44+
45+
46+
47+
=== TEST 3: lua_load_resty_core is effective when using lua_shared_dict
48+
--- stream_config
49+
lua_shared_dict dogs 128k;
50+
--- stream_server_config
51+
content_by_lua_block {
52+
local loaded_resty_core = package.loaded["resty.core"]
53+
local resty_core = require "resty.core"
54+
55+
ngx.say("resty.core loaded: ", loaded_resty_core == resty_core)
56+
}
57+
--- stream_response
58+
resty.core loaded: true
59+
60+
61+
62+
=== TEST 4: lua_load_resty_core 'on' in stream block and 'off' in http block
63+
--- http_config
64+
lua_load_resty_core off;
65+
--- config
66+
location = /t2 {
67+
content_by_lua_block {
68+
local loaded_resty_core = package.loaded["resty.core"]
69+
70+
local msg = "resty.core loaded in http: " .. tostring(loaded_resty_core ~= nil)
71+
ngx.header["Content-Length"] = #msg
72+
ngx.say(msg)
73+
}
74+
}
75+
--- stream_server_config
76+
content_by_lua_block {
77+
local loaded_resty_core = package.loaded["resty.core"]
78+
local resty_core = require "resty.core"
79+
80+
ngx.say("resty.core loaded in stream: ", loaded_resty_core == resty_core)
81+
82+
local sock = ngx.socket.tcp()
83+
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT)
84+
if not ok then
85+
ngx.say("failed to connect: ", err)
86+
return
87+
end
88+
89+
local req = "GET /t2 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
90+
91+
local bytes, err = sock:send(req)
92+
if not bytes then
93+
ngx.say("failed to send request: ", err)
94+
return
95+
end
96+
97+
local cl
98+
99+
while true do
100+
local line, err, part = sock:receive("*l")
101+
if err then
102+
ngx.say("failed to receive headers: ", err, " [", part, "]")
103+
break
104+
end
105+
106+
local k, v = line:match("([^:]*):%s*(.*)")
107+
if k == "Content-Length" then
108+
cl = v
109+
end
110+
111+
if line == "" then
112+
local body, err = sock:receive(cl)
113+
if err then
114+
ngx.say("failed to receive body: ", err)
115+
break
116+
end
117+
118+
ngx.say(body)
119+
break
120+
end
121+
end
122+
123+
ok, err = sock:close()
124+
}
125+
--- stream_response
126+
resty.core loaded in stream: true
127+
resty.core loaded in http: false
128+
129+
130+
131+
=== TEST 5: lua_load_resty_core 'off' in stream block and 'on' in http block
132+
--- config
133+
location = /t2 {
134+
content_by_lua_block {
135+
local loaded_resty_core = package.loaded["resty.core"]
136+
local resty_core = require "resty.core"
137+
138+
local msg = "resty.core loaded in http: " .. tostring(loaded_resty_core == resty_core)
139+
ngx.header["Content-Length"] = #msg
140+
ngx.say(msg)
141+
}
142+
}
143+
--- stream_config
144+
lua_load_resty_core off;
145+
--- stream_server_config
146+
content_by_lua_block {
147+
local loaded_resty_core = package.loaded["resty.core"]
148+
149+
ngx.say("resty.core loaded in stream: ", loaded_resty_core ~= nil)
150+
151+
local sock = ngx.socket.tcp()
152+
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT)
153+
if not ok then
154+
ngx.say("failed to connect: ", err)
155+
return
156+
end
157+
158+
local req = "GET /t2 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
159+
160+
local bytes, err = sock:send(req)
161+
if not bytes then
162+
ngx.say("failed to send request: ", err)
163+
return
164+
end
165+
166+
local cl
167+
168+
while true do
169+
local line, err, part = sock:receive("*l")
170+
if err then
171+
ngx.say("failed to receive headers: ", err, " [", part, "]")
172+
break
173+
end
174+
175+
local k, v = line:match("([^:]*):%s*(.*)")
176+
if k == "Content-Length" then
177+
cl = v
178+
end
179+
180+
if line == "" then
181+
local body, err = sock:receive(cl)
182+
if err then
183+
ngx.say("failed to receive body: ", err)
184+
break
185+
end
186+
187+
ngx.say(body)
188+
break
189+
end
190+
end
191+
192+
ok, err = sock:close()
193+
}
194+
--- stream_response
195+
resty.core loaded in stream: false
196+
resty.core loaded in http: true
197+
198+
199+
200+
=== TEST 6: lua_load_resty_core 'off' in stream block and 'off' in http block
201+
--- http_config
202+
lua_load_resty_core off;
203+
--- config
204+
location = /t2 {
205+
content_by_lua_block {
206+
local loaded_resty_core = package.loaded["resty.core"]
207+
208+
local msg = "resty.core loaded in http: " .. tostring(loaded_resty_core ~= nil)
209+
ngx.header["Content-Length"] = #msg
210+
ngx.say(msg)
211+
}
212+
}
213+
--- stream_config
214+
lua_load_resty_core off;
215+
--- stream_server_config
216+
content_by_lua_block {
217+
local loaded_resty_core = package.loaded["resty.core"]
218+
219+
ngx.say("resty.core loaded in stream: ", loaded_resty_core ~= nil)
220+
221+
local sock = ngx.socket.tcp()
222+
local ok, err = sock:connect("127.0.0.1", $TEST_NGINX_SERVER_PORT)
223+
if not ok then
224+
ngx.say("failed to connect: ", err)
225+
return
226+
end
227+
228+
local req = "GET /t2 HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
229+
230+
local bytes, err = sock:send(req)
231+
if not bytes then
232+
ngx.say("failed to send request: ", err)
233+
return
234+
end
235+
236+
local cl
237+
238+
while true do
239+
local line, err, part = sock:receive("*l")
240+
if err then
241+
ngx.say("failed to receive headers: ", err, " [", part, "]")
242+
break
243+
end
244+
245+
local k, v = line:match("([^:]*):%s*(.*)")
246+
if k == "Content-Length" then
247+
cl = v
248+
end
249+
250+
if line == "" then
251+
local body, err = sock:receive(cl)
252+
if err then
253+
ngx.say("failed to receive body: ", err)
254+
break
255+
end
256+
257+
ngx.say(body)
258+
break
259+
end
260+
end
261+
262+
ok, err = sock:close()
263+
}
264+
--- stream_response
265+
resty.core loaded in stream: false
266+
resty.core loaded in http: false

0 commit comments

Comments
 (0)