nic-6443 commented on code in PR #12751:
URL: https://github.com/apache/apisix/pull/12751#discussion_r2548255136


##########
apisix/plugins/limit-count/util.lua:
##########
@@ -0,0 +1,87 @@
+--
+-- 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 tostring = tostring
+local tonumber = tonumber
+local _M = {version = 0.1}
+
+local commit_script = core.string.compress_script([=[
+    assert(tonumber(ARGV[3]) >= 1, "cost must be at least 1")
+    local ttl = redis.call('ttl', KEYS[1])
+    if ttl < 0 then
+        redis.call('set', KEYS[1], ARGV[1] - ARGV[3], 'EX', ARGV[2])
+        return {ARGV[1] - ARGV[3], ARGV[2]}
+    end
+    return {redis.call('incrby', KEYS[1], 0 - ARGV[3]), ttl}
+]=])
+
+local peek_script = core.string.compress_script([=[
+    local ttl = redis.call('ttl', KEYS[1])
+    local limit = tonumber(ARGV[1])
+    local cost = tonumber(ARGV[3])
+    if ttl < 0 then
+        return {limit - cost, tonumber(ARGV[2])}
+    end
+    local current = redis.call('get', KEYS[1])
+    if not current then
+        return {limit - cost, tonumber(ARGV[2])}
+    end
+    return {tonumber(current) - cost, ttl}
+]=])
+

Review Comment:
   Is it necessary to use a separate Redis script? Can we just remove 
   ```
   assert(tonumber(ARGV[3]) >= 1, "cost must be at least 1")
   ```
   from the `commit_script` directly?



-- 
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]

Reply via email to