Skip to content

Commit

Permalink
change: 'resty.core' is now mandatorily loaded, and the 'lua_load_res…
Browse files Browse the repository at this point in the history
…ty_core' directive is deprecated.
  • Loading branch information
thibaultcha committed Aug 5, 2019
1 parent a96e99a commit 60736e6
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 103 deletions.
28 changes: 8 additions & 20 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1076,26 +1076,14 @@ lua_load_resty_core

**context:** *http*

Controls whether the `resty.core` module (from
[lua-resty-core](https://github.com/openresty/lua-resty-core)) should be loaded
or not. When enabled, this directive is equivalent to executing the following
when the Lua VM is created:

```lua

require "resty.core"
```

Note that usage of the `resty.core` module is recommended, as its
FFI implementation is both faster, safer, and more complete than the Lua C API
of the ngx_lua module.

It must also be noted that the Lua C API of the ngx_lua module will eventually
be removed, and usage of the FFI-based API (i.e. the `resty.core`
module) will become mandatory. This directive only aims at providing a
temporary backwards-compatibility mode in case of edge-cases.

This directive was first introduced in the `v0.10.15` release.
This directive is deprecated since the `v0.10.16` release of this
module. The `resty.core` module from
[lua-resty-core](https://github.com/openresty/lua-resty-core) is now mandatorily
loaded during the Lua VM initialization. Specifying this directive will have no
effect.

This directive was first introduced in the `v0.10.15` release and
used to optionally load the `resty.core` module.

[Back to TOC](#directives)

Expand Down
27 changes: 8 additions & 19 deletions doc/HttpLuaModule.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -831,25 +831,14 @@ how the result will be used. Below is a diagram showing the order in which direc
'''context:''' ''http''
Controls whether the <code>resty.core</code> module (from
[https://github.com/openresty/lua-resty-core lua-resty-core]) should be loaded
or not. When enabled, this directive is equivalent to executing the following
when the Lua VM is created:
<geshi lang="lua">
require "resty.core"
</geshi>
Note that usage of the <code>resty.core</code> module is recommended, as its
FFI implementation is both faster, safer, and more complete than the Lua C API
of the ngx_lua module.
It must also be noted that the Lua C API of the ngx_lua module will eventually
be removed, and usage of the FFI-based API (i.e. the <code>resty.core</code>
module) will become mandatory. This directive only aims at providing a
temporary backwards-compatibility mode in case of edge-cases.
This directive was first introduced in the <code>v0.10.15</code> release.
This directive is deprecated since the <code>v0.10.16</code> release of this
module. The <code>resty.core</code> module from
[https://github.com/openresty/lua-resty-core lua-resty-core] is now mandatorily
loaded during the Lua VM initialization. Specifying this directive will have no
effect.
This directive was first introduced in the <code>v0.10.15</code> release and
used to optionally load the <code>resty.core</code> module.
== lua_capture_error_log ==
'''syntax:''' ''lua_capture_error_log size''
Expand Down
2 changes: 0 additions & 2 deletions src/ngx_http_lua_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ struct ngx_http_lua_main_conf_s {
ngx_cycle_t *cycle;
ngx_pool_t *pool;

ngx_flag_t load_resty_core;

ngx_int_t max_pending_timers;
ngx_int_t pending_timers;

Expand Down
11 changes: 11 additions & 0 deletions src/ngx_http_lua_directive.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ ngx_http_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}


char *
ngx_http_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"lua_load_resty_core is deprecated (the lua-resty-core "
"library is required since ngx_lua v0.10.16)");

return NGX_CONF_OK;
}


char *
ngx_http_lua_package_cpath(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_lua_directive.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ char *ngx_http_lua_init_worker_by_lua_block(ngx_conf_t *cf,
char *ngx_http_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
char *ngx_http_lua_code_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
char *ngx_http_lua_load_resty_core(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);

#if defined(NDK) && NDK

Expand Down
38 changes: 26 additions & 12 deletions src/ngx_http_lua_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ static ngx_command_t ngx_http_lua_cmds[] = {

{ ngx_string("lua_load_resty_core"),
NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
ngx_http_lua_load_resty_core,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_lua_main_conf_t, load_resty_core),
0,
NULL },

{ ngx_string("lua_max_running_timers"),
Expand Down Expand Up @@ -787,14 +787,33 @@ ngx_http_lua_init(ngx_conf_t *cf)
ngx_http_lua_hash_literal("content-length");
ngx_http_lua_location_hash = ngx_http_lua_hash_literal("location");

lmcf->lua = ngx_http_lua_init_vm(NULL, cf->cycle, cf->pool, lmcf,
cf->log, NULL);
if (lmcf->lua == NULL) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
"failed to initialize Lua VM");
rc = ngx_http_lua_init_vm(&lmcf->lua, NULL, cf->cycle, cf->pool,
lmcf, cf->log, NULL);
if (rc != NGX_OK) {
if (rc == NGX_DECLINED) {
ngx_http_lua_assert(lmcf->lua != NULL);

ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
"failed to load the 'resty.core' module "
"(https://github.com/openresty/lua-resty"
"-core); ensure you are using an OpenResty "
"release from https://openresty.org/en/"
"download.html (reason: %s)",
lua_tostring(lmcf->lua, -1));

} else {
/* rc == NGX_ERROR */
ngx_conf_log_error(NGX_LOG_ALERT, cf, 0,
"failed to initialize Lua VM");
}

return NGX_ERROR;
}

/* rc == NGX_OK */

ngx_http_lua_assert(lmcf->lua != NULL);

if (!lmcf->requires_shm && lmcf->init_handler) {
saved_cycle = ngx_cycle;
ngx_cycle = cf->cycle;
Expand Down Expand Up @@ -884,7 +903,6 @@ ngx_http_lua_create_main_conf(ngx_conf_t *cf)
*/

lmcf->pool = cf->pool;
lmcf->load_resty_core = NGX_CONF_UNSET;
lmcf->max_pending_timers = NGX_CONF_UNSET;
lmcf->max_running_timers = NGX_CONF_UNSET;
#if (NGX_PCRE)
Expand Down Expand Up @@ -918,10 +936,6 @@ ngx_http_lua_init_main_conf(ngx_conf_t *cf, void *conf)
{
ngx_http_lua_main_conf_t *lmcf = conf;

if (lmcf->load_resty_core == NGX_CONF_UNSET) {
lmcf->load_resty_core = 1;
}

#if (NGX_PCRE)
if (lmcf->regex_cache_max_entries == NGX_CONF_UNSET) {
lmcf->regex_cache_max_entries = 1024;
Expand Down
38 changes: 17 additions & 21 deletions src/ngx_http_lua_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -3814,10 +3814,10 @@ ngx_http_lua_close_fake_connection(ngx_connection_t *c)
}


lua_State *
ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log,
ngx_pool_cleanup_t **pcln)
ngx_int_t
ngx_http_lua_init_vm(lua_State **new_vm, lua_State *parent_vm,
ngx_cycle_t *cycle, ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf,
ngx_log_t *log, ngx_pool_cleanup_t **pcln)
{
int rc;
lua_State *L;
Expand All @@ -3828,13 +3828,13 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,

cln = ngx_pool_cleanup_add(pool, 0);
if (cln == NULL) {
return NULL;
return NGX_ERROR;
}

/* create new Lua VM instance */
L = ngx_http_lua_new_state(parent_vm, cycle, lmcf, log);
if (L == NULL) {
return NULL;
return NGX_ERROR;
}

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

state = ngx_alloc(sizeof(ngx_http_lua_vm_state_t), log);
if (state == NULL) {
return NULL;
return NGX_ERROR;
}
state->vm = L;
state->count = 1;
Expand Down Expand Up @@ -3878,7 +3878,8 @@ ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,

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

ngx_http_lua_probe_register_preload_package(L, hook[i].package);
ngx_http_lua_probe_register_preload_package(L,
hook[i].package);

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

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

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

rc = lua_pcall(L, 1, 1, 0);
if (rc != 0) {
return NGX_DECLINED;
}

return L;
return NGX_OK;
}


Expand Down
38 changes: 29 additions & 9 deletions src/ngx_http_lua_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ ngx_http_lua_ffi_check_context(ngx_http_lua_ctx_t *ctx, unsigned flags,
SSL_get_ex_data(ssl_conn, ngx_http_lua_ssl_ctx_index)


lua_State *ngx_http_lua_init_vm(lua_State *parent_vm, ngx_cycle_t *cycle,
ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf, ngx_log_t *log,
ngx_pool_cleanup_t **pcln);
ngx_int_t ngx_http_lua_init_vm(lua_State **new_vm, lua_State *parent_vm,
ngx_cycle_t *cycle, ngx_pool_t *pool, ngx_http_lua_main_conf_t *lmcf,
ngx_log_t *log, ngx_pool_cleanup_t **pcln);

lua_State *ngx_http_lua_new_thread(ngx_http_request_t *r, lua_State *l,
int *ref);
Expand Down Expand Up @@ -289,7 +289,8 @@ ngx_http_lua_init_ctx(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx)
static ngx_inline ngx_http_lua_ctx_t *
ngx_http_lua_create_ctx(ngx_http_request_t *r)
{
lua_State *L;
ngx_int_t rc;
lua_State *L = NULL;
ngx_http_lua_ctx_t *ctx;
ngx_pool_cleanup_t *cln;
ngx_http_lua_loc_conf_t *llcf;
Expand All @@ -311,14 +312,33 @@ ngx_http_lua_create_ctx(ngx_http_request_t *r)
dd("lmcf: %p", lmcf);
#endif

L = ngx_http_lua_init_vm(lmcf->lua, lmcf->cycle, r->pool, lmcf,
r->connection->log, &cln);
if (L == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to initialize Lua VM");
rc = ngx_http_lua_init_vm(&L, lmcf->lua, lmcf->cycle, r->pool, lmcf,
r->connection->log, &cln);
if (rc != NGX_OK) {
if (rc == NGX_DECLINED) {
ngx_http_lua_assert(L != NULL);

ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to load the 'resty.core' module "
"(https://github.com/openresty/lua-resty"
"-core); ensure you are using an OpenResty "
"release from https://openresty.org/en/"
"download.html (reason: %s)",
lua_tostring(L, -1));

} else {
/* rc == NGX_ERROR */
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to initialize Lua VM");
}

return NULL;
}

/* rc == NGX_OK */

ngx_http_lua_assert(L != NULL);

if (lmcf->init_handler) {
if (lmcf->init_handler(r->connection->log, lmcf, L) != NGX_OK) {
/* an error happened */
Expand Down

0 comments on commit 60736e6

Please sign in to comment.