Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

剩余可用令牌数计算错误 #5751

Open
lce0105 opened this issue Apr 3, 2024 · 0 comments
Open

剩余可用令牌数计算错误 #5751

lce0105 opened this issue Apr 3, 2024 · 0 comments

Comments

@lce0105
Copy link

lce0105 commented Apr 3, 2024

在tryAcquireAsync方法的lua脚本中, 计算当前可用的令牌数时
当tonumber(currentValue) + released > tonumber(rate)时,
那么 currentValue = tonumber(rate) - redis.call("zcard", permitsName)
这里减去的数量为什么是permitsName的元素的个数, 而不是已使用的令牌数相加

比如像下面这样计算是不是会更准确一些
if tonumber(currentValue) + released > tonumber(rate) then
// 统计当前已经使用的令牌数量
local usedValues = redis.call("zrange", permitsName, 0, -1)
local used = 0
for i, v in ipairs(usedValues) do
local random, permits = struct.unpack("Bc0I", v)
used = used + permits
end
// 可用令牌数=rate-已经使用的令牌数量
currentValue = tonumber(rate) -used

3.27.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant