Skip to content

Commit

Permalink
Policy: rate-limit headers, address PR feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
  • Loading branch information
eloycoto committed Jun 18, 2020
1 parent b489e35 commit b1f73c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
10 changes: 6 additions & 4 deletions gateway/src/apicast/policy/rate_limit_headers/cache_entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ local stringx = require 'pl.stringx'
local _M = {}
local mt = { __index = _M }

local export_separator = "#"

function _M.new(usage, max, remaining, reset)
local self = setmetatable({}, mt)

Expand Down Expand Up @@ -43,9 +45,9 @@ function _M:reset(max, remaining, reset)
end

function _M:export()
return string.format("%s#%s#%s",
self.limit:__tostring(),
self.remaining:__tostring(),
return string.format("%s%s%s%s%s",
self.limit:__tostring(), export_separator,
self.remaining:__tostring(), export_separator,
self.reset:remaining_secs_positive(now()))
end

Expand All @@ -58,7 +60,7 @@ function _M.import(usage, exported_data)
return _M.Init_empty(usage)
end

local data = stringx.split(exported_data, "#")
local data = stringx.split(exported_data, export_separator)
return _M.new(usage, data[1] or 0, data[2] or 0, data[3] or 0)
end

Expand Down
46 changes: 25 additions & 21 deletions spec/policy/custom_metrics/custom_metrics_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ describe('custom metrics policy', function()
usage:add("foo", 1)
end)

it('if Auth cache is disabled, report the usage', function()
describe('if Auth cache is disabled', function()
it('reports the usage', function()
local config = {
rules = {
{
Expand All @@ -46,34 +47,37 @@ describe('custom metrics policy', function()
policy:post_action(context)
assert.spy(policy.report).was_called()
assert.spy(policy.report).was.called_with(context, usage)
end)
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" }
describe('if Auth cache is enabled', function()
it('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()
local policy = custom_metrics.new(config)

assert.spy(context.usage.merge).was_called_with(context.usage, usage)
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)

end)
10 changes: 8 additions & 2 deletions spec/policy/rate_limit_headers/cache_entry_spec.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
local Usage = require('apicast.usage')
local cache_entry = require("apicast.policy.rate_limit_headers.cache_entry")


describe("Cache key", function()
local usage
local cache_entry

before_each(function()
usage = Usage.new()
usage:add("a", 3)
stub(ngx, 'now', function() return 100 end)
-- Imported here to be able to use stub ngx.now()
cache_entry = require("apicast.policy.rate_limit_headers.cache_entry")
end)

it("New works as expected", function()
Expand All @@ -32,10 +36,10 @@ describe("Cache key", function()

local entry = cache_entry.new(usage, 1, 10, 3)
assert.same(entry:export(), "1#10#3")

local data = cache_entry.import(usage, entry:export())
assert.same(data.limit:__tostring(), "1")
assert.same(data.remaining:__tostring(), "10")
assert.same(data.reset:__tostring(), "103")
end)


Expand All @@ -47,12 +51,14 @@ describe("Cache key", function()
local data = cache_entry.import(usage, "1#asd#123")
assert.same(data.limit:__tostring(), "1")
assert.same(data.remaining:__tostring(), "0")
assert.same(data.reset:__tostring(), "223")
end)

it("no raw_data return empty data", function()
local data = cache_entry.import(usage, "")
assert.same(data.limit:__tostring(), "0")
assert.same(data.remaining:__tostring(), "0")
assert.same(data.reset:__tostring(), "100")
end)

end)
Expand Down

0 comments on commit b1f73c6

Please sign in to comment.