Skip to content

Commit c4d5ae7

Browse files
kingluozhuizhuhaomeng
authored andcommitted
feature: allowed some ngx APIs to be used in worker thread.
1 parent 421a8da commit c4d5ae7

File tree

4 files changed

+698
-41
lines changed

4 files changed

+698
-41
lines changed

README.markdown

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9124,7 +9124,35 @@ ngx.run_worker_thread
91249124

91259125
Wrap the [nginx worker thread](http://nginx.org/en/docs/dev/development_guide.html#threads) to execute lua function. The caller coroutine would yield until the function returns.
91269126

9127-
Note that no ngx_lua API can be used in the `function_name` function of the `module` module since it is invoked in a separate thread.
9127+
Only the following ngx_lua APIs could be used in `function_name` function of the `module` module:
9128+
9129+
* `ngx.encode_base64`
9130+
* `ngx.decode_base64`
9131+
9132+
* `ngx.hmac_sha1`
9133+
* `ngx.encode_args`
9134+
* `ngx.decode_args`
9135+
* `ngx.quote_sql_str`
9136+
9137+
* `ngx.re.match`
9138+
* `ngx.re.find`
9139+
* `ngx.re.gmatch`
9140+
* `ngx.re.sub`
9141+
* `ngx.re.gsub`
9142+
9143+
* `ngx.crc32_short`
9144+
* `ngx.crc32_long`
9145+
* `ngx.hmac_sha1`
9146+
* `ngx.md5_bin`
9147+
* `ngx.md5`
9148+
9149+
* `ngx.config.subsystem`
9150+
* `ngx.config.debug`
9151+
* `ngx.config.prefix`
9152+
* `ngx.config.nginx_version`
9153+
* `ngx.config.nginx_configure`
9154+
* `ngx.config.ngx_lua_version`
9155+
91289156

91299157
The first argument `threadpool` specifies the Nginx thread pool name defined by [thread_pool](https://nginx.org/en/docs/ngx_core_module.html#thread_pool).
91309158

doc/HttpLuaModule.wiki

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7820,7 +7820,35 @@ This API was first enabled in the <code>v0.6.0</code> release.
78207820
78217821
Wrap the [http://nginx.org/en/docs/dev/development_guide.html#threads nginx worker thread] to execute lua function. The caller coroutine would yield until the function returns.
78227822
7823-
Note that no ngx_lua API can be used in the `function_name` function of the `module` module since it is invoked in a separate thread.
7823+
Only the following ngx_lua APIs could be used in `function_name` function of the `module` module:
7824+
7825+
* `ngx.encode_base64`
7826+
* `ngx.decode_base64`
7827+
7828+
* `ngx.hmac_sha1`
7829+
* `ngx.encode_args`
7830+
* `ngx.decode_args`
7831+
* `ngx.quote_sql_str`
7832+
7833+
* `ngx.re.match`
7834+
* `ngx.re.find`
7835+
* `ngx.re.gmatch`
7836+
* `ngx.re.sub`
7837+
* `ngx.re.gsub`
7838+
7839+
* `ngx.crc32_short`
7840+
* `ngx.crc32_long`
7841+
* `ngx.hmac_sha1`
7842+
* `ngx.md5_bin`
7843+
* `ngx.md5`
7844+
7845+
* `ngx.config.subsystem`
7846+
* `ngx.config.debug`
7847+
* `ngx.config.prefix`
7848+
* `ngx.config.nginx_version`
7849+
* `ngx.config.nginx_configure`
7850+
* `ngx.config.ngx_lua_version`
7851+
78247852
78257853
The first argument `threadpool` specifies the Nginx thread pool name defined by [thread_pool](https://nginx.org/en/docs/ngx_core_module.html#thread_pool).
78267854

src/ngx_http_lua_worker_thread.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "ngx_http_lua_worker_thread.h"
1616
#include "ngx_http_lua_util.h"
17+
#include "ngx_http_lua_string.h"
18+
#include "ngx_http_lua_config.h"
1719

1820

1921
#if (NGX_THREADS)
@@ -127,8 +129,36 @@ ngx_http_lua_get_task_ctx(lua_State *L, ngx_http_request_t *r)
127129
/* pop path, cpath and "package" table from L */
128130
lua_pop(L, 3);
129131

130-
/* pop the "package" table */
131-
lua_pop(vm, 1);
132+
/* inject API from C */
133+
lua_newtable(L); /* ngx.* */
134+
ngx_http_lua_inject_string_api(vm);
135+
ngx_http_lua_inject_config_api(vm);
136+
lua_setglobal(vm, "ngx");
137+
138+
/* inject API via ffi */
139+
lua_getglobal(vm, "require");
140+
lua_pushstring(vm, "resty.core.regex");
141+
if (lua_pcall(vm, 1, 0, 0) != 0) {
142+
lua_close(vm);
143+
ngx_free(ctx);
144+
return NULL;
145+
}
146+
147+
lua_getglobal(vm, "require");
148+
lua_pushstring(vm, "resty.core.hash");
149+
if (lua_pcall(vm, 1, 0, 0) != 0) {
150+
lua_close(vm);
151+
ngx_free(ctx);
152+
return NULL;
153+
}
154+
155+
lua_getglobal(vm, "require");
156+
lua_pushstring(vm, "resty.core.base64");
157+
if (lua_pcall(vm, 1, 0, 0) != 0) {
158+
lua_close(vm);
159+
ngx_free(ctx);
160+
return NULL;
161+
}
132162

133163
} else {
134164
ctx = ctxpool->next;

0 commit comments

Comments
 (0)