Skip to content

Fix max_keepalive_connections #1000

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

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## Unreleased

- Fix `max_keepalive_connections` not being properly handled. (#1000)

## Version 1.0.7 (November 15th, 2024)

- Support `proxy=…` configuration on `ConnectionPool()`. (#974)
Expand Down
2 changes: 1 addition & 1 deletion httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ 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 sum(connection.is_idle() for connection in self._connections)
> self._max_keepalive_connections
):
# log: "closing idle connection"
Expand Down
2 changes: 1 addition & 1 deletion httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ 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 sum(connection.is_idle() for connection in self._connections)
> self._max_keepalive_connections
):
# log: "closing idle connection"
Expand Down
21 changes: 13 additions & 8 deletions tests/_async/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def test_connection_pool_with_keepalive():
)

async with httpcore.AsyncConnectionPool(
network_backend=network_backend,
network_backend=network_backend, max_keepalive_connections=1
) as pool:
# Sending an intial request, which once complete will return to the pool, IDLE.
async with pool.stream("GET", "https://example.com/") as response:
Expand Down Expand Up @@ -79,28 +79,33 @@ async def test_connection_pool_with_keepalive():
)

# Sending a request to a different origin will not reuse the existing IDLE connection.
async with pool.stream("GET", "http://example.com/") as response:
async with pool.stream("GET", "http://example.com/") as response_1, pool.stream(
"GET", "http://example.com/"
) as response_2:
info = [repr(c) for c in pool.connections]
assert info == [
"<AsyncHTTPConnection ['https://example.com:443', HTTP/1.1, IDLE, Request Count: 2]>",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This connection is dropped without the fix.

"<AsyncHTTPConnection ['http://example.com:80', HTTP/1.1, ACTIVE, Request Count: 1]>",
"<AsyncHTTPConnection ['http://example.com:80', HTTP/1.1, ACTIVE, Request Count: 1]>",
]
assert (
repr(pool)
== "<AsyncConnectionPool [Requests: 1 active, 0 queued | Connections: 1 active, 1 idle]>"
== "<AsyncConnectionPool [Requests: 2 active, 0 queued | Connections: 2 active, 1 idle]>"
)
await response.aread()
await response_1.aread()
await response_2.aread()

assert response.status == 200
assert response.content == b"Hello, world!"
assert response_1.status == 200
assert response_1.content == b"Hello, world!"
assert response_2.status == 200
assert response_2.content == b"Hello, world!"
info = [repr(c) for c in pool.connections]
assert info == [
"<AsyncHTTPConnection ['https://example.com:443', HTTP/1.1, IDLE, Request Count: 2]>",
"<AsyncHTTPConnection ['http://example.com:80', HTTP/1.1, IDLE, Request Count: 1]>",
]
assert (
repr(pool)
== "<AsyncConnectionPool [Requests: 0 active, 0 queued | Connections: 0 active, 2 idle]>"
== "<AsyncConnectionPool [Requests: 0 active, 0 queued | Connections: 0 active, 1 idle]>"
)


Expand Down
21 changes: 13 additions & 8 deletions tests/_sync/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_connection_pool_with_keepalive():
)

with httpcore.ConnectionPool(
network_backend=network_backend,
network_backend=network_backend, max_keepalive_connections=1
) as pool:
# Sending an intial request, which once complete will return to the pool, IDLE.
with pool.stream("GET", "https://example.com/") as response:
Expand Down Expand Up @@ -79,28 +79,33 @@ def test_connection_pool_with_keepalive():
)

# Sending a request to a different origin will not reuse the existing IDLE connection.
with pool.stream("GET", "http://example.com/") as response:
with pool.stream("GET", "http://example.com/") as response_1, pool.stream(
"GET", "http://example.com/"
) as response_2:
info = [repr(c) for c in pool.connections]
assert info == [
"<HTTPConnection ['https://example.com:443', HTTP/1.1, IDLE, Request Count: 2]>",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This connection is dropped without the fix.

"<HTTPConnection ['http://example.com:80', HTTP/1.1, ACTIVE, Request Count: 1]>",
"<HTTPConnection ['http://example.com:80', HTTP/1.1, ACTIVE, Request Count: 1]>",
]
assert (
repr(pool)
== "<ConnectionPool [Requests: 1 active, 0 queued | Connections: 1 active, 1 idle]>"
== "<ConnectionPool [Requests: 2 active, 0 queued | Connections: 2 active, 1 idle]>"
)
response.read()
response_1.read()
response_2.read()

assert response.status == 200
assert response.content == b"Hello, world!"
assert response_1.status == 200
assert response_1.content == b"Hello, world!"
assert response_2.status == 200
assert response_2.content == b"Hello, world!"
info = [repr(c) for c in pool.connections]
assert info == [
"<HTTPConnection ['https://example.com:443', HTTP/1.1, IDLE, Request Count: 2]>",
"<HTTPConnection ['http://example.com:80', HTTP/1.1, IDLE, Request Count: 1]>",
]
assert (
repr(pool)
== "<ConnectionPool [Requests: 0 active, 0 queued | Connections: 0 active, 2 idle]>"
== "<ConnectionPool [Requests: 0 active, 0 queued | Connections: 0 active, 1 idle]>"
)


Expand Down