Skip to content

Commit 19f37eb

Browse files
authored
Merge pull request #758 from 3scale/lua-resty-limit-traffic
[resty] use forked resty.limit.count
2 parents b94979d + 4053b47 commit 19f37eb

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2727
- The `scope` of the Rate Limit policy is `service` by default [PR #704](https://github.com/3scale/apicast/pull/704)
2828
- Decoded JWTs are now exposed in the policies context by the APIcast policy [PR #718](https://github.com/3scale/apicast/pull/718)
2929
- Upgraded OpenResty to 1.13.6.2, uses OpenSSL 1.1 [PR #733](https://github.com/3scale/apicast/pull/733)
30+
- Use forked `resty.limit.count` that uses increments instead of decrements [PR #758](https://github.com/3scale/apicast/pull/758)
3031

3132
### Fixed
3233

gateway/src/apicast/policy/rate_limit/rate_limit.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local _M = policy.new('Rate Limit Policy')
33

44
local resty_limit_conn = require('resty.limit.conn')
55
local resty_limit_req = require('resty.limit.req')
6-
local resty_limit_count = require('resty.limit.count')
6+
local resty_limit_count = require('resty.limit.count-inc')
77

88
local ngx_semaphore = require "ngx.semaphore"
99
local limit_traffic = require "resty.limit.traffic"

gateway/src/resty/limit/count-inc.lua

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- This file applies a patch from
2+
-- https://github.com/3scale/lua-resty-limit-traffic/commit/c53f2240e953a9656580dbafcc142363ab063100
3+
-- It can be removed when https://github.com/openresty/lua-resty-limit-traffic/pull/34 is merged and released.
4+
5+
local _M = {}
6+
local resty_limit_count = require('resty.limit.count')
7+
local setmetatable = setmetatable
8+
9+
local mt = {
10+
__index = _M
11+
}
12+
13+
function _M.new(...)
14+
return setmetatable(resty_limit_count.new(...), mt)
15+
end
16+
17+
function _M.incoming(self, key, commit)
18+
local dict = self.dict
19+
local limit = self.limit
20+
local window = self.window
21+
22+
local count, err
23+
24+
if commit then
25+
count, err = dict:incr(key, 1, 0, window)
26+
27+
if not count then
28+
return nil, err
29+
end
30+
else
31+
count = (dict:get(key) or 0) + 1
32+
end
33+
34+
if count > limit then
35+
return nil, "rejected"
36+
end
37+
38+
return 0, limit - count
39+
end
40+
41+
-- uncommit remaining and return remaining value
42+
function _M.uncommit(self, key)
43+
assert(key)
44+
local dict = self.dict
45+
local limit = self.limit
46+
47+
local count, err = dict:incr(key, -1)
48+
if not count then
49+
if err == "not found" then
50+
count = 0
51+
else
52+
return nil, err
53+
end
54+
end
55+
56+
return limit - count
57+
end
58+
59+
return setmetatable(_M, { __index = resty_limit_count })
60+

spec/policy/rate_limit/rate_limit_spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('Rate limit policy', function()
149149
redis:connect(redis_host, redis_port)
150150
redis:select(1)
151151
local fixed_window = redis:get('fixed_window_test3')
152-
assert.equal('-1', fixed_window)
152+
assert.equal('2', fixed_window)
153153
end)
154154

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

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

194194
it('delay (conn)', function()

0 commit comments

Comments
 (0)