Skip to content

Commit

Permalink
fix(globalpatches) randomseed throws error when seeded in wrong context
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Sep 13, 2016
1 parent a6301dd commit 3144e77
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions kong/core/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ local seed
-- unique seed for Nginx workers).
-- luacheck: globals math
_G.math.randomseed = function()
if ngx.get_phase() ~= "init_worker" then
ngx.log(ngx.ERR, "math.randomseed() must be called in init_worker")
elseif not seed then
if not seed then
if ngx.get_phase() ~= "init_worker" then
error("math.randomseed() must be called in init_worker", 2)
end

seed = ngx.time() + ngx.worker.pid()
ngx.log(ngx.DEBUG, "random seed: ", seed, " for worker n", ngx.worker.id(),
" (pid: ", ngx.worker.pid(), ")")
randomseed(seed)
else
ngx.log(ngx.DEBUG, "attempt to seed random number generator, but ",
"already seeded")
"already seeded with ", seed)
end

return seed
end

0 comments on commit 3144e77

Please sign in to comment.