zhoujiexiong commented on code in PR #11374:
URL: https://github.com/apache/apisix/pull/11374#discussion_r1665155130


##########
apisix/plugins/limit-count/delayed_syncer.lua:
##########
@@ -0,0 +1,132 @@
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements.  See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership.  The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License.  You may obtain a copy of the License at
+--
+--   http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing,
+-- software distributed under the License is distributed on an
+-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+-- KIND, either express or implied.  See the License for the
+-- specific language governing permissions and limitations
+-- under the License.
+
+local core = require("apisix.core")
+local consts = require("apisix.constants")
+local new_redis_cli = require("apisix.utils.redis").new
+local new_redis_cluster_cli = require("apisix.utils.rediscluster").new
+
+local ngx_timer = ngx.timer
+local redis_stop
+local redis_cluster_stop
+local _M = {}
+
+
+local function sync_counter_data(premature, counter, to_be_synced, 
redis_confs, script)
+  if premature then
+    return
+  end
+  for key, _ in pairs(to_be_synced) do
+    local num_reqs, err = counter:get(key .. consts.REDIS_COUNTER)
+    if not num_reqs then
+      core.log.error("failed to get num_reqs shdict during periodic sync: ", 
err)
+      return
+    end
+
+    local conf = redis_confs[key]
+
+    local red, err
+    if conf.policy == "redis" then
+      red, err = new_redis_cli(conf)
+    elseif conf.policy == "redis-cluster" then
+      red, err = new_redis_cluster_cli(conf)
+    else
+      core.log.error("invalid policy type: ", conf.policy)
+      return
+    end
+
+    if not red then
+      core.log.error("failed to get redis client during periodic sync: ", err)
+      return
+    end
+
+    local res, err = red:eval(script, 1, key, conf.count, conf.time_window, 
num_reqs)
+    if err then
+      core.log.error("failed to sync shdict data to redis: ", err)
+      return
+    end
+
+    local remaining = res[1]
+    local ttl = res[2]
+    core.log.info("syncing shdict num_req counter to redis. remaining: ", 
remaining,
+                  " ttl: ", ttl, " reqs: ", num_reqs)
+    counter:set(key .. consts.SHDICT_REDIS_REMAINING, tonumber(remaining), 
tonumber(ttl))
+    counter:set(key .. consts.REDIS_COUNTER, 0)

Review Comment:
   
![image](https://github.com/apache/apisix/assets/10219699/09e93c4c-6df1-4206-898e-fb600e8db522)
   



-- 
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: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to