Skip to content

Commit 574f0bb

Browse files
committed
bugfix: mark committed to false only when it is nil in conn
1 parent 449e8b4 commit 574f0bb

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

lib/resty/limit/conn.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ function _M.incoming(self, key, commit)
4646
local dict = self.dict
4747
local max = self.max
4848

49-
self.committed = false
49+
if self.committed == nil then
50+
self.committed = false
51+
end
5052

5153
local conn, err
5254
if commit then

lib/resty/limit/conn.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ key so that we can avoid a single client from flooding our service with too many
193193
does not prefix nor suffix the user key so it is the user's responsibility to ensure the key
194194
is unique in the `lua_shared_dict` shm zone).
195195
* `commit` is a boolean value. If set to `true`, the object will actually record the event
196-
in the shm zone backing the current object; otherwise it would just be a "dry run" (which is the default).
196+
in the shm zone backing the current object; otherwise it would just be a "dry run" (which is the default),
197+
which does not change the committed state.
197198

198199
The return values depend on the following cases:
199200

t/conn.t

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,23 @@ committed: false
114114
3: 0, conn: 1
115115
committed: true
116116
4: 0, conn: 2
117-
committed: false
117+
committed: true
118118
5: 0, conn: 2
119119
committed: true
120120
6: 1, conn: 3
121-
committed: false
121+
committed: true
122122
7: 1, conn: 3
123-
committed: false
123+
committed: true
124124
8: 1, conn: 3
125-
committed: false
125+
committed: true
126126
9: 1, conn: 3
127-
committed: false
127+
committed: true
128128
10: 1, conn: 3
129-
committed: false
129+
committed: true
130130
11: 1, conn: 3
131-
committed: false
131+
committed: true
132132
12: 1, conn: 3
133-
committed: false
133+
committed: true
134134
--- no_error_log
135135
[error]
136136
[lua]
@@ -285,3 +285,43 @@ failed to limit conn: rejected
285285
--- no_error_log
286286
[error]
287287
[lua]
288+
289+
290+
291+
=== TEST 6: incoming dry run shall not modify committed state
292+
--- http_config eval
293+
"
294+
$::HttpConfig
295+
296+
lua_shared_dict store 1m;
297+
"
298+
--- config
299+
location = /t {
300+
content_by_lua_block {
301+
local limit_conn = require "resty.limit.conn"
302+
local lim = limit_conn.new("store", 2, 1, 1)
303+
ngx.shared.store:flush_all()
304+
local key = "foo"
305+
306+
local delay, err = lim:incoming(key, true)
307+
if not delay then
308+
ngx.say("failed to limit conn: ", err)
309+
end
310+
delay, err = lim:incoming(key, false)
311+
if not delay then
312+
ngx.say("failed to limit conn: ", err)
313+
end
314+
if not lim:is_committed() then
315+
ngx.say("not committed")
316+
else
317+
ngx.say("committed")
318+
end
319+
}
320+
}
321+
--- request
322+
GET /t
323+
--- response_body
324+
committed
325+
--- no_error_log
326+
[error]
327+
[lua]

0 commit comments

Comments
 (0)