From 6d8a406ceba5a203690a61b1d0e2d18fb623ea6c Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sat, 20 Nov 2021 18:51:46 -0800 Subject: [PATCH] Use time.monotonic. --- voussoirkit/ratelimiter.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/voussoirkit/ratelimiter.py b/voussoirkit/ratelimiter.py index d721661..e00f39c 100644 --- a/voussoirkit/ratelimiter.py +++ b/voussoirkit/ratelimiter.py @@ -50,7 +50,7 @@ class Ratelimiter: self.mode = mode self.lock = threading.Lock() - self.last_operation = time.time() + self.last_operation = time.monotonic() self.balance = 0 @property @@ -58,7 +58,8 @@ class Ratelimiter: return self.allowance / self.period 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 = min(self.balance, self.allowance) @@ -76,7 +77,7 @@ class Ratelimiter: self.balance = 0 successful = True - self.last_operation = time.time() + self.last_operation = now return successful def limit(self, cost=None):