AlinsRan commented on code in PR #12751:
URL: https://github.com/apache/apisix/pull/12751#discussion_r2583651173
##########
apisix/plugins/limit-count/limit-count-redis.lua:
##########
@@ -52,37 +44,66 @@ function _M.new(plugin_name, limit, window, conf)
return setmetatable(self, mt)
end
-function _M.incoming(self, key, cost)
+
+local function log_phase_incoming_thread(premature, self, key, cost)
local conf = self.conf
local red, err = redis.new(conf)
if not red then
- return red, err, 0
+ return red, err
end
+ return util.redis_log_phase_incoming(self, red, key, cost)
+end
- local limit = self.limit
- local window = self.window
- local res
- key = self.plugin_name .. tostring(key)
- local ttl = 0
- res, err = red:eval(script, 1, key, limit, window, cost or 1)
+local function log_phase_incoming(self, key, cost, dry_run)
+ if dry_run then
+ return true
+ end
- if err then
- return nil, err, ttl
+ local ok, err = ngx_timer_at(0, log_phase_incoming_thread, self, key, cost)
+ if not ok then
+ core.log.error("failed to create timer: ", err)
+ return nil, err
end
- local remaining = res[1]
- ttl = res[2]
+ return ok
+end
+
+
+function _M.incoming(self, key, cost, dry_run)
+ if get_phase() == "log" then
+ local ok, err = log_phase_incoming(self, key, cost, dry_run)
+ if not ok then
+ return nil, err, 0
+ end
+
+ -- best-effort result because lua-resty-redis is not allowed in log
phase
+ return 0, self.limit, self.window
+ end
+
+ local conf = self.conf
+ local red, err = redis.new(conf)
+ if not red then
+ return red, err, 0
+ end
+
+ local commit = true
+ if dry_run ~= nil then
+ commit = not dry_run
+ end
+
+ local delay, remaining, ttl = util.redis_incoming(self, red, key, commit,
cost)
Review Comment:
```suggestion
local delay, remaining, ttl = util.redis_incoming(self, red, key, not
commit, cost)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]