streamrip
streamrip copied to clipboard
Added parent class of aiohttp.ClientSession that does retries when 429 / Too Many Requests is detected in response
I was tired of streamrip crashing all the time from Tidal's 429
So I made this, it will automatically retry when a 429 is detected, after waiting 5 seconds anyways
Might be better to add max_retries and retry_delay to the config
( I did not test this too much, but it was working for me )
import asyncio
from typing import Any
import aiohttp
import aiohttp.log
class StreamRipClient(aiohttp.ClientSession):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.max_retries = 5
self.retry_delay = 5
async def _request(self, method: str, url: str, **kwargs) -> aiohttp.ClientResponse:
for attempt in range(self.max_retries):
try:
response = await super()._request(method, url, **kwargs)
if response.status != 429:
return response
else:
aiohttp.log.client_logger.warning(
f"Rate limited. Retrying in {self.retry_delay} seconds..."
)
await asyncio.sleep(self.retry_delay)
except aiohttp.ClientError:
if attempt == self.max_retries - 1:
raise
aiohttp.log.client_logger.warning(
f"Request failed. Retrying in {self.retry_delay} seconds..."
)
await asyncio.sleep(self.retry_delay)
raise aiohttp.ClientError(f"Max retries ({self.max_retries}) exceeded")
Can confirm it seems to work :) If other services dont 429 for rate limiting they will need more code
This is great! Can you put the class in client.py? And rename it StreamripClientSession?