Skip to content

Commit

Permalink
Merge pull request Kong#1613 from Mashape/fix/randomseed-patch
Browse files Browse the repository at this point in the history
fix(globalpatches) randomseed throws error when seeded in wrong context
  • Loading branch information
thibaultcha authored Sep 14, 2016
2 parents 934fa86 + 3144e77 commit e4f0fae
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 e4f0fae

Please sign in to comment.