streamrip icon indicating copy to clipboard operation
streamrip copied to clipboard

Added parent class of aiohttp.ClientSession that does retries when 429 / Too Many Requests is detected in response

Open obnoxiousmods opened this issue 1 year ago • 2 comments

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")

obnoxiousmods avatar Sep 14 '24 00:09 obnoxiousmods

rip_url_httpslisten tidal comartist4761990_2024_09_13_18_06_04

Can confirm it seems to work :) If other services dont 429 for rate limiting they will need more code

obnoxiousmods avatar Sep 14 '24 01:09 obnoxiousmods

This is great! Can you put the class in client.py? And rename it StreamripClientSession?

nathom avatar Dec 07 '24 06:12 nathom