Skip to content

Commit 5da189e

Browse files
authored
Merge pull request #1302 from fesily/openresty-meta
meta: openresty add api run_worker_thread
2 parents db4ae9c + 86002c8 commit 5da189e

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

meta/3rd/OpenResty/library/ngx.lua

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,5 +4245,145 @@ ngx.resp = {}
42454245
---@return string|'"truncated"' error
42464246
function ngx.resp.get_headers(max_headers, raw) end
42474247

4248+
---@alias ngx.thread.arg boolean|number|integer|string|lightuserdata|table
4249+
4250+
---**syntax:** *ok, res1, res2, ... = ngx.run_worker_thread(threadpool, module_name, func_name, arg1, arg2, ...)*
4251+
---
4252+
---**context:** *rewrite_by_lua*, access_by_lua*, content_by_lua**
4253+
---
4254+
---**This API is still experimental and may change in the future without notice.**
4255+
---
4256+
---**This API is available only for Linux.**
4257+
---
4258+
---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.
4259+
---
4260+
---Only the following ngx_lua APIs could be used in `function_name` function of the `module` module:
4261+
---
4262+
---* `ngx.encode_base64`
4263+
---* `ngx.decode_base64`
4264+
---
4265+
---* `ngx.hmac_sha1`
4266+
---* `ngx.encode_args`
4267+
---* `ngx.decode_args`
4268+
---* `ngx.quote_sql_str`
4269+
---
4270+
---* `ngx.re.match`
4271+
---* `ngx.re.find`
4272+
---* `ngx.re.gmatch`
4273+
---* `ngx.re.sub`
4274+
---* `ngx.re.gsub`
4275+
---
4276+
---* `ngx.crc32_short`
4277+
---* `ngx.crc32_long`
4278+
---* `ngx.hmac_sha1`
4279+
---* `ngx.md5_bin`
4280+
---* `ngx.md5`
4281+
---
4282+
---* `ngx.config.subsystem`
4283+
---* `ngx.config.debug`
4284+
---* `ngx.config.prefix`
4285+
---* `ngx.config.nginx_version`
4286+
---* `ngx.config.nginx_configure`
4287+
---* `ngx.config.ngx_lua_version`
4288+
---
4289+
---
4290+
---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).
4291+
---
4292+
---The second argument `module_name` specifies the lua module name to execute in the worker thread, which would return a lua table. The module must be inside the package path, e.g.
4293+
---
4294+
---```nginx
4295+
---
4296+
---lua_package_path '/opt/openresty/?.lua;;';
4297+
---```
4298+
---
4299+
---The third argument `func_name` specifies the function field in the module table as the second argument.
4300+
---
4301+
---The type of `arg`s must be one of type below:
4302+
---
4303+
---* boolean
4304+
---* number
4305+
---* string
4306+
---* nil
4307+
---* table (the table may be recursive, and contains members of types above.)
4308+
---
4309+
---The `ok` is in boolean type, which indicate the C land error (failed to get thread from thread pool, pcall the module function failed, .etc). If `ok` is `false`, the `res1` is the error string.
4310+
---
4311+
---The return values (res1, ...) are returned by invocation of the module function. Normally, the `res1` should be in boolean type, so that the caller could inspect the error.
4312+
---
4313+
---This API is useful when you need to execute the below types of tasks:
4314+
---
4315+
---* CPU bound task, e.g. do md5 calculation
4316+
---* File I/O task
4317+
---* Call `os.execute()` or blocking C API via `ffi`
4318+
---* Call external Lua library not based on cosocket or nginx
4319+
---
4320+
---Example1: do md5 calculation.
4321+
---
4322+
---```nginx
4323+
---
4324+
---location /calc_md5 {
4325+
--- default_type 'text/plain';
4326+
---
4327+
--- content_by_lua_block {
4328+
--- local ok, md5_or_err = ngx.run_worker_thread("testpool", "md5", "md5")
4329+
--- ngx.say(ok, " : ", md5_or_err)
4330+
--- }
4331+
--- }
4332+
---```
4333+
---
4334+
---`md5.lua`
4335+
---
4336+
---```lua
4337+
---local function md5()
4338+
--- return ngx.md5("hello")
4339+
---end
4340+
---```
4341+
---
4342+
---Example2: write logs into the log file.
4343+
---
4344+
---```nginx
4345+
---
4346+
---location /write_log_file {
4347+
--- default_type 'text/plain';
4348+
---
4349+
--- content_by_lua_block {
4350+
--- local ok, err = ngx.run_worker_thread("testpool", "write_log_file", "log", ngx.var.arg_str)
4351+
--- if not ok then
4352+
--- ngx.say(ok, " : ", err)
4353+
--- return
4354+
--- end
4355+
--- ngx.say(ok)
4356+
--- }
4357+
--- }
4358+
---```
4359+
---
4360+
---`write_log_file.lua`
4361+
---
4362+
---```lua
4363+
---
4364+
--- local function log(str)
4365+
--- local file, err = io.open("/tmp/tmp.log", "a")
4366+
--- if not file then
4367+
--- return false, err
4368+
--- end
4369+
--- file:write(str)
4370+
--- file:flush()
4371+
--- file:close()
4372+
--- return true
4373+
--- end
4374+
--- return {log=log}
4375+
---```
4376+
---
4377+
---@param threadpool string
4378+
---@param module_name string
4379+
---@param func_name string
4380+
---@param arg1? ngx.thread.arg
4381+
---@param arg2? ngx.thread.arg
4382+
---@param ... ngx.thread.arg
4383+
---@return boolean ok
4384+
---@return ngx.thread.arg? result_or_error
4385+
---@return ...
4386+
function ngx.run_worker_thread(threadpool, module_name, func_name, arg1, arg2, ...)
4387+
end
42484388

42494389
return ngx

0 commit comments

Comments
 (0)