Name
lua-resty-transaction-queue - Lua Transaction Queue for the ngx_lua
Table of Contents
- Name
- Status
- Description
- Synopsis
- Usage
- Setting Transation Queue
- Developing Request Callback
- Limitations
- TODO
- Author
- Copyright and License
- See Also
Status
Description
This Lua library is a Transaction Queue for the ngx_lua nginx module:
https://github.com/openresty/lua-nginx-module/#readme
This module is a priority message queue for request processing. When Nginx receives a request, the request can be assigned a priority (1 ~ 10), and it will be dispatched to the message queue with the priority correspondingly. Each Nginx worker has a timer, and the timer will fetch the request from the priority message queue regularly and call the user-defined callback function to process the request. Low priority requests are processed only after high priority requests have been processed.
Synopsis
lua_package_path "/path/to/lua-resty-transaction-queue/lib/?.lua;;";
init_worker_by_lua_file transaction_queue_init.lua;
server {
set_by_lua_file $queue_size transaction_queue_set.lua 10;
location /test {
set $queue_priority 9;
set $queue_handler "xxx";
content_by_lua_file transaction_queue_handle.lua;
}
}
Usage
Transaction Queue will register a timer during initialization for each Nginx worker. When Nginx receives a requests, the request will be dispatched to the transaction queue and respond user immediately. After that, the request will be processed asynchronously by the timer with the user-defined callback function. User should configure the callback function and the priority for each request, and user can also configure the transaction queue, such as queue size, etc.
Setting Transation Queue
Setting Package Path
syntax: lua_package_path "/path/to/lua-resty-tm/lib/?.lua;;";
context: http
Setting Init Method
syntax: init_worker_by_lua_file transaction_queue_init.lua;
context: http
phase: starting-worker
Setting Queue Size
syntax: set_by_lua_file $queue_size transaction_queue_set.lua 10;
context: server, server if, location, location if
phase: rewrite
Setting Request Priority
syntax: set $queue_priority 9;
context: location, location if
phase: rewrite
Setting Request Callback
syntax: set $queue_handler "xxx";
context: location, location if
phase: rewrite
Setting Request Handle
syntax: content_by_lua_file transaction_queue_handle.lua;
context: location, location if
phase: content
Developing Request Callback
The Callback Module need return a table, which mush include an execute method.
The definition of execute method is:
function _M.execute(task)
end
The type of parameter task is table, which include two kv pairs, as follows:
{
uri:ngx.var.uri,
args:ngx.req.get_uri_args(),
}
The Callback Module Example:
local _M = { VERSION = '0.0.1' }
local log = ngx.log
local NOTICE = ngx.NOTICE
function _M.execute(task)
log(NOTICE, "uri:", task.uri)
for k, v in pairs(task.args) do
log(NOTICE, "task args, k:", k, ", value:", v)
end
end
return _M
Limitations
TODO
Author
Guangshu Wang (Wesley) guangshu.wgs@antfin.com, Ant Financial Inc.
Copyright and License
This module is licensed under the BSD license.
Copyright (C) 2019, by Guangshu Wang (Wesley) guangshu.wgs@antfin.com, Ant Financial Inc.
All rights reserved.