diff --git a/docs/source/bot_methods.rst b/docs/source/inclusions/bot_methods.rst similarity index 100% rename from docs/source/bot_methods.rst rename to docs/source/inclusions/bot_methods.rst diff --git a/docs/source/inclusions/pool_size_tip.rst b/docs/source/inclusions/pool_size_tip.rst new file mode 100644 index 000000000..53dd2b311 --- /dev/null +++ b/docs/source/inclusions/pool_size_tip.rst @@ -0,0 +1,10 @@ +.. tip:: + When making requests to the Bot API in an asynchronous fashion (e.g. via + :attr:`block=False `, :meth:`Application.create_task `, + :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates` or the :class:`~telegram.ext.JobQueue`), it can happen that more requests + are being made in parallel than there are connections in the pool. + If the number of requests is much higher than the number of connections, even setting + :meth:`~telegram.ext.ApplicationBuilder.pool_timeout` to a larger value may not always be enough to prevent pool + timeouts. + You should therefore set :meth:`~telegram.ext.ApplicationBuilder.concurrent_updates`, :meth:`~telegram.ext.ApplicationBuilder.connection_pool_size` and + :meth:`~telegram.ext.ApplicationBuilder.pool_timeout` to values that make sense for your setup. \ No newline at end of file diff --git a/telegram/_bot.py b/telegram/_bot.py index 46c0cfa04..ce726a3e2 100644 --- a/telegram/_bot.py +++ b/telegram/_bot.py @@ -172,7 +172,7 @@ class Bot(TelegramObject, AbstractAsyncContextManager): private_key (:obj:`bytes`, optional): Private key for decryption of telegram passport data. private_key_password (:obj:`bytes`, optional): Password for above private key. - .. include:: bot_methods.rst + .. include:: inclusions/bot_methods.rst """ diff --git a/telegram/ext/_application.py b/telegram/ext/_application.py index c4086ba7a..a28e13521 100644 --- a/telegram/ext/_application.py +++ b/telegram/ext/_application.py @@ -252,7 +252,7 @@ class Application(Generic[BT, CCT, UD, CD, BD, JQ], AbstractAsyncContextManager) if isinstance(concurrent_updates, int) and concurrent_updates < 0: raise ValueError("`concurrent_updates` must be a non-negative integer!") if concurrent_updates is True: - concurrent_updates = 4096 + concurrent_updates = 256 self._concurrent_updates_sem = asyncio.BoundedSemaphore(concurrent_updates or 1) self._concurrent_updates: int = concurrent_updates or 0 diff --git a/telegram/ext/_applicationbuilder.py b/telegram/ext/_applicationbuilder.py index aa110910b..325b624d0 100644 --- a/telegram/ext/_applicationbuilder.py +++ b/telegram/ext/_applicationbuilder.py @@ -191,7 +191,7 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]): ) else: connection_pool_size = ( - DefaultValue.get_value(getattr(self, f"{prefix}connection_pool_size")) or 128 + DefaultValue.get_value(getattr(self, f"{prefix}connection_pool_size")) or 256 ) timeouts = dict( @@ -424,7 +424,9 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]): def connection_pool_size(self: BuilderType, connection_pool_size: int) -> BuilderType: """Sets the size of the connection pool for the :paramref:`~telegram.request.HTTPXRequest.connection_pool_size` parameter of - :attr:`telegram.Bot.request`. Defaults to ``128``. + :attr:`telegram.Bot.request`. Defaults to ``256``. + + .. include:: inclusions/pool_size_tip.rst Args: connection_pool_size (:obj:`int`): The size of the connection pool. @@ -504,6 +506,8 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]): :paramref:`~telegram.request.HTTPXRequest.pool_timeout` parameter of :attr:`telegram.Bot.request`. Defaults to :obj:`None`. + .. include:: inclusions/pool_size_tip.rst + Args: pool_timeout (:obj:`float`): See :paramref:`telegram.request.HTTPXRequest.pool_timeout` for more information. @@ -770,11 +774,13 @@ class ApplicationBuilder(Generic[BT, CCT, UD, CD, BD, JQ]): that your bot does not (explicitly or implicitly) rely on updates being processed sequentially. + .. include:: inclusions/pool_size_tip.rst + .. seealso:: :attr:`telegram.ext.Application.concurrent_updates` Args: concurrent_updates (:obj:`bool` | :obj:`int`): Passing :obj:`True` will allow for - ``4096`` updates to be processed concurrently. Pass an integer to specify a + ``256`` updates to be processed concurrently. Pass an integer to specify a different number of updates that may be processed concurrently. Returns: diff --git a/tests/test_application.py b/tests/test_application.py index 13c6f0738..5c7895ada 100644 --- a/tests/test_application.py +++ b/tests/test_application.py @@ -139,7 +139,7 @@ class TestApplication: assert recwarn[0].filename == __file__, "stacklevel is incorrect!" @pytest.mark.parametrize( - "concurrent_updates, expected", [(0, 0), (4, 4), (False, 0), (True, 4096)] + "concurrent_updates, expected", [(0, 0), (4, 4), (False, 0), (True, 256)] ) @pytest.mark.filterwarnings("ignore: `Application` instances should") def test_init(self, bot, concurrent_updates, expected): diff --git a/tests/test_applicationbuilder.py b/tests/test_applicationbuilder.py index a2e0e5361..095a66137 100644 --- a/tests/test_applicationbuilder.py +++ b/tests/test_applicationbuilder.py @@ -93,7 +93,7 @@ class TestApplicationBuilder: ) client = app.bot.request._client - assert client.limits == httpx.Limits(max_connections=128, max_keepalive_connections=128) + assert client.limits == httpx.Limits(max_connections=256, max_keepalive_connections=256) assert client.proxies is None assert client.timeout == httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=1.0)