This is an automated email from the ASF dual-hosted git repository.

liling pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 37823fd  bug: set new value if the redis flag is expired. (#1110)
37823fd is described below

commit 37823fdbc0dbff0a5954a47ab744f2ffbfd05faf
Author: YuanSheng Wang <[email protected]>
AuthorDate: Thu Mar 19 23:25:43 2020 +0800

    bug: set new value if the redis flag is expired. (#1110)
    
    * bug: set new value if the redis flag is expired.
    
    * change: added todo item.
---
 .../plugins/limit-count/limit-count-redis.lua      | 37 ++++++++++++++++++----
 1 file changed, 31 insertions(+), 6 deletions(-)

diff --git a/lua/apisix/plugins/limit-count/limit-count-redis.lua 
b/lua/apisix/plugins/limit-count/limit-count-redis.lua
index ab5dbeb..b71cd85 100644
--- a/lua/apisix/plugins/limit-count/limit-count-redis.lua
+++ b/lua/apisix/plugins/limit-count/limit-count-redis.lua
@@ -16,12 +16,13 @@
 --
 local redis_new = require("resty.redis").new
 local core = require("apisix.core")
+local resty_lock = require("resty.lock")
 local assert = assert
 local setmetatable = setmetatable
 local tostring = tostring
 
 
-local _M = {version = 0.2}
+local _M = {version = 0.3}
 
 
 local mt = {
@@ -70,15 +71,39 @@ function _M.incoming(self, key)
     local remaining
     key = self.plugin_name .. tostring(key)
 
-    local ret, err = red:ttl(key)
+    local ret = red:ttl(key)
     core.log.info("ttl key: ", key, " ret: ", ret, " err: ", err)
     if ret < 0 then
-        ret, err = red:set(key, limit -1, "EX", window, "NX")
-        if not ret then
-            return nil, err
+        -- todo: test case
+        local lock, err = resty_lock:new("plugin-limit-count")
+        if not lock then
+            return false, "failed to create lock: " .. err
         end
 
-        return 0, limit -1
+        local elapsed, err = lock:lock(key)
+        if not elapsed then
+            return false, "failed to acquire the lock: " .. err
+        end
+
+        ret = red:ttl(key)
+        if ret < 0 then
+            ok, err = lock:unlock()
+            if not ok then
+                return false, "failed to unlock: " .. err
+            end
+
+            ret, err = red:set(key, limit -1, "EX", window)
+            if not ret then
+                return nil, err
+            end
+
+            return 0, limit -1
+        end
+
+        ok, err = lock:unlock()
+        if not ok then
+            return false, "failed to unlock: " .. err
+        end
     end
 
     remaining, err = red:incrby(key, -1)

Reply via email to