Skip to content

Commit ebc62e5

Browse files
committed
[executor] call .init and .init_worker on policy modules
Because it has to be executed for policies without configuration, it needs to be executed on the module level, not instance.
1 parent 9d13e7d commit ebc62e5

File tree

6 files changed

+84
-30
lines changed

6 files changed

+84
-30
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3131
- Use forked `resty.limit.count` that uses increments instead of decrements [PR #758](https://github.com/3scale/apicast/pull/758)
3232
- Rate Limit policy to take into account changes in the config [PR #703](https://github.com/3scale/apicast/pull/703)
3333
- The regular expression for mapping rules has been changed, so that special characters are accepted in the wildcard values for path [PR #717](https://github.com/3scale/apicast/pull/714)
34+
- Call `init` and `init_worker` on all available policies regardless they are used or not [PR #770](https://github.com/3scale/apicast/pull/770)
35+
- Cache loaded policies. Loading one policy several times will use the same instance [PR #770](https://github.com/3scale/apicast/pull/770)
36+
- Load all policies into cache when starting APIcast master process. [PR #770](https://github.com/3scale/apicast/pull/770)
37+
- `init` and `init_worker` phases are executed on the policy module, not the instance of a policy with a configuration [PR #770](https://github.com/3scale/apicast/pull/770)
3438

3539
### Fixed
3640

examples/custom-module/blacklist.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ end
3535

3636
function _M.init()
3737
iputils.enable_lrucache()
38-
apicast:init()
38+
apicast.init()
3939
end
4040

4141
local balancer_with_blacklist = resty_balancer.new(function(peers)

gateway/src/apicast/executor.lua

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local PolicyChain = require('apicast.policy_chain')
1010
local policy = require('apicast.policy')
1111
local linked_list = require('apicast.linked_list')
1212
local prometheus = require('apicast.prometheus')
13-
local policy_loader = require('apicast.policy_loader')
13+
1414

1515
local setmetatable = setmetatable
1616
local ipairs = ipairs
@@ -19,8 +19,6 @@ local _M = { }
1919

2020
local mt = { __index = _M }
2121

22-
local policy_modules = policy_loader:get_all()
23-
2422
-- forward all policy methods to the policy chain
2523
for _,phase in policy.phases() do
2624
_M[phase] = function(self, ...)
@@ -63,24 +61,41 @@ function _M:context(phase)
6361
return shared_build_context(self)
6462
end
6563

66-
local init = _M.init
67-
function _M:init()
68-
init(self)
64+
do
65+
local policy_loader = require('apicast.policy_loader')
66+
local policies
67+
68+
local init = _M.init
69+
function _M:init()
70+
local executed = {}
71+
72+
for _,policy in init(self) do
73+
executed[policy.init] = true
74+
end
75+
76+
policies = policy_loader:get_all()
6977

70-
for _, policy_mod in ipairs(policy_modules) do
71-
if policy_mod.init then
72-
policy_mod.init()
73-
end
78+
for _, policy in ipairs(policies) do
79+
if not executed[policy.init] then
80+
policy.init()
81+
executed[policy.init] = true
82+
end
83+
end
7484
end
75-
end
7685

77-
local init_worker = _M.init_worker
78-
function _M:init_worker()
79-
init_worker(self)
86+
local init_worker = _M.init_worker
87+
function _M:init_worker()
88+
local executed = {}
89+
90+
for _,policy in init_worker(self) do
91+
executed[policy.init_worker] = true
92+
end
8093

81-
for _, policy_mod in ipairs(policy_modules) do
82-
if policy_mod.init_worker then
83-
policy_mod.init_worker()
94+
for _, policy in ipairs(policies or policy_loader:get_all()) do
95+
if not executed[policy.init_worker] then
96+
policy.init_worker()
97+
executed[policy.init_worker] = true
98+
end
8499
end
85100
end
86101
end

gateway/src/apicast/policy/load_configuration/load_configuration.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ local configuration_store = require('apicast.configuration_store')
66

77
local new = _M.new
88

9+
_M.configuration = configuration_store.new()
10+
911
function _M.new(...)
1012
local policy = new(...)
11-
policy.configuration = configuration_store.new()
13+
policy.configuration = _M.configuration
1214
return policy
1315
end
1416

@@ -18,12 +20,12 @@ function _M:export()
1820
}
1921
end
2022

21-
function _M:init()
22-
configuration_loader.init(self.configuration)
23+
function _M.init()
24+
configuration_loader.init(_M.configuration)
2325
end
2426

25-
function _M:init_worker()
26-
configuration_loader.init_worker(self.configuration)
27+
function _M.init_worker()
28+
configuration_loader.init_worker(_M.configuration)
2729
end
2830

2931
function _M:rewrite(context)

gateway/src/apicast/policy_chain.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local setmetatable = setmetatable
99
local error = error
1010
local rawset = rawset
1111
local type = type
12+
local ipairs = ipairs
1213
local require = require
1314
local insert = table.insert
1415
local sub = string.sub
@@ -153,6 +154,8 @@ local function call_chain(phase_name)
153154
ngx.log(ngx.DEBUG, 'policy chain execute phase: ', phase_name, ', policy: ', self[i]._NAME, ', i: ', i)
154155
self[i][phase_name](self[i], ...)
155156
end
157+
158+
return ipairs(self)
156159
end
157160
end
158161

spec/executor_spec.lua

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local executor = require 'apicast.executor'
1+
local Executor = require 'apicast.executor'
22
local PolicyChain = require 'apicast.policy_chain'
33
local Policy = require 'apicast.policy'
44

@@ -9,7 +9,7 @@ describe('executor', function()
99

1010
it('forwards all the policy methods to the policy chain', function()
1111
local chain = PolicyChain.default()
12-
local exec = executor.new(chain)
12+
local exec = Executor.new(chain)
1313
-- Stub all the nginx phases methods for each of the policies
1414
for _, phase in Policy.phases() do
1515
for _, policy in ipairs(chain) do
@@ -22,14 +22,44 @@ describe('executor', function()
2222
for _, phase in Policy.phases() do
2323
exec[phase](exec)
2424
for _, policy in ipairs(chain) do
25-
assert.stub(policy[phase]).was_called()
25+
assert.stub(policy[phase]).was_called(1)
2626
end
2727
end
2828
end)
2929

30+
31+
context('policy is in the chain', function()
32+
33+
local executor
34+
local policy
35+
36+
before_each(function()
37+
local chain = PolicyChain.build({'apicast.policy.apicast'})
38+
policy = getmetatable(chain[1]).__index
39+
executor = Executor.new(chain)
40+
end)
41+
42+
it('calls .init just once', function()
43+
local init = stub(policy, 'init')
44+
45+
executor:init()
46+
47+
assert.stub(init).was_called(1)
48+
end)
49+
50+
it('calls .init_worker just once', function()
51+
local init = stub(policy, 'init_worker')
52+
53+
executor:init_worker()
54+
55+
assert.stub(init).was_called(1)
56+
end)
57+
end)
58+
59+
3060
it('is initialized with default chain', function()
3161
local default = PolicyChain.default()
32-
local policy_chain = executor.policy_chain
62+
local policy_chain = Executor.policy_chain
3363

3464
assert.same(#default, #policy_chain)
3565

@@ -45,7 +75,7 @@ describe('executor', function()
4575
local chain = PolicyChain.new({})
4676
assert.falsy(chain.frozen)
4777

48-
executor.new(chain)
78+
Executor.new(chain)
4979
assert.truthy(chain.frozen)
5080
end)
5181

@@ -58,7 +88,7 @@ describe('executor', function()
5888
policy_2.export = function() return { p2 = '2' } end
5989

6090
local chain = PolicyChain.new({ policy_1, policy_2 })
61-
local context = executor.new(chain):context('rewrite')
91+
local context = Executor.new(chain):context('rewrite')
6292

6393
assert.equal('1', context.p1)
6494
assert.equal('2', context.p2)
@@ -77,7 +107,7 @@ describe('executor', function()
77107
local inner_chain = PolicyChain.new({ policy_2, policy_3 })
78108
local outer_chain = PolicyChain.new({ policy_1, inner_chain })
79109

80-
local context = executor.new(outer_chain):context('rewrite')
110+
local context = Executor.new(outer_chain):context('rewrite')
81111

82112
assert.equal('1', context.p1)
83113
assert.equal('2', context.p2)

0 commit comments

Comments
 (0)