Skip to content

Commit

Permalink
Merge pull request #758 from 3scale/lua-resty-limit-traffic
Browse files Browse the repository at this point in the history
[resty] use forked resty.limit.count
  • Loading branch information
mikz authored Jun 11, 2018
2 parents b94979d + 4053b47 commit 19f37eb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
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

0 comments on commit 19f37eb

Please sign in to comment.