RcGcDb/src/wiki_ratelimiter.py
2021-03-17 14:15:29 +01:00

20 lines
680 B
Python

import logging, time, asyncio
logger = logging.getLogger("rcgcdw.ratelimiter")
class RateLimiter:
def __init__(self):
self.timeout_until = 0
def timeout_add(self, timeout: float):
"""This function sets a new timeout"""
self.timeout_until = time.time() + timeout
#logger.debug("Added {} timeout".format(timeout))
async def timeout_wait(self):
"""This awaitable calculates the time to wait according to timeout_until, does not wait if it's past the timeout to not skip a cycle"""
calculated_timeout = self.timeout_until - time.time()
#logger.debug("Waiting {}".format(calculated_timeout))
if calculated_timeout > 0:
await asyncio.sleep(calculated_timeout)