Skip to content

Httpx timeout improvements #4882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions changes/unreleased/4882.JpgQAHHnCponMKStbC4JsG.toml
Original file line number Diff line number Diff line change
@@ -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 <https://www.python-httpx.org/advanced/resource-limits/>`__ for more details on these settings."
"""
[[pull_requests]]
uid = "4882"
author_uid = "Poolitzer"
closes_threads = []
6 changes: 5 additions & 1 deletion src/telegram/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down
11 changes: 8 additions & 3 deletions src/telegram/request/_httpxrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://www.python-httpx.org/advanced/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`.
Expand Down Expand Up @@ -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,
Expand All @@ -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"):
Expand Down
10 changes: 4 additions & 6 deletions tests/ext/test_applicationbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions tests/request/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
Loading
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