Skip to content

Commit

Permalink
Merge pull request #8 from moonlibs/fix-infinite-rate
Browse files Browse the repository at this point in the history
fix: sync/rate fixes rate:wait() on infinite rps
  • Loading branch information
ochaton authored Sep 5, 2023
2 parents 05a4778 + 2b284e5 commit 00722df
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sync.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local sync = {
_VERSION = '0.11.0',
_VERSION = '0.11.1',
}

sync.cond = require 'sync.cond'
Expand Down
4 changes: 2 additions & 2 deletions sync/rate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ end
---@return boolean|sync.rate.reservation reservation, any? error_or_time_to_act
function rate:_reserve(time, n, wait)
if self.rps == math.huge then
return true
return true, time
end
if self.rps == 0 then
if self.burst >= n then
self.burst = self.burst - n
return true
return true, time
end
return false, "not enough burst"
end
Expand Down
5 changes: 4 additions & 1 deletion test/06-limit.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ test:deadline(function()

test:ok(almost_gt(reserv.timeToAct, fiber.time()+1/rate.rps, 0.01), "timeToAct ≥ now+1/rps")
test:ok(almost_gt(fiber.time()+2/rate.rps, reserv.timeToAct, 0.01), "timeToAct ≤ now+2/rps")
reserv:cancel() -- cancell reservation
reserv:cancel() -- cancel reservation

reserv = rate:reserve()
assert(reserv)
Expand Down Expand Up @@ -142,6 +142,9 @@ test:deadline(function()
for _ = 1, 5 do
test:ok(rate:allow(), "infinite rate almost allows")
end
for _ = 1, 5 do
test:ok(rate:wait(), "infinite rate never waits")
end
end)

end, 3, "rps=inf")
Expand Down

0 comments on commit 00722df

Please sign in to comment.