Skip to content
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

Fix the error in generating the idle connection list #920

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

- Fix generate the idle connection list. (#920)
- Handle `SSLError` exception. (#918)

## 1.0.5 (March 27th, 2024)
Expand Down
8 changes: 7 additions & 1 deletion httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
closing_connections.append(connection)
elif (
connection.is_idle()
and len([connection.is_idle() for connection in self._connections])
and len(
[
connection
for connection in self._connections
if connection.is_idle()
]
)
> self._max_keepalive_connections
):
# log: "closing idle connection"
Expand Down
8 changes: 7 additions & 1 deletion httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ def _assign_requests_to_connections(self) -> List[ConnectionInterface]:
closing_connections.append(connection)
elif (
connection.is_idle()
and len([connection.is_idle() for connection in self._connections])
and len(
[
connection
for connection in self._connections
if connection.is_idle()
]
)
> self._max_keepalive_connections
):
# log: "closing idle connection"
Expand Down
30 changes: 30 additions & 0 deletions tests/_async/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,3 +830,33 @@ async def trace(name, kwargs):
"http11.response_closed.started",
"http11.response_closed.complete",
]


@pytest.mark.anyio
async def test_connection_pool_with_idle_connections():
"""
When the max_keepalive_connections is not set, we can get 10 idle connections after 10 requests,
if max_keepalive_connections is set to 5, we only get 5 idle connections after 10 requests.
"""
network_backend = httpcore.AsyncMockBackend(
[
b"HTTP/1.1 200 OK\r\n",
b"Content-Type: plain/text\r\n",
b"Content-Length: 13\r\n",
b"\r\n",
b"Hello, world!",
]
* 10
)

async with httpcore.AsyncConnectionPool(network_backend=network_backend) as pool:
for i in range(10):
async with pool.stream("GET", f"https://example{i}.com/") as response:
await response.aread()

assert 10 == len([c for c in pool.connections if c.is_idle()])
pool._max_keepalive_connections = 5
for i in range(10):
async with pool.stream("GET", f"https://example{i}.com/") as response:
await response.aread()
assert 5 == len([c for c in pool.connections if c.is_idle()])
Loading