lua randomseed per worker
This commit is contained in:
parent
66aecbd0b2
commit
4896b064ca
6 changed files with 51 additions and 3 deletions
|
@ -3,3 +3,8 @@ globals = {
|
||||||
'_TEST'
|
'_TEST'
|
||||||
}
|
}
|
||||||
exclude_files = {'./rootfs/etc/nginx/lua/test/**/*.lua'}
|
exclude_files = {'./rootfs/etc/nginx/lua/test/**/*.lua'}
|
||||||
|
files["rootfs/etc/nginx/lua/lua_ingress.lua"] = {
|
||||||
|
ignore = { "122" },
|
||||||
|
-- TODO(elvinefendi) figure out why this does not work
|
||||||
|
--read_globals = {"math.randomseed"},
|
||||||
|
}
|
||||||
|
|
|
@ -25,4 +25,4 @@ fi
|
||||||
|
|
||||||
hack/verify-all.sh
|
hack/verify-all.sh
|
||||||
|
|
||||||
luacheck -q rootfs/etc/nginx/lua/
|
luacheck --codes -q rootfs/etc/nginx/lua/
|
||||||
|
|
26
rootfs/etc/nginx/lua/lua_ingress.lua
Normal file
26
rootfs/etc/nginx/lua/lua_ingress.lua
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
local _M = {}
|
||||||
|
|
||||||
|
local seeds = {}
|
||||||
|
local original_randomseed = math.randomseed
|
||||||
|
math.randomseed = function(seed)
|
||||||
|
local pid = ngx.worker.pid()
|
||||||
|
|
||||||
|
if seeds[pid] then
|
||||||
|
ngx.log(ngx.WARN,
|
||||||
|
string.format("ignoring math.randomseed(%d) since PRNG is already seeded for worker %d", seed, pid))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
original_randomseed(seed)
|
||||||
|
seeds[pid] = seed
|
||||||
|
end
|
||||||
|
|
||||||
|
local function randomseed()
|
||||||
|
math.randomseed(ngx.time() + ngx.worker.pid())
|
||||||
|
end
|
||||||
|
|
||||||
|
function _M.init_worker()
|
||||||
|
randomseed()
|
||||||
|
end
|
||||||
|
|
||||||
|
return _M
|
9
rootfs/etc/nginx/lua/test/lua_ingress_test.lua
Normal file
9
rootfs/etc/nginx/lua/test/lua_ingress_test.lua
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
describe("lua_ingress", function()
|
||||||
|
it("patches math.randomseed to not be called more than once per worker", function()
|
||||||
|
local s = spy.on(ngx, "log")
|
||||||
|
|
||||||
|
math.randomseed(100)
|
||||||
|
assert.spy(s).was_called_with(ngx.WARN,
|
||||||
|
string.format("ignoring math.randomseed(%d) since PRNG is already seeded for worker %d", 100, ngx.worker.pid()))
|
||||||
|
end)
|
||||||
|
end)
|
|
@ -1,4 +1,5 @@
|
||||||
local ffi = require("ffi")
|
local ffi = require("ffi")
|
||||||
|
local lua_ingress = require("lua_ingress")
|
||||||
|
|
||||||
-- without this we get errors such as "attempt to redefine XXX"
|
-- without this we get errors such as "attempt to redefine XXX"
|
||||||
local old_cdef = ffi.cdef
|
local old_cdef = ffi.cdef
|
||||||
|
@ -32,7 +33,6 @@ end
|
||||||
ngx.log = function(...) end
|
ngx.log = function(...) end
|
||||||
ngx.print = function(...) end
|
ngx.print = function(...) end
|
||||||
|
|
||||||
-- TODO(elvinefendi) once this is implemented for production (should be!), share the same code
|
lua_ingress.init_worker()
|
||||||
math.randomseed(ngx.time() + ngx.worker.pid())
|
|
||||||
|
|
||||||
require "busted.runner"({ standalone = false })
|
require "busted.runner"({ standalone = false })
|
||||||
|
|
|
@ -63,6 +63,13 @@ http {
|
||||||
-- init modules
|
-- init modules
|
||||||
local ok, res
|
local ok, res
|
||||||
|
|
||||||
|
ok, res = pcall(require, "lua_ingress")
|
||||||
|
if not ok then
|
||||||
|
error("require failed: " .. tostring(res))
|
||||||
|
else
|
||||||
|
lua_ingress = res
|
||||||
|
end
|
||||||
|
|
||||||
ok, res = pcall(require, "configuration")
|
ok, res = pcall(require, "configuration")
|
||||||
if not ok then
|
if not ok then
|
||||||
error("require failed: " .. tostring(res))
|
error("require failed: " .. tostring(res))
|
||||||
|
@ -98,6 +105,7 @@ http {
|
||||||
}
|
}
|
||||||
|
|
||||||
init_worker_by_lua_block {
|
init_worker_by_lua_block {
|
||||||
|
lua_ingress.init_worker()
|
||||||
balancer.init_worker()
|
balancer.init_worker()
|
||||||
{{ if $all.EnableMetrics }}
|
{{ if $all.EnableMetrics }}
|
||||||
monitor.init_worker()
|
monitor.init_worker()
|
||||||
|
|
Loading…
Reference in a new issue