Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2c6f235

Browse files
committedJun 11, 2024·
Code review simplification
1 parent 1358872 commit 2c6f235

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed
 

‎httpcore/_async/connection_pool.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -238,28 +238,27 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
238238
those connections to be handled seperately.
239239
"""
240240
closing_connections = []
241-
idling_connections = {c for c in self._connections if c.is_idle()}
241+
idling_count = sum(1 for c in self._connections if c.is_idle())
242242

243243
# First we handle cleaning up any connections that are closed,
244244
# have expired their keep-alive, or surplus idle connections.
245245
for connection in list(self._connections):
246246
if connection.is_closed():
247247
# log: "removing closed connection"
248248
self._connections.remove(connection)
249-
idling_connections.discard(connection)
249+
idling_count -= int(connection.is_idle())
250250
elif connection.has_expired():
251251
# log: "closing expired connection"
252252
self._connections.remove(connection)
253-
idling_connections.discard(connection)
254253
closing_connections.append(connection)
254+
idling_count -= int(connection.is_idle())
255255
elif (
256-
connection.is_idle()
257-
and len(idling_connections) > self._max_keepalive_connections
256+
connection.is_idle() and idling_count > self._max_keepalive_connections
258257
):
259258
# log: "closing idle connection"
260259
self._connections.remove(connection)
261-
idling_connections.discard(connection)
262260
closing_connections.append(connection)
261+
idling_count -= 1
263262

264263
# Assign queued requests to connections.
265264
queued_requests = [request for request in self._requests if request.is_queued()]

‎httpcore/_sync/connection_pool.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -238,28 +238,27 @@ def _assign_requests_to_connections(self) -> List[ConnectionInterface]:
238238
those connections to be handled seperately.
239239
"""
240240
closing_connections = []
241-
idling_connections = {c for c in self._connections if c.is_idle()}
241+
idling_count = sum(1 for c in self._connections if c.is_idle())
242242

243243
# First we handle cleaning up any connections that are closed,
244244
# have expired their keep-alive, or surplus idle connections.
245245
for connection in list(self._connections):
246246
if connection.is_closed():
247247
# log: "removing closed connection"
248248
self._connections.remove(connection)
249-
idling_connections.discard(connection)
249+
idling_count -= int(connection.is_idle())
250250
elif connection.has_expired():
251251
# log: "closing expired connection"
252252
self._connections.remove(connection)
253-
idling_connections.discard(connection)
254253
closing_connections.append(connection)
254+
idling_count -= int(connection.is_idle())
255255
elif (
256-
connection.is_idle()
257-
and len(idling_connections) > self._max_keepalive_connections
256+
connection.is_idle() and idling_count > self._max_keepalive_connections
258257
):
259258
# log: "closing idle connection"
260259
self._connections.remove(connection)
261-
idling_connections.discard(connection)
262260
closing_connections.append(connection)
261+
idling_count -= 1
263262

264263
# Assign queued requests to connections.
265264
queued_requests = [request for request in self._requests if request.is_queued()]

0 commit comments

Comments
 (0)
Please sign in to comment.