refactor some lua code
This commit is contained in:
parent
ced6c5bd96
commit
cb4755835e
11 changed files with 83 additions and 47 deletions
57
rootfs/etc/nginx/lua/util/defer.lua
Normal file
57
rootfs/etc/nginx/lua/util/defer.lua
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
local util = require("util")
|
||||
|
||||
local timer_started = false
|
||||
local queue = {}
|
||||
local MAX_QUEUE_SIZE = 10000
|
||||
|
||||
local _M = {}
|
||||
|
||||
local function flush_queue(premature)
|
||||
-- TODO Investigate if we should actually still flush the queue when we're
|
||||
-- shutting down.
|
||||
if premature then return end
|
||||
|
||||
local current_queue = queue
|
||||
queue = {}
|
||||
timer_started = false
|
||||
|
||||
for _,v in ipairs(current_queue) do
|
||||
v.func(unpack(v.args))
|
||||
end
|
||||
end
|
||||
|
||||
-- `to_timer_phase` will enqueue a function that will be executed in a timer
|
||||
-- context, at a later point in time. The purpose is that some APIs (such as
|
||||
-- sockets) are not available during some nginx request phases (such as the
|
||||
-- logging phase), but are available for use in timers. There are no ordering
|
||||
-- guarantees for when a function will be executed.
|
||||
function _M.to_timer_phase(func, ...)
|
||||
if ngx.get_phase() == "timer" then
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
|
||||
if #queue >= MAX_QUEUE_SIZE then
|
||||
ngx.log(ngx.ERR, "deferred timer queue full")
|
||||
return nil, "deferred timer queue full"
|
||||
end
|
||||
|
||||
table.insert(queue, { func = func, args = {...} })
|
||||
if not timer_started then
|
||||
local ok, err = ngx.timer.at(0, flush_queue)
|
||||
if ok then
|
||||
-- unfortunately this is to deal with tests - when running unit tests, we
|
||||
-- dont actually run the timer, we call the function inline
|
||||
if util.tablelength(queue) > 0 then
|
||||
timer_started = true
|
||||
end
|
||||
else
|
||||
local msg = "failed to create timer: " .. tostring(err)
|
||||
ngx.log(ngx.ERR, msg)
|
||||
return nil, msg
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
return _M
|
||||
70
rootfs/etc/nginx/lua/util/split.lua
Normal file
70
rootfs/etc/nginx/lua/util/split.lua
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
local _M = {}
|
||||
|
||||
-- splits strings into host and port
|
||||
local function parse_addr(addr)
|
||||
local _, _, host, port = addr:find("([^:]+):([^:]+)")
|
||||
if host and port then
|
||||
return {host=host, port=port}
|
||||
else
|
||||
return nil, "error in parsing upstream address!"
|
||||
end
|
||||
end
|
||||
|
||||
function _M.get_first_value(var)
|
||||
local t = _M.split_upstream_var(var) or {}
|
||||
if #t == 0 then return nil end
|
||||
return t[1]
|
||||
end
|
||||
|
||||
function _M.split_pair(pair, seperator)
|
||||
local i = pair:find(seperator)
|
||||
if i == nil then
|
||||
return pair, nil
|
||||
else
|
||||
local name = pair:sub(1, i - 1)
|
||||
local value = pair:sub(i + 1, -1)
|
||||
return name, value
|
||||
end
|
||||
end
|
||||
|
||||
-- http://nginx.org/en/docs/http/ngx_http_upstream_module.html#example
|
||||
-- CAVEAT: nginx is giving out : instead of , so the docs are wrong
|
||||
-- 127.0.0.1:26157 : 127.0.0.1:26157 , ngx.var.upstream_addr
|
||||
-- 200 : 200 , ngx.var.upstream_status
|
||||
-- 0.00 : 0.00, ngx.var.upstream_response_time
|
||||
function _M.split_upstream_var(var)
|
||||
if not var then
|
||||
return nil, nil
|
||||
end
|
||||
local t = {}
|
||||
for v in var:gmatch("[^%s|,]+") do
|
||||
if v ~= ":" then
|
||||
t[#t+1] = v
|
||||
end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
-- Splits an NGINX $upstream_addr and returns an array of tables with a `host` and `port` key-value pair.
|
||||
function _M.split_upstream_addr(addrs_str)
|
||||
if not addrs_str then
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
local addrs = _M.split_upstream_var(addrs_str)
|
||||
local host_and_ports = {}
|
||||
|
||||
for _, v in ipairs(addrs) do
|
||||
local a, err = parse_addr(v)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
host_and_ports[#host_and_ports+1] = a
|
||||
end
|
||||
if #host_and_ports == 0 then
|
||||
return nil, "no upstream addresses to parse!"
|
||||
end
|
||||
return host_and_ports
|
||||
end
|
||||
|
||||
return _M
|
||||
Loading…
Add table
Add a link
Reference in a new issue