Skip to content

Commit

Permalink
[wip] prototype of running __gc metamethod on policies
Browse files Browse the repository at this point in the history
  • Loading branch information
mikz committed May 1, 2018
1 parent 5501149 commit a0f9697
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gateway/src/apicast/configuration_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ function lazy.rewrite(configuration, host)
ngx.log(ngx.WARN, 'failed to acquire lock to lazy load: ', host, ' error: ', err)
end

collectgarbage()

return configuration
end

Expand Down
28 changes: 26 additions & 2 deletions gateway/src/apicast/policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local PHASES = {
local setmetatable = setmetatable
local ipairs = ipairs
local format = string.format

local empty = table.new(0,0 )
local noop = function() end

local function __tostring(policy)
Expand All @@ -30,6 +30,29 @@ local function __eq(policy, other)
return policy._NAME == other._NAME and policy._VERSION == other._VERSION
end

local inspect = require('inspect')

local function __gc(proxy)
local policy = getmetatable(proxy).__index -- proxy always points to the policy
local mt = getmetatable(policy) or empty-- in case someone changed the policy metatable

local gc = mt.__gc or noop

return gc(policy)
end

local function metatable(policy)
local proxy = newproxy(true)
local mt = getmetatable(proxy)

mt.__index = policy
mt.__tostring = __tostring
mt.__gc = __gc
policy.proxy = proxy

return mt
end

--- Initialize new policy
-- Returns a new policy that you can extend however you want.
-- @tparam string name Name of the new policy.
Expand All @@ -40,7 +63,8 @@ function _M.new(name, version)
_NAME = name,
_VERSION = version or '0.0',
}
local mt = { __index = policy, __tostring = __tostring, policy = policy }

local mt = metatable(policy)

function policy.new()
return setmetatable({}, mt)
Expand Down
5 changes: 5 additions & 0 deletions gateway/src/apicast/policy/echo/echo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ local new = _M.new

function _M.new(configuration)
local policy = new(configuration)
local mt = getmetatable(policy)

function mt.__gc(policy)
ngx.log(ngx.STDERR, 'running __gc of echo policy')
end

if configuration then
policy.status = tonumber(configuration.status)
Expand Down

0 comments on commit a0f9697

Please sign in to comment.