Fix ratelimiter reject mode always subtracting balance.

master
voussoir 2021-12-21 17:02:08 -08:00
parent 0a0bbae88c
commit 28b4c8626b
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 10 additions and 10 deletions

View File

@ -21,7 +21,8 @@ class Ratelimiter:
Our spending balance per `period` seconds.
period:
The number of seconds over which we can perform `allowance` operations.
The number of seconds over which we can perform `allowance`
operations.
operation_cost:
The default amount to remove from our balance after each operation.
@ -65,22 +66,21 @@ class Ratelimiter:
time_diff = now - self.last_operation
self.balance += time_diff * self.gain_rate
self.balance = min(self.balance, self.allowance)
self.last_operation = now
self.balance -= cost
if self.balance >= 0:
success = True
sleep_needed = 0
elif self.mode == 'reject':
if self.mode == 'reject' and self.balance < cost:
success = False
sleep_needed = 0
return (success, sleep_needed)
self.balance -= cost
success = True
if self.balance >= 0:
sleep_needed = 0
else:
success = True
sleep_needed = abs(self.balance) / self.gain_rate
self.last_operation = now
return (success, sleep_needed)
def limit(self, cost=None):