Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

obnoxiousmods
Copy link

@obnoxiousmods obnoxiousmods commented Sep 14, 2024

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
Copy link
Author

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

@nathom
Copy link
Owner

nathom commented Dec 7, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants