Use time.monotonic.
This commit is contained in:
parent
edeff079f7
commit
6d8a406ceb
1 changed files with 4 additions and 3 deletions
|
@ -50,7 +50,7 @@ class Ratelimiter:
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.lock = threading.Lock()
|
self.lock = threading.Lock()
|
||||||
|
|
||||||
self.last_operation = time.time()
|
self.last_operation = time.monotonic()
|
||||||
self.balance = 0
|
self.balance = 0
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -58,7 +58,8 @@ class Ratelimiter:
|
||||||
return self.allowance / self.period
|
return self.allowance / self.period
|
||||||
|
|
||||||
def _limit(self, cost):
|
def _limit(self, cost):
|
||||||
time_diff = time.time() - self.last_operation
|
now = time.monotonic()
|
||||||
|
time_diff = now - self.last_operation
|
||||||
self.balance += time_diff * self.gain_rate
|
self.balance += time_diff * self.gain_rate
|
||||||
self.balance = min(self.balance, self.allowance)
|
self.balance = min(self.balance, self.allowance)
|
||||||
|
|
||||||
|
@ -76,7 +77,7 @@ class Ratelimiter:
|
||||||
self.balance = 0
|
self.balance = 0
|
||||||
successful = True
|
successful = True
|
||||||
|
|
||||||
self.last_operation = time.time()
|
self.last_operation = now
|
||||||
return successful
|
return successful
|
||||||
|
|
||||||
def limit(self, cost=None):
|
def limit(self, cost=None):
|
||||||
|
|
Loading…
Reference in a new issue