Permalink
Browse files

hotfix(cli) add the shm mock hack

  • Loading branch information...
1 parent 1b32f3c commit cc5ce0db01ad5378cf8efce16a58aed2366e55e5 @thibaultcha thibaultcha committed Dec 22, 2016
Showing with 118 additions and 9 deletions.
  1. +2 −0 bin/busted
  2. +1 −3 bin/kong
  3. +115 −6 kong/core/globalpatches.lua
View
@@ -1,5 +1,7 @@
#!/usr/bin/env resty
+require("kong.core.globalpatches")({cli = true})
+
-- force LuaSocket usage to resolve `/etc/hosts` until
-- supported by resty-cli.
-- See https://github.com/Mashape/kong/issues/1523
View
@@ -1,8 +1,6 @@
#!/usr/bin/env resty
--- a flag to detect whether our modules (especially kong.core.globalpatches)
--- are running under resty-cli or in a real ngx_lua, runtime environment.
-ngx.RESTY_CLI = true
+require("kong.core.globalpatches")({cli = true})
-- force LuaSocket usage to resolve `/etc/hosts` until
-- supported by resty-cli.
@@ -19,7 +19,7 @@ return function(opts)
if not seed then
if not opts.cli and ngx.get_phase() ~= "init_worker" then
ngx.log(ngx.WARN, "math.randomseed() must be called in init_worker ",
- "context\n", debug.traceback('', 2)) -- nil [message] arg doesn't work with level
+ "context\n", debug.traceback('', 2)) -- nil [message] arg doesn't work with level
end
local bytes, err = util.get_rand_bytes(8)
@@ -44,14 +44,14 @@ return function(opts)
seed = tonumber(str)
else
ngx.log(ngx.ERR, "could not seed from OpenSSL RAND_bytes, seeding ",
- "PRNG with time and worker pid instead (this can ",
- "result to duplicated seeds): ", err)
+ "PRNG with time and worker pid instead (this can ",
+ "result to duplicated seeds): ", err)
seed = ngx.now()*1000 + ngx.worker.pid()
end
ngx.log(ngx.DEBUG, "random seed: ", seed, " for worker nb ",
- ngx.worker.id())
+ ngx.worker.id())
if not opts.cli then
local ok, err = ngx.shared.kong:safe_set("pid: " .. ngx.worker.pid(), seed)
@@ -63,11 +63,120 @@ return function(opts)
randomseed(seed)
else
ngx.log(ngx.DEBUG, "attempt to seed random number generator, but ",
- "already seeded with: ", seed, "\n",
- debug.traceback('', 2)) -- nil [message] arg doesn't work with level
+ "already seeded with: ", seed, "\n",
+ debug.traceback('', 2)) -- nil [message] arg doesn't work with level
end
return seed
end
end
+
+ if opts.cli then
+ -- ngx.shared.DICT proxy
+ -- https://github.com/bsm/fakengx/blob/master/fakengx.lua
+ -- with minor fixes and addtions such as exptime
+ --
+ -- See https://github.com/openresty/resty-cli/pull/12
+ -- for a definitive solution ot using shms in CLI
+ local SharedDict = {}
+ local function set(data, key, value)
+ data[key] = {
+ value = value,
+ info = {expired = false}
+ }
+ end
+ function SharedDict:new()
+ return setmetatable({data = {}}, {__index = self})
+ end
+ function SharedDict:get(key)
+ return self.data[key] and self.data[key].value, nil
+ end
+ function SharedDict:set(key, value)
+ set(self.data, key, value)
+ return true, nil, false
+ end
+ SharedDict.safe_set = SharedDict.set
+ function SharedDict:add(key, value, exptime)
+ if self.data[key] ~= nil then
+ return false, "exists", false
+ end
+
+ if exptime then
+ ngx.timer.at(exptime, function()
+ self.data[key] = nil
+ end)
+ end
+
+ set(self.data, key, value)
+ return true, nil, false
+ end
+ function SharedDict:replace(key, value)
+ if self.data[key] == nil then
+ return false, "not found", false
+ end
+ set(self.data, key, value)
+ return true, nil, false
+ end
+ function SharedDict:delete(key)
+ if self.data[key] ~= nil then
+ self.data[key] = nil
+ end
+ return true
+ end
+ function SharedDict:incr(key, value)
+ if not self.data[key] then
+ return nil, "not found"
+ elseif type(self.data[key].value) ~= "number" then
+ return nil, "not a number"
+ end
+ self.data[key].value = self.data[key].value + value
+ return self.data[key].value, nil
+ end
+ function SharedDict:flush_all()
+ for _, item in pairs(self.data) do
+ item.info.expired = true
+ end
+ end
+ function SharedDict:flush_expired(n)
+ local data = self.data
+ local flushed = 0
+
+ for key, item in pairs(self.data) do
+ if item.info.expired then
+ data[key] = nil
+ flushed = flushed + 1
+ if n and flushed == n then
+ break
+ end
+ end
+ end
+ self.data = data
+ return flushed
+ end
+ function SharedDict:get_keys(n)
+ n = n or 1024
+ local i = 0
+ local keys = {}
+ for k in pairs(self.data) do
+ keys[#keys+1] = k
+ i = i + 1
+ if n ~= 0 and i == n then
+ break
+ end
+ end
+ return keys
+ end
+
+ -- hack
+ _G.ngx.shared = setmetatable({}, {
+ __index = function(self, key)
+ local shm = rawget(self, key)
+ if not shm then
+ shm = SharedDict:new()
+ rawset(self, key, SharedDict:new())
+ end
+ return shm
+ end
+ })
+ end
end

0 comments on commit cc5ce0d

Please sign in to comment.