fixed rate limit
This commit is contained in:
parent
76760bdaaf
commit
e4be2cb69c
1 changed files with 29 additions and 22 deletions
|
|
@ -27,30 +27,37 @@ def rate_limited(func):
|
||||||
"""Decorator to enforce rate limiting on Tidal API calls"""
|
"""Decorator to enforce rate limiting on Tidal API calls"""
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
global _last_api_call_time
|
max_retries = 3
|
||||||
|
last_exception = None
|
||||||
|
for attempt in range(max_retries):
|
||||||
|
global _last_api_call_time
|
||||||
|
|
||||||
with _api_call_lock:
|
with _api_call_lock:
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
time_since_last_call = current_time - _last_api_call_time
|
time_since_last_call = current_time - _last_api_call_time
|
||||||
|
|
||||||
if time_since_last_call < MIN_API_INTERVAL:
|
if time_since_last_call < MIN_API_INTERVAL:
|
||||||
sleep_time = MIN_API_INTERVAL - time_since_last_call
|
sleep_time = MIN_API_INTERVAL - time_since_last_call
|
||||||
time.sleep(sleep_time)
|
time.sleep(sleep_time)
|
||||||
|
|
||||||
_last_api_call_time = time.time()
|
_last_api_call_time = time.time()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = func(*args, **kwargs)
|
result = func(*args, **kwargs)
|
||||||
return result
|
return result
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Implement exponential backoff for API errors
|
last_exception = e
|
||||||
if "rate limit" in str(e).lower() or "429" in str(e):
|
# Implement exponential backoff for API errors
|
||||||
logger.warning(f"Rate limit hit, implementing backoff: {e}")
|
if "rate limit" in str(e).lower() or "429" in str(e):
|
||||||
time.sleep(3.0) # Wait 3 seconds before retrying
|
logger.warning(f"Rate limit hit, implementing backoff: {e}")
|
||||||
elif "503" in str(e) or "502" in str(e):
|
time.sleep(3.0) # Wait 3 seconds before retrying
|
||||||
logger.warning(f"Tidal service error, backing off: {e}")
|
continue
|
||||||
time.sleep(2.0) # Wait 2 seconds for service errors
|
elif "503" in str(e) or "502" in str(e):
|
||||||
raise
|
logger.warning(f"Tidal service error, backing off: {e}")
|
||||||
|
time.sleep(2.0) # Wait 2 seconds for service errors
|
||||||
|
continue
|
||||||
|
|
||||||
|
raise last_exception
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue