1
0
mirror of https://gitlab.com/simple-stock-bots/simple-stock-bot.git synced 2025-07-22 06:01:40 +00:00

Resolve "Make announcement for Milestone completion"

This commit is contained in:
2023-10-17 05:14:24 +00:00
parent 686cf889c1
commit a54653bbc7
3 changed files with 73 additions and 31 deletions

View File

@@ -1,31 +1,31 @@
import time
import logging
log = logging.getLogger(__name__)
def rate_limited(max_per_second):
"""
Decorator that ensures the wrapped function is called at most `max_per_second` times per second.
"""
min_interval = 1.0 / max_per_second
def decorate(func):
last_called = [0.0]
def rate_limited_function(*args, **kwargs):
elapsed = time.time() - last_called[0]
left_to_wait = min_interval - elapsed
if left_to_wait > 0:
log.info(f"Rate limit exceeded. Waiting for {left_to_wait:.2f} seconds.")
time.sleep(left_to_wait)
ret = func(*args, **kwargs)
last_called[0] = time.time()
return ret
return rate_limited_function
return decorate
import time
import logging
log = logging.getLogger(__name__)
def rate_limited(max_per_second):
"""
Decorator that ensures the wrapped function is called at most `max_per_second` times per second.
"""
min_interval = 1.0 / max_per_second
def decorate(func):
last_called = [0.0]
def rate_limited_function(*args, **kwargs):
elapsed = time.time() - last_called[0]
left_to_wait = min_interval - elapsed
if left_to_wait > 0:
log.info(f"Rate limit exceeded. Waiting for {left_to_wait:.2f} seconds.")
time.sleep(left_to_wait)
ret = func(*args, **kwargs)
last_called[0] = time.time()
return ret
return rate_limited_function
return decorate