Skip to content

Commit

Permalink
add retry_after parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
z44d committed Jun 28, 2024
1 parent 442f1b6 commit 92095f6
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions tgram/tgbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def __init__(
parse_mode: Literal["Markdown", "MarkdownV2", "HTML"] = None,
protect_content: bool = None,
workers: int = None,
retry_after: int = 0,
) -> None:
self.bot_token = bot_token
self.api_url = api_url
Expand All @@ -93,6 +94,7 @@ def __init__(
self.parse_mode = parse_mode
self.protect_content = protect_content
self.workers = workers or min(32, (os.cpu_count() or 0) + 4)
self.retry_after = retry_after
self.executor = ThreadPoolExecutor(self.workers, thread_name_prefix="Handlers")
self.loop = asyncio.get_event_loop()

Expand Down Expand Up @@ -172,6 +174,16 @@ async def _send_request(self, method: str, **kwargs) -> Any:
response_json = await response.json()

if not response_json["ok"]:
if response_json["error_code"] is 429 and self.retry_after:
s = response_json["parameters"]["retry_after"]
retry_after = s if s < self.retry_after else self.retry_after
logger.warning(
"You got floodwait for %s seconds, I will retry after %s",
s,
retry_after,
)
await asyncio.sleep(retry_after)
return await self._send_request(method, **kwargs)
del response_json["ok"]
raise APIException(json.dumps(response_json))

Expand Down

0 comments on commit 92095f6

Please sign in to comment.