Skip to content

Commit 8cad50e

Browse files
committed
synchronized with lua-nginx-module #60736e6 (deprecate 'lua_load_resty_core').
1 parent 9a9fd01 commit 8cad50e

File tree

6 files changed

+97
-48
lines changed

6 files changed

+97
-48
lines changed

src/subsys/ngx_subsys_lua_common.h.tt2

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ struct ngx_[% subsys %]_lua_main_conf_s {
233233
ngx_cycle_t *cycle;
234234
ngx_pool_t *pool;
235235

236-
ngx_flag_t load_resty_core;
237-
238236
ngx_int_t max_pending_timers;
239237
ngx_int_t pending_timers;
240238

src/subsys/ngx_subsys_lua_directive.c.tt2

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,23 @@ ngx_[% subsys %]_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
190190
}
191191

192192

193+
char *
194+
ngx_[% subsys %]_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd,
195+
void *conf)
196+
{
197+
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
198+
"lua_load_resty_core is deprecated (the lua-resty-core "
199+
"library is required since "
200+
[% IF http_subsys %]
201+
"ngx_lua v0.10.16)");
202+
[% ELSIF stream_subsys %]
203+
"ngx_stream_lua v0.0.8)");
204+
[% END %]
205+
206+
return NGX_CONF_OK;
207+
}
208+
209+
193210
char *
194211
ngx_[% subsys %]_lua_package_cpath(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
195212
{

src/subsys/ngx_subsys_lua_directive.h.tt2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ char *ngx_[% subsys %]_lua_init_worker_by_lua_block(ngx_conf_t *cf,
5555
char *ngx_[% subsys %]_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
5656
void *conf);
5757
char *ngx_[% subsys %]_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
58+
char *ngx_[% subsys %]_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd,
59+
void *conf);
5860

5961
[% IF http_subsys %]
6062
#if defined(NDK) && NDK

src/subsys/ngx_subsys_lua_module.c.tt2

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ static ngx_command_t ngx_[% subsys %]_lua_cmds[] = {
101101

102102
{ ngx_string("lua_load_resty_core"),
103103
NGX_[% SUBSYS %]_MAIN_CONF|NGX_CONF_FLAG,
104-
ngx_conf_set_flag_slot,
104+
ngx_[% subsys %]_lua_load_resty_core,
105105
NGX_[% SUBSYS %]_MAIN_CONF_OFFSET,
106-
offsetof(ngx_[% subsys %]_lua_main_conf_t, load_resty_core),
106+
0,
107107
NULL },
108108

109109
{ ngx_string("lua_max_running_timers"),
@@ -943,14 +943,33 @@ ngx_[% subsys %]_lua_init(ngx_conf_t *cf)
943943
ngx_[% subsys %]_lua_location_hash = ngx_[% subsys %]_lua_hash_literal("location");
944944
[% END %]
945945

946-
lmcf->lua = ngx_[% subsys %]_lua_init_vm(NULL, cf->cycle, cf->pool, lmcf,
947-
cf->log, NULL);
948-
if (lmcf->lua == NULL) {
949-
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
950-
"failed to initialize Lua VM");
951-
return NGX_ERROR;
946+
rc = ngx_[% subsys %]_lua_init_vm(&lmcf->lua, NULL, cf->cycle, cf->pool,
947+
lmcf, cf->log, NULL);
948+
if (rc != NGX_OK) {
949+
if (rc == NGX_DECLINED) {
950+
ngx_[% subsys %]_lua_assert(lmcf->lua != NULL);
951+
952+
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
953+
"failed to load the 'resty.core' module "
954+
"(https://github.com/openresty/lua-resty"
955+
"-core); ensure you are using an OpenResty "
956+
"release from https://openresty.org/en/"
957+
"download.html (reason: %s)",
958+
lua_tostring(lmcf->lua, -1));
959+
960+
} else {
961+
/* rc == NGX_ERROR */
962+
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
963+
"failed to initialize Lua VM");
964+
}
965+
966+
return NGX_ERROR;
952967
}
953968

969+
/* rc == NGX_OK */
970+
971+
ngx_[% subsys %]_lua_assert(lmcf->lua != NULL);
972+
954973
if (!lmcf->requires_shm && lmcf->init_handler) {
955974
saved_cycle = ngx_cycle;
956975
ngx_cycle = cf->cycle;
@@ -1044,7 +1063,6 @@ ngx_[% subsys %]_lua_create_main_conf(ngx_conf_t *cf)
10441063
*/
10451064

10461065
lmcf->pool = cf->pool;
1047-
lmcf->load_resty_core = NGX_CONF_UNSET;
10481066
lmcf->max_pending_timers = NGX_CONF_UNSET;
10491067
lmcf->max_running_timers = NGX_CONF_UNSET;
10501068
#if (NGX_PCRE)
@@ -1088,10 +1106,6 @@ ngx_[% subsys %]_lua_init_main_conf(ngx_conf_t *cf, void *conf)
10881106
{
10891107
ngx_[% subsys %]_lua_main_conf_t *lmcf = conf;
10901108

1091-
if (lmcf->load_resty_core == NGX_CONF_UNSET) {
1092-
lmcf->load_resty_core = 1;
1093-
}
1094-
10951109
#if (NGX_PCRE)
10961110
if (lmcf->regex_cache_max_entries == NGX_CONF_UNSET) {
10971111
lmcf->regex_cache_max_entries = 1024;

src/subsys/ngx_subsys_lua_util.c.tt2

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4058,9 +4058,10 @@ ngx_[% subsys %]_lua_close_fake_connection(ngx_connection_t *c)
40584058
}
40594059

40604060

4061-
lua_State *
4062-
ngx_[% subsys %]_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
4063-
ngx_pool_t *pool, ngx_[% subsys %]_lua_main_conf_t *lmcf, ngx_log_t *log,
4061+
ngx_int_t
4062+
ngx_[% subsys %]_lua_init_vm(lua_State **new_vm, lua_State *parent_vm,
4063+
ngx_cycle_t *cycle, ngx_pool_t *pool,
4064+
ngx_[% subsys %]_lua_main_conf_t *lmcf, ngx_log_t *log,
40644065
ngx_pool_cleanup_t **pcln)
40654066
{
40664067
int rc;
@@ -4073,13 +4074,13 @@ ngx_[% subsys %]_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
40734074

40744075
cln = ngx_pool_cleanup_add(pool, 0);
40754076
if (cln == NULL) {
4076-
return NULL;
4077+
return NGX_ERROR;
40774078
}
40784079

40794080
/* create new Lua VM instance */
40804081
L = ngx_[% subsys %]_lua_new_state(parent_vm, cycle, lmcf, log);
40814082
if (L == NULL) {
4082-
return NULL;
4083+
return NGX_ERROR;
40834084
}
40844085

40854086
ngx_log_debug1(NGX_LOG_DEBUG_[% SUBSYS %], log, 0, "lua initialize the "
@@ -4090,7 +4091,7 @@ ngx_[% subsys %]_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
40904091

40914092
state = ngx_alloc(sizeof(ngx_[% subsys %]_lua_vm_state_t), log);
40924093
if (state == NULL) {
4093-
return NULL;
4094+
return NGX_ERROR;
40944095
}
40954096
state->vm = L;
40964097
state->count = 1;
@@ -4123,7 +4124,8 @@ ngx_[% subsys %]_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
41234124

41244125
for (i = 0; i < lmcf->preload_hooks->nelts; i++) {
41254126

4126-
ngx_[% subsys %]_lua_probe_register_preload_package(L, hook[i].package);
4127+
ngx_[% subsys %]_lua_probe_register_preload_package(L,
4128+
hook[i].package);
41274129

41284130
lua_pushcfunction(L, hook[i].loader);
41294131
lua_setfield(L, -2, (char *) hook[i].package);
@@ -4132,22 +4134,17 @@ ngx_[% subsys %]_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
41324134
lua_pop(L, 2);
41334135
}
41344136

4135-
if (lmcf->load_resty_core) {
4136-
lua_getglobal(L, "require");
4137-
lua_pushstring(L, "resty.core");
4137+
*new_vm = L;
41384138

4139-
rc = lua_pcall(L, 1, 1, 0);
4140-
if (rc != 0) {
4141-
ngx_log_error(NGX_LOG_ERR, log, 0,
4142-
"lua_load_resty_core failed to load the resty.core "
4143-
"module from https://github.com/openresty/lua-resty"
4144-
"-core; ensure you are using an OpenResty release "
4145-
"from https://openresty.org/en/download.html "
4146-
"(rc: %i, reason: %s)", rc, lua_tostring(L, -1));
4147-
}
4148-
}
4139+
lua_getglobal(L, "require");
4140+
lua_pushstring(L, "resty.core");
41494141

4150-
return L;
4142+
rc = lua_pcall(L, 1, 1, 0);
4143+
if (rc != 0) {
4144+
return NGX_DECLINED;
4145+
}
4146+
4147+
return NGX_OK;
41514148
}
41524149

41534150

src/subsys/ngx_subsys_lua_util.h.tt2

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ ngx_[% subsys %]_lua_ffi_check_context(ngx_[% subsys %]_lua_ctx_t *ctx,
155155
SSL_get_ex_data(ssl_conn, ngx_[% subsys %]_lua_ssl_ctx_index)
156156

157157

158-
lua_State *ngx_[% subsys %]_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
159-
ngx_pool_t *pool, ngx_[% subsys %]_lua_main_conf_t *lmcf, ngx_log_t *log,
158+
ngx_int_t ngx_[% subsys %]_lua_init_vm(lua_State **new_vm, lua_State *parent_vm,
159+
ngx_cycle_t *cycle, ngx_pool_t *pool,
160+
ngx_[% subsys %]_lua_main_conf_t *lmcf, ngx_log_t *log,
160161
ngx_pool_cleanup_t **pcln);
161162

162163
lua_State *ngx_[% subsys %]_lua_new_thread([% req_type %] *r, lua_State *l,
@@ -315,7 +316,8 @@ ngx_http_lua_create_ctx(ngx_http_request_t *r)
315316
ngx_stream_lua_create_ctx(ngx_stream_session_t *r)
316317
[% END %]
317318
{
318-
lua_State *L;
319+
ngx_int_t rc;
320+
lua_State *L = NULL;
319321
ngx_[% subsys %]_lua_ctx_t *ctx;
320322
ngx_pool_cleanup_t *cln;
321323
ngx_[% subsys %]_lua_loc_conf_t *llcf;
@@ -367,8 +369,8 @@ ngx_stream_lua_create_ctx(ngx_stream_session_t *r)
367369
#endif
368370

369371
[% IF http_subsys %]
370-
L = ngx_http_lua_init_vm(lmcf->lua, lmcf->cycle, r->pool, lmcf,
371-
r->connection->log, &cln);
372+
rc = ngx_http_lua_init_vm(&L, lmcf->lua, lmcf->cycle, r->pool, lmcf,
373+
r->connection->log, &cln);
372374

373375
[% ELSIF stream_subsys %]
374376
/*
@@ -378,8 +380,8 @@ ngx_stream_lua_create_ctx(ngx_stream_session_t *r)
378380
* the correct semantics.
379381
*/
380382

381-
L = ngx_stream_lua_init_vm(lmcf->lua, lmcf->cycle, sreq->pool, lmcf,
382-
r->connection->log, &cln);
383+
rc = ngx_stream_lua_init_vm(&L, lmcf->lua, lmcf->cycle, sreq->pool,
384+
lmcf, r->connection->log, &cln);
383385

384386
while (cln->next != NULL) {
385387
cln = cln->next;
@@ -392,11 +394,30 @@ ngx_stream_lua_create_ctx(ngx_stream_session_t *r)
392394
cln->next = NULL;
393395
[% END %]
394396

395-
if (L == NULL) {
396-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
397-
"failed to initialize Lua VM");
398-
return NULL;
399-
}
397+
if (rc != NGX_OK) {
398+
if (rc == NGX_DECLINED) {
399+
ngx_[% subsys %]_lua_assert(L != NULL);
400+
401+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
402+
"failed to load the 'resty.core' module "
403+
"(https://github.com/openresty/lua-resty"
404+
"-core); ensure you are using an OpenResty "
405+
"release from https://openresty.org/en/"
406+
"download.html (reason: %s)",
407+
lua_tostring(L, -1));
408+
409+
} else {
410+
/* rc == NGX_ERROR */
411+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
412+
"failed to initialize Lua VM");
413+
}
414+
415+
return NULL;
416+
}
417+
418+
/* rc == NGX_OK */
419+
420+
ngx_[% subsys %]_lua_assert(L != NULL);
400421

401422
if (lmcf->init_handler) {
402423
if (lmcf->init_handler(r->connection->log, lmcf, L) != NGX_OK) {

0 commit comments

Comments
 (0)