AlinsRan commented on code in PR #12751: URL: https://github.com/apache/apisix/pull/12751#discussion_r2583656004
########## apisix/plugins/limit-count/util.lua: ########## @@ -0,0 +1,79 @@ +-- +-- 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]) >= 0, "cost must be at least 0") + 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} +]=]) + +function _M.redis_incoming(self, red, key, commit, cost) + local limit = self.limit + local window = self.window + key = self.plugin_name .. tostring(key) + + local requested_cost = cost or 1 + local script_cost = commit and requested_cost or 0 + local res, err = red:eval(commit_script, 1, key, limit, window, script_cost) + + if err then + return nil, err, 0 + end + + local stored_remaining = tonumber(res[1]) + if stored_remaining == nil then + stored_remaining = limit - script_cost + end + local ttl = tonumber(res[2]) or window + + local remaining + if commit then + remaining = stored_remaining + else + remaining = stored_remaining - requested_cost + end Review Comment: ```suggestion local remaining = stored_remaining - (commit and 0 or 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]
