Skip to content

Commit 60736e6

Browse files
committed
change: 'resty.core' is now mandatorily loaded, and the 'lua_load_resty_core' directive is deprecated.
1 parent a96e99a commit 60736e6

9 files changed

+171
-103
lines changed

README.markdown

+8-20
Original file line numberDiff line numberDiff line change
@@ -1076,26 +1076,14 @@ lua_load_resty_core
10761076

10771077
**context:** *http*
10781078

1079-
Controls whether the `resty.core` module (from
1080-
[lua-resty-core](https://github.com/openresty/lua-resty-core)) should be loaded
1081-
or not. When enabled, this directive is equivalent to executing the following
1082-
when the Lua VM is created:
1083-
1084-
```lua
1085-
1086-
require "resty.core"
1087-
```
1088-
1089-
Note that usage of the `resty.core` module is recommended, as its
1090-
FFI implementation is both faster, safer, and more complete than the Lua C API
1091-
of the ngx_lua module.
1092-
1093-
It must also be noted that the Lua C API of the ngx_lua module will eventually
1094-
be removed, and usage of the FFI-based API (i.e. the `resty.core`
1095-
module) will become mandatory. This directive only aims at providing a
1096-
temporary backwards-compatibility mode in case of edge-cases.
1097-
1098-
This directive was first introduced in the `v0.10.15` release.
1079+
This directive is deprecated since the `v0.10.16` release of this
1080+
module. The `resty.core` module from
1081+
[lua-resty-core](https://github.com/openresty/lua-resty-core) is now mandatorily
1082+
loaded during the Lua VM initialization. Specifying this directive will have no
1083+
effect.
1084+
1085+
This directive was first introduced in the `v0.10.15` release and
1086+
used to optionally load the `resty.core` module.
10991087

11001088
[Back to TOC](#directives)
11011089

doc/HttpLuaModule.wiki

+8-19
Original file line numberDiff line numberDiff line change
@@ -831,25 +831,14 @@ how the result will be used. Below is a diagram showing the order in which direc
831831
832832
'''context:''' ''http''
833833
834-
Controls whether the <code>resty.core</code> module (from
835-
[https://github.com/openresty/lua-resty-core lua-resty-core]) should be loaded
836-
or not. When enabled, this directive is equivalent to executing the following
837-
when the Lua VM is created:
838-
839-
<geshi lang="lua">
840-
require "resty.core"
841-
</geshi>
842-
843-
Note that usage of the <code>resty.core</code> module is recommended, as its
844-
FFI implementation is both faster, safer, and more complete than the Lua C API
845-
of the ngx_lua module.
846-
847-
It must also be noted that the Lua C API of the ngx_lua module will eventually
848-
be removed, and usage of the FFI-based API (i.e. the <code>resty.core</code>
849-
module) will become mandatory. This directive only aims at providing a
850-
temporary backwards-compatibility mode in case of edge-cases.
851-
852-
This directive was first introduced in the <code>v0.10.15</code> release.
834+
This directive is deprecated since the <code>v0.10.16</code> release of this
835+
module. The <code>resty.core</code> module from
836+
[https://github.com/openresty/lua-resty-core lua-resty-core] is now mandatorily
837+
loaded during the Lua VM initialization. Specifying this directive will have no
838+
effect.
839+
840+
This directive was first introduced in the <code>v0.10.15</code> release and
841+
used to optionally load the <code>resty.core</code> module.
853842
854843
== lua_capture_error_log ==
855844
'''syntax:''' ''lua_capture_error_log size''

src/ngx_http_lua_common.h

-2
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ struct ngx_http_lua_main_conf_s {
188188
ngx_cycle_t *cycle;
189189
ngx_pool_t *pool;
190190

191-
ngx_flag_t load_resty_core;
192-
193191
ngx_int_t max_pending_timers;
194192
ngx_int_t pending_timers;
195193

src/ngx_http_lua_directive.c

+11
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ ngx_http_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
179179
}
180180

181181

182+
char *
183+
ngx_http_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
184+
{
185+
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
186+
"lua_load_resty_core is deprecated (the lua-resty-core "
187+
"library is required since ngx_lua v0.10.16)");
188+
189+
return NGX_CONF_OK;
190+
}
191+
192+
182193
char *
183194
ngx_http_lua_package_cpath(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
184195
{

src/ngx_http_lua_directive.h

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ char *ngx_http_lua_init_worker_by_lua_block(ngx_conf_t *cf,
5050
char *ngx_http_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
5151
void *conf);
5252
char *ngx_http_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
53+
char *ngx_http_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd,
54+
void *conf);
5355

5456
#if defined(NDK) && NDK
5557

src/ngx_http_lua_module.c

+26-12
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ static ngx_command_t ngx_http_lua_cmds[] = {
7979

8080
{ ngx_string("lua_load_resty_core"),
8181
NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
82-
ngx_conf_set_flag_slot,
82+
ngx_http_lua_load_resty_core,
8383
NGX_HTTP_MAIN_CONF_OFFSET,
84-
offsetof(ngx_http_lua_main_conf_t, load_resty_core),
84+
0,
8585
NULL },
8686

8787
{ ngx_string("lua_max_running_timers"),
@@ -787,14 +787,33 @@ ngx_http_lua_init(ngx_conf_t *cf)
787787
ngx_http_lua_hash_literal("content-length");
788788
ngx_http_lua_location_hash = ngx_http_lua_hash_literal("location");
789789

790-
lmcf->lua = ngx_http_lua_init_vm(NULL, cf->cycle, cf->pool, lmcf,
791-
cf->log, NULL);
792-
if (lmcf->lua == NULL) {
793-
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
794-
"failed to initialize Lua VM");
790+
rc = ngx_http_lua_init_vm(&lmcf->lua, NULL, cf->cycle, cf->pool,
791+
lmcf, cf->log, NULL);
792+
if (rc != NGX_OK) {
793+
if (rc == NGX_DECLINED) {
794+
ngx_http_lua_assert(lmcf->lua != NULL);
795+
796+
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
797+
"failed to load the 'resty.core' module "
798+
"(https://github.com/openresty/lua-resty"
799+
"-core); ensure you are using an OpenResty "
800+
"release from https://openresty.org/en/"
801+
"download.html (reason: %s)",
802+
lua_tostring(lmcf->lua, -1));
803+
804+
} else {
805+
/* rc == NGX_ERROR */
806+
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
807+
"failed to initialize Lua VM");
808+
}
809+
795810
return NGX_ERROR;
796811
}
797812

813+
/* rc == NGX_OK */
814+
815+
ngx_http_lua_assert(lmcf->lua != NULL);
816+
798817
if (!lmcf->requires_shm && lmcf->init_handler) {
799818
saved_cycle = ngx_cycle;
800819
ngx_cycle = cf->cycle;
@@ -884,7 +903,6 @@ ngx_http_lua_create_main_conf(ngx_conf_t *cf)
884903
*/
885904

886905
lmcf->pool = cf->pool;
887-
lmcf->load_resty_core = NGX_CONF_UNSET;
888906
lmcf->max_pending_timers = NGX_CONF_UNSET;
889907
lmcf->max_running_timers = NGX_CONF_UNSET;
890908
#if (NGX_PCRE)
@@ -918,10 +936,6 @@ ngx_http_lua_init_main_conf(ngx_conf_t *cf, void *conf)
918936
{
919937
ngx_http_lua_main_conf_t *lmcf = conf;
920938

921-
if (lmcf->load_resty_core == NGX_CONF_UNSET) {
922-
lmcf->load_resty_core = 1;
923-
}
924-
925939
#if (NGX_PCRE)
926940
if (lmcf->regex_cache_max_entries == NGX_CONF_UNSET) {
927941
lmcf->regex_cache_max_entries = 1024;

src/ngx_http_lua_util.c

+17-21
Original file line numberDiff line numberDiff line change
@@ -3814,10 +3814,10 @@ ngx_http_lua_close_fake_connection(ngx_connection_t *c)
38143814
}
38153815

38163816

3817-
lua_State *
3818-
ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
3819-
ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log,
3820-
ngx_pool_cleanup_t **pcln)
3817+
ngx_int_t
3818+
ngx_http_lua_init_vm(lua_State **new_vm, lua_State *parent_vm,
3819+
ngx_cycle_t *cycle, ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf,
3820+
ngx_log_t *log, ngx_pool_cleanup_t **pcln)
38213821
{
38223822
int rc;
38233823
lua_State *L;
@@ -3828,13 +3828,13 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
38283828

38293829
cln = ngx_pool_cleanup_add(pool, 0);
38303830
if (cln == NULL) {
3831-
return NULL;
3831+
return NGX_ERROR;
38323832
}
38333833

38343834
/* create new Lua VM instance */
38353835
L = ngx_http_lua_new_state(parent_vm, cycle, lmcf, log);
38363836
if (L == NULL) {
3837-
return NULL;
3837+
return NGX_ERROR;
38383838
}
38393839

38403840
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0, "lua initialize the "
@@ -3845,7 +3845,7 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
38453845

38463846
state = ngx_alloc(sizeof(ngx_http_lua_vm_state_t), log);
38473847
if (state == NULL) {
3848-
return NULL;
3848+
return NGX_ERROR;
38493849
}
38503850
state->vm = L;
38513851
state->count = 1;
@@ -3878,7 +3878,8 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
38783878

38793879
for (i = 0; i < lmcf->preload_hooks->nelts; i++) {
38803880

3881-
ngx_http_lua_probe_register_preload_package(L, hook[i].package);
3881+
ngx_http_lua_probe_register_preload_package(L,
3882+
hook[i].package);
38823883

38833884
lua_pushcfunction(L, hook[i].loader);
38843885
lua_setfield(L, -2, (char *) hook[i].package);
@@ -3887,22 +3888,17 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
38873888
lua_pop(L, 2);
38883889
}
38893890

3890-
if (lmcf->load_resty_core) {
3891-
lua_getglobal(L, "require");
3892-
lua_pushstring(L, "resty.core");
3891+
*new_vm = L;
38933892

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

3905-
return L;
3901+
return NGX_OK;
39063902
}
39073903

39083904

src/ngx_http_lua_util.h

+29-9
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ ngx_http_lua_ffi_check_context(ngx_http_lua_ctx_t *ctx, unsigned flags,
136136
SSL_get_ex_data(ssl_conn, ngx_http_lua_ssl_ctx_index)
137137

138138

139-
lua_State *ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
140-
ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log,
141-
ngx_pool_cleanup_t **pcln);
139+
ngx_int_t ngx_http_lua_init_vm(lua_State **new_vm, lua_State *parent_vm,
140+
ngx_cycle_t *cycle, ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf,
141+
ngx_log_t *log, ngx_pool_cleanup_t **pcln);
142142

143143
lua_State *ngx_http_lua_new_thread(ngx_http_request_t *r, lua_State *l,
144144
int *ref);
@@ -289,7 +289,8 @@ ngx_http_lua_init_ctx(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx)
289289
static ngx_inline ngx_http_lua_ctx_t *
290290
ngx_http_lua_create_ctx(ngx_http_request_t *r)
291291
{
292-
lua_State *L;
292+
ngx_int_t rc;
293+
lua_State *L = NULL;
293294
ngx_http_lua_ctx_t *ctx;
294295
ngx_pool_cleanup_t *cln;
295296
ngx_http_lua_loc_conf_t *llcf;
@@ -311,14 +312,33 @@ ngx_http_lua_create_ctx(ngx_http_request_t *r)
311312
dd("lmcf: %p", lmcf);
312313
#endif
313314

314-
L = ngx_http_lua_init_vm(lmcf->lua, lmcf->cycle, r->pool, lmcf,
315-
r->connection->log, &cln);
316-
if (L == NULL) {
317-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
318-
"failed to initialize Lua VM");
315+
rc = ngx_http_lua_init_vm(&L, lmcf->lua, lmcf->cycle, r->pool, lmcf,
316+
r->connection->log, &cln);
317+
if (rc != NGX_OK) {
318+
if (rc == NGX_DECLINED) {
319+
ngx_http_lua_assert(L != NULL);
320+
321+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
322+
"failed to load the 'resty.core' module "
323+
"(https://github.com/openresty/lua-resty"
324+
"-core); ensure you are using an OpenResty "
325+
"release from https://openresty.org/en/"
326+
"download.html (reason: %s)",
327+
lua_tostring(L, -1));
328+
329+
} else {
330+
/* rc == NGX_ERROR */
331+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
332+
"failed to initialize Lua VM");
333+
}
334+
319335
return NULL;
320336
}
321337

338+
/* rc == NGX_OK */
339+
340+
ngx_http_lua_assert(L != NULL);
341+
322342
if (lmcf->init_handler) {
323343
if (lmcf->init_handler(r->connection->log, lmcf, L) != NGX_OK) {
324344
/* an error happened */

0 commit comments

Comments
 (0)