Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[resty] use forked resty.limit.count #758

Merged
merged 1 commit into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- The `scope` of the Rate Limit policy is `service` by default [PR #704](https://github.com/3scale/apicast/pull/704)
- Decoded JWTs are now exposed in the policies context by the APIcast policy [PR #718](https://github.com/3scale/apicast/pull/718)
- Upgraded OpenResty to 1.13.6.2, uses OpenSSL 1.1 [PR #733](https://github.com/3scale/apicast/pull/733)
- Use forked `resty.limit.count` that uses increments instead of decrements [PR #758](https://github.com/3scale/apicast/pull/758)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion gateway/src/apicast/policy/rate_limit/rate_limit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local _M = policy.new('Rate Limit Policy')

local resty_limit_conn = require('resty.limit.conn')
local resty_limit_req = require('resty.limit.req')
local resty_limit_count = require('resty.limit.count')
local resty_limit_count = require('resty.limit.count-inc')

local ngx_semaphore = require "ngx.semaphore"
local limit_traffic = require "resty.limit.traffic"
Expand Down
60 changes: 60 additions & 0 deletions gateway/src/resty/limit/count-inc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
-- This file applies a patch from
-- https://github.com/3scale/lua-resty-limit-traffic/commit/c53f2240e953a9656580dbafcc142363ab063100
-- It can be removed when https://github.com/openresty/lua-resty-limit-traffic/pull/34 is merged and released.

local _M = {}
local resty_limit_count = require('resty.limit.count')
local setmetatable = setmetatable

local mt = {
__index = _M
}

function _M.new(...)
return setmetatable(resty_limit_count.new(...), mt)
end

function _M.incoming(self, key, commit)
local dict = self.dict
local limit = self.limit
local window = self.window

local count, err

if commit then
count, err = dict:incr(key, 1, 0, window)

if not count then
return nil, err
end
else
count = (dict:get(key) or 0) + 1
end

if count > limit then
return nil, "rejected"
end

return 0, limit - count
end

-- uncommit remaining and return remaining value
function _M.uncommit(self, key)
assert(key)
local dict = self.dict
local limit = self.limit

local count, err = dict:incr(key, -1)
if not count then
if err == "not found" then
count = 0
else
return nil, err
end
end

return limit - count
end

return setmetatable(_M, { __index = resty_limit_count })

6 changes: 3 additions & 3 deletions spec/policy/rate_limit/rate_limit_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('Rate limit policy', function()
redis:connect(redis_host, redis_port)
redis:select(1)
local fixed_window = redis:get('fixed_window_test3')
assert.equal('-1', fixed_window)
assert.equal('2', fixed_window)
end)

it('rejected (count), name_type is liquid', function()
Expand All @@ -169,7 +169,7 @@ describe('Rate limit policy', function()
redis:connect(redis_host, redis_port)
redis:select(1)
local fixed_window = redis:get('fixed_window_test3')
assert.equal('-1', fixed_window)
assert.equal('2', fixed_window)
end)

it('rejected (count), name_type is liquid, ngx variable', function()
Expand All @@ -188,7 +188,7 @@ describe('Rate limit policy', function()
redis:connect(redis_host, redis_port)
redis:select(1)
local fixed_window = redis:get('fixed_window_test3')
assert.equal('-1', fixed_window)
assert.equal('2', fixed_window)
end)

it('delay (conn)', function()
Expand Down