-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Policy: rate_limit_headers fix cache issues.
When testing rate_limit_headers the number of APICast workers are always set to 1. When the number of workers increased, the lru_cache is not longer effective due to multiple threads. Because we only have information about usage statistics on Authrep, and this is using ngx.shared info, this policy also need to use shared information to be able to reply without doing the authrep in all sessions. Fix THREESCALE-3795 Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
- Loading branch information
Showing
8 changed files
with
146 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
lua_shared_dict api_keys 10m; | ||
lua_shared_dict rate_limit_headers 20m; | ||
lua_shared_dict configuration 10m; | ||
lua_shared_dict locks 1m; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
local custom_metrics = require("apicast.policy.custom_metrics") | ||
local ngx_variable = require 'apicast.policy.ngx_variable' | ||
local backend_client = require('apicast.backend_client') | ||
local Usage = require('apicast.usage') | ||
|
||
|
||
describe('custom metrics policy', function() | ||
local context = {} | ||
local usage = {} | ||
|
||
before_each(function() | ||
ngx.var = {} | ||
ngx.header = {} | ||
|
||
stub(ngx_variable, 'available_context', function(context) return context end) | ||
stub(ngx.req, 'get_headers', function() return {} end) | ||
stub(ngx.resp, 'get_headers', function() return {} end) | ||
|
||
context = { usage = {} } | ||
stub(context.usage, 'merge', function() return end) | ||
|
||
usage = Usage.new() | ||
usage:add("foo", 1) | ||
end) | ||
|
||
it('if Auth cache is disabled, report the usage', function() | ||
local config = { | ||
rules = { | ||
{ | ||
metric = "foo", | ||
increment = "1", | ||
condition = { | ||
combine_op = "and", | ||
operations = { | ||
{ left = "foo", op = "==", right = "foo" } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
local policy = custom_metrics.new(config) | ||
|
||
stub(policy, "report", function(context, usage) return end) | ||
|
||
policy:post_action(context) | ||
assert.spy(policy.report).was_called() | ||
assert.spy(policy.report).was.called_with(context, usage) | ||
end) | ||
|
||
it('if Auth cache is enabled, usage is incremented', function() | ||
ngx.var.cached_key = true | ||
|
||
local config = { | ||
rules = { | ||
{ | ||
metric = "foo", | ||
increment = "1", | ||
condition = { | ||
combine_op = "and", | ||
operations = { | ||
{ left = "foo", op = "==", right = "foo" } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
local policy = custom_metrics.new(config) | ||
|
||
stub(policy, "report", function(context, usage) return end) | ||
policy:post_action(context) | ||
assert.spy(policy.report).was_not_called() | ||
assert.spy(context.usage.merge).was_called() | ||
|
||
assert.spy(context.usage.merge).was_called_with(context.usage, usage) | ||
|
||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters