Add async_antiflood helper for AsyncTeleBot#2589
Open
Dramex wants to merge 2 commits intoeternnoir:masterfrom
Open
Add async_antiflood helper for AsyncTeleBot#2589Dramex wants to merge 2 commits intoeternnoir:masterfrom
Dramex wants to merge 2 commits intoeternnoir:masterfrom
Conversation
The sync antiflood() helper in telebot.util wraps a call in a retry loop that honours Telegram's retry_after parameter on 429 responses. There is no equivalent for AsyncTeleBot users, so every async bot ends up re-implementing the same loop around asyncio.sleep and ApiTelegramException. This adds async_antiflood(), mirroring the sync signature and behaviour but awaiting function() and using asyncio.sleep(). Also adds four tests covering: - the happy path returns on the first call without sleeping - a single 429 triggers exactly one retry after sleeping retry_after seconds - non-429 ApiTelegramException propagates immediately with no retry - after number_retries attempts the final 429 surfaces to the caller
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an async equivalent of telebot.util.antiflood() for AsyncTeleBot, enabling retry-on-429 behavior that honors Telegram’s retry_after parameter without requiring each async bot to re-implement the loop.
Changes:
- Introduce
telebot.util.async_antiflood()mirroringantiflood()semantics for async callables. - Export
async_antifloodviatelebot.util.__all__. - Add unit tests covering success, 429 retry/sleep behavior, non-429 propagation, and retry budget exhaustion.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
telebot/util.py |
Adds async_antiflood() and exports it in __all__. |
tests/test_async_telebot.py |
Adds test cases validating async_antiflood() behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Collaborator
|
LGTM. @coder2020official ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
telebot.util.antiflood()wraps a sync call in a retry loop that honours Telegram'sretry_afterparameter on 429 responses.AsyncTeleBotusers have no equivalent, so every async bot re-implements the same loop aroundasyncio.sleepandApiTelegramException.This adds
async_antiflood()next to the sync one intelebot.util. Same signature, same retry budget semantics, but awaitsfunction(*args, **kwargs)and sleeps viaasyncio.sleep. Drop-in for an async bot:Behaviour
Mirrors the sync version exactly:
ApiTelegramExceptionpropagates immediately, no retry.retry_afterseconds, then retry, up tonumber_retries - 1times.Tests
Four new tests in
tests/test_async_telebot.py:retry_aftersecondsnumber_retriesattempts the last 429 surfacesAll five tests in the file pass (the earlier strong-ref test from #2588 plus the four new ones).
Notes
async_antifloodimportsApiTelegramExceptionandasynciolazily at call time, same style the sync helper uses, sotelebot.utilkeeps its import graph clean.telebot.util.__all__.