Skip to content

Commit

Permalink
fix: sync/rate fixes rate:wait() on infinite rps
Browse files Browse the repository at this point in the history
	* rate:wait() crashed for rates with infinite rps _reserve
	must return numeric `timeToAct` if reservation is allowed
  • Loading branch information
Vladislav Grubov committed Sep 5, 2023
1 parent 05a4778 commit 5ab8ffc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
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 5ab8ffc

Please sign in to comment.