diff --git a/changes/unreleased/4882.JpgQAHHnCponMKStbC4JsG.toml b/changes/unreleased/4882.JpgQAHHnCponMKStbC4JsG.toml new file mode 100644 index 00000000000..f47875d9dc3 --- /dev/null +++ b/changes/unreleased/4882.JpgQAHHnCponMKStbC4JsG.toml @@ -0,0 +1,10 @@ +other = """ +Set the default connection pool size for ``HTTPXRequest`` to 256 to allow more concurrent requests by default. Drop the ``httpx`` parameter ``max_keepalive_connections``. This way, the ``httpx`` default of 20 is used, leading to a smaller number of idle connections in large connection pools. + +.. hint:: + If you manually build the ``HTTPXRequest`` objects, please be aware that these changes also applies to you. Kindly double check your settings. To specify custom limits, you can set them via the parameter ``httpx_kwargs`` of ``HTTPXRequest``. See also `the httpx documentation `__ for more details on these settings." +""" +[[pull_requests]] +uid = "4882" +author_uid = "Poolitzer" +closes_threads = [] diff --git a/src/telegram/_bot.py b/src/telegram/_bot.py index 39056b41581..33fba87e798 100644 --- a/src/telegram/_bot.py +++ b/src/telegram/_bot.py @@ -339,7 +339,11 @@ def __init__( self._initialized: bool = False self._request: tuple[BaseRequest, BaseRequest] = ( - HTTPXRequest() if get_updates_request is None else get_updates_request, + ( + HTTPXRequest(connection_pool_size=1) + if get_updates_request is None + else get_updates_request + ), HTTPXRequest() if request is None else request, ) diff --git a/src/telegram/request/_httpxrequest.py b/src/telegram/request/_httpxrequest.py index 5b1b412463e..7b5bd961ddd 100644 --- a/src/telegram/request/_httpxrequest.py +++ b/src/telegram/request/_httpxrequest.py @@ -49,7 +49,13 @@ class HTTPXRequest(BaseRequest): Args: connection_pool_size (:obj:`int`, optional): Number of connections to keep in the - connection pool. Defaults to ``1``. + connection pool. Defaults to ``256``. + + .. versionchanged:: NEXT.VERSION + Set the default to ``256``. + Stopped applying to ``httpx.Limits.max_keepalive_connections``. Now only applies to + ``httpx.Limits.max_connections``. See `Resource Limits + `_ read_timeout (:obj:`float` | :obj:`None`, optional): If passed, specifies the maximum amount of time (in seconds) to wait for a response from Telegram's server. This value is used unless a different value is passed to :meth:`do_request`. @@ -138,7 +144,7 @@ class HTTPXRequest(BaseRequest): def __init__( self, - connection_pool_size: int = 1, + connection_pool_size: int = 256, read_timeout: Optional[float] = 5.0, write_timeout: Optional[float] = 5.0, connect_timeout: Optional[float] = 5.0, @@ -159,7 +165,6 @@ def __init__( ) limits = httpx.Limits( max_connections=connection_pool_size, - max_keepalive_connections=connection_pool_size, ) if http_version not in ("1.1", "2", "2.0"): diff --git a/tests/ext/test_applicationbuilder.py b/tests/ext/test_applicationbuilder.py index 847ce178892..b7ef6297fc4 100644 --- a/tests/ext/test_applicationbuilder.py +++ b/tests/ext/test_applicationbuilder.py @@ -150,9 +150,7 @@ class Client: assert app.bot.local_mode is False get_updates_client = app.bot._request[0]._client - assert get_updates_client.limits == httpx.Limits( - max_connections=1, max_keepalive_connections=1 - ) + assert get_updates_client.limits == httpx.Limits(max_connections=1) assert get_updates_client.proxy is None assert get_updates_client.timeout == httpx.Timeout( connect=5.0, read=5.0, write=5.0, pool=1.0 @@ -161,7 +159,7 @@ class Client: assert not get_updates_client.http2 client = app.bot.request._client - assert client.limits == httpx.Limits(max_connections=256, max_keepalive_connections=256) + assert client.limits == httpx.Limits(max_connections=256) assert client.proxy is None assert client.timeout == httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=1.0) assert client.http1 is True @@ -391,7 +389,7 @@ def init_httpx_request(self_, *args, **kwargs): client = app.bot.request._client assert client.timeout == httpx.Timeout(pool=3, connect=2, read=4, write=5) - assert client.limits == httpx.Limits(max_connections=1, max_keepalive_connections=1) + assert client.limits == httpx.Limits(max_connections=1) assert client.proxy == "proxy" assert client.http1 is True assert client.http2 is False @@ -408,7 +406,7 @@ def init_httpx_request(self_, *args, **kwargs): client = app.bot._request[0]._client assert client.timeout == httpx.Timeout(pool=3, connect=2, read=4, write=5) - assert client.limits == httpx.Limits(max_connections=1, max_keepalive_connections=1) + assert client.limits == httpx.Limits(max_connections=1) assert client.proxy == "get_updates_proxy" assert client.http1 is True assert client.http2 is False diff --git a/tests/request/test_request.py b/tests/request/test_request.py index eda6829ccf7..84cffdb044d 100644 --- a/tests/request/test_request.py +++ b/tests/request/test_request.py @@ -439,9 +439,7 @@ class Client: request = HTTPXRequest() assert request._client.timeout == httpx.Timeout(connect=5.0, read=5.0, write=5.0, pool=1.0) assert request._client.proxy is None - assert request._client.limits == httpx.Limits( - max_connections=1, max_keepalive_connections=1 - ) + assert request._client.limits == httpx.Limits(max_connections=256) assert request._client.http1 is True assert not request._client.http2 @@ -454,9 +452,7 @@ class Client: pool_timeout=46, ) assert request._client.proxy == "proxy" - assert request._client.limits == httpx.Limits( - max_connections=42, max_keepalive_connections=42 - ) + assert request._client.limits == httpx.Limits(max_connections=42) assert request._client.timeout == httpx.Timeout(connect=43, read=44, write=45, pool=46) async def test_multiple_inits_and_shutdowns(self, monkeypatch): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy