Prevent to crash polling (#995)

* Prevent to crash polling

* Added changelog
This commit is contained in:
Alex Root Junior 2022-08-30 01:46:19 +03:00 committed by GitHub
parent 40c29a0494
commit 0b5f4d62c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 7 deletions

1
CHANGES/995.misc.rst Normal file
View file

@ -0,0 +1 @@
Fixed polling crash when Telegram Bot API raises HTTP 429 status-code.

View file

@ -8,7 +8,7 @@ from typing import Any, AsyncGenerator, Dict, List, Optional, Union
from .. import loggers from .. import loggers
from ..client.bot import Bot from ..client.bot import Bot
from ..exceptions import TelegramAPIError, TelegramNetworkError, TelegramServerError from ..exceptions import TelegramAPIError
from ..fsm.middleware import FSMContextMiddleware from ..fsm.middleware import FSMContextMiddleware
from ..fsm.storage.base import BaseEventIsolation, BaseStorage from ..fsm.storage.base import BaseEventIsolation, BaseStorage
from ..fsm.storage.memory import DisabledEventIsolation, MemoryStorage from ..fsm.storage.memory import DisabledEventIsolation, MemoryStorage
@ -183,15 +183,15 @@ class Dispatcher(Router):
get_updates = GetUpdates(timeout=polling_timeout, allowed_updates=allowed_updates) get_updates = GetUpdates(timeout=polling_timeout, allowed_updates=allowed_updates)
kwargs = {} kwargs = {}
if bot.session.timeout: if bot.session.timeout:
# Request timeout can be lower than session timeout ant that's OK. # Request timeout can be lower than session timeout and that's OK.
# To prevent false-positive TimeoutError we should wait longer than polling timeout # To prevent false-positive TimeoutError we should wait longer than polling timeout
kwargs["request_timeout"] = int(bot.session.timeout + polling_timeout) kwargs["request_timeout"] = int(bot.session.timeout + polling_timeout)
while True: while True:
try: try:
updates = await bot(get_updates, **kwargs) updates = await bot(get_updates, **kwargs)
except (TelegramNetworkError, TelegramServerError) as e: except Exception as e:
# In cases when Telegram Bot API was inaccessible don't need to stop polling process # In cases when Telegram Bot API was inaccessible don't need to stop polling
# because some of developers can't make auto-restarting of the script # process because some developers can't make auto-restarting of the script
loggers.dispatcher.error("Failed to fetch updates - %s: %s", type(e).__name__, e) loggers.dispatcher.error("Failed to fetch updates - %s: %s", type(e).__name__, e)
# And also backoff timeout is best practice to retry any network activity # And also backoff timeout is best practice to retry any network activity
loggers.dispatcher.warning( loggers.dispatcher.warning(
@ -211,8 +211,8 @@ class Dispatcher(Router):
yield update yield update
# The getUpdates method returns the earliest 100 unconfirmed updates. # The getUpdates method returns the earliest 100 unconfirmed updates.
# To confirm an update, use the offset parameter when calling getUpdates # To confirm an update, use the offset parameter when calling getUpdates
# All updates with update_id less than or equal to offset will be marked as confirmed on the server # All updates with update_id less than or equal to offset will be marked
# and will no longer be returned. # as confirmed on the server and will no longer be returned.
get_updates.offset = update.update_id + 1 get_updates.offset = update.update_id + 1
async def _listen_update(self, update: Update, **kwargs: Any) -> Any: async def _listen_update(self, update: Update, **kwargs: Any) -> Any: