lua-nginx-module
lua-nginx-module copied to clipboard
Sharing changable data inside a worker and timers
I have read this part of the documentation https://github.com/openresty/lua-nginx-module#data-sharing-within-an-nginx-worker
Which is what I need, but I also require that a variable inside a module I define, is accessed within the worker (request handlers). But it is updated through a timer.every mechanism so something like this.
init_worker_by_lua_block : Create timer to update the variable. server_rewrite_by_lua_block: Use the variable. (I chose server_rewrite because I thought it is earlier stage than rewrite)
The variable is an integer, so I am asking if this usage is subject to race condition. It's an integer, so I could easily make it an atomic operation to set/get it in C (without locks). But that means writing an Nginx module. I was wondering if this was also possible with Lua.
Edit: Maybe a clever trick like employing https://www.lua.org/manual/2.1/subsection3_5_2.html (I have no experience with it), to retain/implement the atomic ability of setting and reading an integer in C.
Edit_2: Would making the variable not global (_M), but accessed through getter/setter functions in the module, but kept in the lua module as local var = 3; help? Like helping with concurrency and avoiding table look up.
The variable is an integer, so I am asking if this usage is subject to race condition.
there is no race condition within the worker when updating the intergeral variable.
The variable is an integer, so I am asking if this usage is subject to race condition.
there is no race condition within the worker when updating the intergeral variable.
Just to make it clear let me share an example
module.lua
local _M = {}
local value = 0
function _M.get()
return value
end
function _M.set(premature)
if premature then
return
end
value = value2
end
return _M
Nginx configuration part
init_by_lua_block {
require "module"
}
init_worker_by_lua_block {
local module = require "module"
local ok, err = ngx.timer.every(3, module.set)
if not ok then
ngx.log(ngx.ERR, "failed to create the timer: ", err)
return
end
}
access_by_lua_block {
local module = require "module"
if module.get() <= another_value
then
return ngx.exit(503)
end
}
As you can see, timer callback (namely a "light thread") sets, request handlers reads. So this behavior is free of race condition? I guess it is because of job scheduling with nginx+lua.
Also would avoiding _M.get() and exposing value directly by _M.value increase performance by skipping one function name lookup?
As you can see, timer callback (namely a "light thread") sets, request handlers reads. So this behavior is free of race condition? I guess it is because of job scheduling with nginx+lua.
Yes
Also would avoiding _M.get() and exposing value directly by _M.value increase performance by skipping one function name lookup?
Yes