Skip to content

Commit 87f0a09

Browse files
committed
feat: implemented the lua_load_resty_core directive which loads resty.core by default.
1 parent 7ac6f3f commit 87f0a09

File tree

6 files changed

+166
-0
lines changed

6 files changed

+166
-0
lines changed

README.markdown

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ See Also
10041004
Directives
10051005
==========
10061006

1007+
* [lua_load_resty_core](#lua_load_resty_core)
10071008
* [lua_capture_error_log](#lua_capture_error_log)
10081009
* [lua_use_default_type](#lua_use_default_type)
10091010
* [lua_malloc_trim](#lua_malloc_trim)
@@ -1079,6 +1080,38 @@ how the result will be used. Below is a diagram showing the order in which direc
10791080

10801081
[Back to TOC](#table-of-contents)
10811082

1083+
lua_load_resty_core
1084+
-------------------
1085+
1086+
**syntax:** *lua_load_resty_core on|off*
1087+
1088+
**default:** *lua_load_resty_core on*
1089+
1090+
**context:** *http*
1091+
1092+
Controls whether the `resty.core` module (from
1093+
[lua-resty-core](https://github.com/openresty/lua-resty-core)) should be loaded
1094+
or not. When enabled, this directive is equivalent to executing the following
1095+
when the Lua VM is created:
1096+
1097+
```lua
1098+
1099+
require "resty.core"
1100+
```
1101+
1102+
Note that usage of the `resty.core` module is recommended, as its
1103+
FFI implementation is both faster, safer, and more complete than the Lua C API
1104+
of the ngx_lua module.
1105+
1106+
It must also be noted that the Lua C API of the ngx_lua module will eventually
1107+
be removed, and usage of the FFI-based API (i.e. the `resty.core`
1108+
module) will become mandatory. This directive only aims at providing a
1109+
temporary backwards-compatibility mode in case of edge-cases.
1110+
1111+
This directive was first introduced in the `v0.10.15` release.
1112+
1113+
[Back to TOC](#directives)
1114+
10821115
lua_capture_error_log
10831116
---------------------
10841117
**syntax:** *lua_capture_error_log size*

doc/HttpLuaModule.wiki

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,34 @@ how the result will be used. Below is a diagram showing the order in which direc
835835
836836
![Lua Nginx Modules Directives](https://cloud.githubusercontent.com/assets/2137369/15272097/77d1c09e-1a37-11e6-97ef-d9767035fc3e.png)
837837
838+
== lua_load_resty_core ==
839+
840+
'''syntax:''' ''lua_load_resty_core on|off''
841+
842+
'''default:''' ''lua_load_resty_core on''
843+
844+
'''context:''' ''http''
845+
846+
Controls whether the <code>resty.core</code> module (from
847+
[https://github.com/openresty/lua-resty-core lua-resty-core]) should be loaded
848+
or not. When enabled, this directive is equivalent to executing the following
849+
when the Lua VM is created:
850+
851+
<geshi lang="lua">
852+
require "resty.core"
853+
</geshi>
854+
855+
Note that usage of the <code>resty.core</code> module is recommended, as its
856+
FFI implementation is both faster, safer, and more complete than the Lua C API
857+
of the ngx_lua module.
858+
859+
It must also be noted that the Lua C API of the ngx_lua module will eventually
860+
be removed, and usage of the FFI-based API (i.e. the <code>resty.core</code>
861+
module) will become mandatory. This directive only aims at providing a
862+
temporary backwards-compatibility mode in case of edge-cases.
863+
864+
This directive was first introduced in the <code>v0.10.15</code> release.
865+
838866
== lua_capture_error_log ==
839867
'''syntax:''' ''lua_capture_error_log size''
840868

src/ngx_http_lua_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ struct ngx_http_lua_main_conf_s {
182182
ngx_cycle_t *cycle;
183183
ngx_pool_t *pool;
184184

185+
ngx_flag_t load_resty_core;
186+
185187
ngx_int_t max_pending_timers;
186188
ngx_int_t pending_timers;
187189

src/ngx_http_lua_module.c

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

7878
static ngx_command_t ngx_http_lua_cmds[] = {
7979

80+
{ ngx_string("lua_load_resty_core"),
81+
NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
82+
ngx_conf_set_flag_slot,
83+
NGX_HTTP_MAIN_CONF_OFFSET,
84+
offsetof(ngx_http_lua_main_conf_t, load_resty_core),
85+
NULL },
86+
8087
{ ngx_string("lua_max_running_timers"),
8188
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
8289
ngx_conf_set_num_slot,
@@ -877,6 +884,7 @@ ngx_http_lua_create_main_conf(ngx_conf_t *cf)
877884
*/
878885

879886
lmcf->pool = cf->pool;
887+
lmcf->load_resty_core = NGX_CONF_UNSET;
880888
lmcf->max_pending_timers = NGX_CONF_UNSET;
881889
lmcf->max_running_timers = NGX_CONF_UNSET;
882890
#if (NGX_PCRE)
@@ -910,6 +918,10 @@ ngx_http_lua_init_main_conf(ngx_conf_t *cf, void *conf)
910918
{
911919
ngx_http_lua_main_conf_t *lmcf = conf;
912920

921+
if (lmcf->load_resty_core == NGX_CONF_UNSET) {
922+
lmcf->load_resty_core = 1;
923+
}
924+
913925
#if (NGX_PCRE)
914926
if (lmcf->regex_cache_max_entries == NGX_CONF_UNSET) {
915927
lmcf->regex_cache_max_entries = 1024;

src/ngx_http_lua_util.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ static int ngx_http_lua_get_raw_phase_context(lua_State *L);
147147
#define LUA_PATH_SEP ";"
148148
#endif
149149

150+
151+
#if !defined(LUA_DEFAULT_PATH) && (NGX_DEBUG)
152+
#define LUA_DEFAULT_PATH "../lua-resty-core/lib/?.lua;" \
153+
"../lua-resty-lrucache/lib/?.lua"
154+
#endif
155+
156+
150157
#define AUX_MARK "\1"
151158

152159

@@ -3813,6 +3820,7 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
38133820
ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log,
38143821
ngx_pool_cleanup_t **pcln)
38153822
{
3823+
int rc;
38163824
lua_State *L;
38173825
ngx_uint_t i;
38183826
ngx_pool_cleanup_t *cln;
@@ -3880,6 +3888,21 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
38803888
lua_pop(L, 2);
38813889
}
38823890

3891+
if (lmcf->load_resty_core) {
3892+
lua_getglobal(L, "require");
3893+
lua_pushstring(L, "resty.core");
3894+
3895+
rc = lua_pcall(L, 1, 1, 0);
3896+
if (rc != 0) {
3897+
ngx_log_error(NGX_LOG_ERR, log, 0,
3898+
"lua_load_resty_core failed to load the resty.core "
3899+
"module from https://github.com/openresty/lua-resty"
3900+
"-core; ensure you are using an OpenResty release "
3901+
"from https://openresty.org/en/download.html "
3902+
"(rc: %i, reason: %s)", rc, lua_tostring(L, -1));
3903+
}
3904+
}
3905+
38833906
return L;
38843907
}
38853908

t/161-load-resty-core.t

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use Test::Nginx::Socket::Lua;
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->request) {
11+
$block->set_value("request", "GET /t");
12+
}
13+
14+
if (!defined $block->no_error_log) {
15+
$block->set_value("no_error_log", "[error]");
16+
}
17+
});
18+
19+
no_long_string();
20+
run_tests();
21+
22+
__DATA__
23+
24+
=== TEST 1: lua_load_resty_core is enabled by default
25+
--- config
26+
location = /t {
27+
content_by_lua_block {
28+
local loaded_resty_core = package.loaded["resty.core"]
29+
local resty_core = require "resty.core"
30+
31+
ngx.say("resty.core loaded: ", loaded_resty_core == resty_core)
32+
}
33+
}
34+
--- response_body
35+
resty.core loaded: true
36+
37+
38+
39+
=== TEST 2: lua_load_resty_core can be disabled
40+
--- http_config
41+
lua_load_resty_core off;
42+
--- config
43+
location = /t {
44+
content_by_lua_block {
45+
local loaded_resty_core = package.loaded["resty.core"]
46+
47+
ngx.say("resty.core loaded: ", loaded_resty_core ~= nil)
48+
}
49+
}
50+
--- response_body
51+
resty.core loaded: false
52+
53+
54+
55+
=== TEST 3: lua_load_resty_core is effective when using lua_shared_dict
56+
--- http_config
57+
lua_shared_dict dogs 128k;
58+
--- config
59+
location = /t {
60+
content_by_lua_block {
61+
local loaded_resty_core = package.loaded["resty.core"]
62+
local resty_core = require "resty.core"
63+
64+
ngx.say("resty.core loaded: ", loaded_resty_core == resty_core)
65+
}
66+
}
67+
--- response_body
68+
resty.core loaded: true

0 commit comments

Comments
 (0)