diff --git a/lib/resty/limit/conn.lua b/lib/resty/limit/conn.lua index 46c7a9e..92a64ca 100644 --- a/lib/resty/limit/conn.lua +++ b/lib/resty/limit/conn.lua @@ -46,7 +46,9 @@ function _M.incoming(self, key, commit) local dict = self.dict local max = self.max - self.committed = false + if self.committed == nil then + self.committed = false + end local conn, err if commit then diff --git a/lib/resty/limit/conn.md b/lib/resty/limit/conn.md index 283342a..0dcc36a 100644 --- a/lib/resty/limit/conn.md +++ b/lib/resty/limit/conn.md @@ -193,7 +193,8 @@ key so that we can avoid a single client from flooding our service with too many does not prefix nor suffix the user key so it is the user's responsibility to ensure the key is unique in the `lua_shared_dict` shm zone). * `commit` is a boolean value. If set to `true`, the object will actually record the event -in the shm zone backing the current object; otherwise it would just be a "dry run" (which is the default). +in the shm zone backing the current object; otherwise it would just be a "dry run" (which is the default), +which does not change the committed state. The return values depend on the following cases: diff --git a/t/conn.t b/t/conn.t index 041c1e8..6710f8c 100644 --- a/t/conn.t +++ b/t/conn.t @@ -114,23 +114,23 @@ committed: false 3: 0, conn: 1 committed: true 4: 0, conn: 2 -committed: false +committed: true 5: 0, conn: 2 committed: true 6: 1, conn: 3 -committed: false +committed: true 7: 1, conn: 3 -committed: false +committed: true 8: 1, conn: 3 -committed: false +committed: true 9: 1, conn: 3 -committed: false +committed: true 10: 1, conn: 3 -committed: false +committed: true 11: 1, conn: 3 -committed: false +committed: true 12: 1, conn: 3 -committed: false +committed: true --- no_error_log [error] [lua] @@ -285,3 +285,43 @@ failed to limit conn: rejected --- no_error_log [error] [lua] + + + +=== TEST 6: incoming dry run shall not modify committed state +--- http_config eval +" +$::HttpConfig + + lua_shared_dict store 1m; +" +--- config + location = /t { + content_by_lua_block { + local limit_conn = require "resty.limit.conn" + local lim = limit_conn.new("store", 2, 1, 1) + ngx.shared.store:flush_all() + local key = "foo" + + local delay, err = lim:incoming(key, true) + if not delay then + ngx.say("failed to limit conn: ", err) + end + delay, err = lim:incoming(key, false) + if not delay then + ngx.say("failed to limit conn: ", err) + end + if not lim:is_committed() then + ngx.say("not committed") + else + ngx.say("committed") + end + } + } +--- request + GET /t +--- response_body +committed +--- no_error_log +[error] +[lua]