Skip to content

Commit 55dad25

Browse files
Optimize connection pool loop check
1 parent da86ca4 commit 55dad25

File tree

2 files changed

+42
-38
lines changed

2 files changed

+42
-38
lines changed

httpcore/_async/connection_pool.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
238238
those connections to be handled seperately.
239239
"""
240240
closing_connections = []
241+
idling_count = 0
241242

242243
# First we handle cleaning up any connections that are closed,
243244
# have expired their keep-alive, or surplus idle connections.
@@ -249,27 +250,25 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
249250
# log: "closing expired connection"
250251
self._connections.remove(connection)
251252
closing_connections.append(connection)
252-
elif (
253-
connection.is_idle()
254-
and len([connection.is_idle() for connection in self._connections])
255-
> self._max_keepalive_connections
256-
):
253+
elif connection.is_idle():
254+
if idling_count < self._max_keepalive_connections:
255+
idling_count += 1
256+
continue
257257
# log: "closing idle connection"
258258
self._connections.remove(connection)
259259
closing_connections.append(connection)
260260

261261
# Assign queued requests to connections.
262-
queued_requests = [request for request in self._requests if request.is_queued()]
263-
for pool_request in queued_requests:
262+
for pool_request in list(self._requests):
263+
if not pool_request.is_queued():
264+
continue
265+
264266
origin = pool_request.request.url.origin
265267
available_connections = [
266268
connection
267269
for connection in self._connections
268270
if connection.can_handle_request(origin) and connection.is_available()
269271
]
270-
idle_connections = [
271-
connection for connection in self._connections if connection.is_idle()
272-
]
273272

274273
# There are three cases for how we may be able to handle the request:
275274
#
@@ -286,15 +285,18 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
286285
connection = self.create_connection(origin)
287286
self._connections.append(connection)
288287
pool_request.assign_to_connection(connection)
289-
elif idle_connections:
290-
# log: "closing idle connection"
291-
connection = idle_connections[0]
292-
self._connections.remove(connection)
293-
closing_connections.append(connection)
294-
# log: "creating new connection"
295-
connection = self.create_connection(origin)
296-
self._connections.append(connection)
297-
pool_request.assign_to_connection(connection)
288+
else:
289+
idling_connection = next(
290+
(c for c in self._connections if c.is_idle()), None
291+
)
292+
if idling_connection is not None:
293+
# log: "closing idle connection"
294+
self._connections.remove(idling_connection)
295+
closing_connections.append(idling_connection)
296+
# log: "creating new connection"
297+
new_connection = self.create_connection(origin)
298+
self._connections.append(new_connection)
299+
pool_request.assign_to_connection(new_connection)
298300

299301
return closing_connections
300302

httpcore/_sync/connection_pool.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def _assign_requests_to_connections(self) -> List[ConnectionInterface]:
238238
those connections to be handled seperately.
239239
"""
240240
closing_connections = []
241+
idling_count = 0
241242

242243
# First we handle cleaning up any connections that are closed,
243244
# have expired their keep-alive, or surplus idle connections.
@@ -249,27 +250,25 @@ def _assign_requests_to_connections(self) -> List[ConnectionInterface]:
249250
# log: "closing expired connection"
250251
self._connections.remove(connection)
251252
closing_connections.append(connection)
252-
elif (
253-
connection.is_idle()
254-
and len([connection.is_idle() for connection in self._connections])
255-
> self._max_keepalive_connections
256-
):
253+
elif connection.is_idle():
254+
if idling_count < self._max_keepalive_connections:
255+
idling_count += 1
256+
continue
257257
# log: "closing idle connection"
258258
self._connections.remove(connection)
259259
closing_connections.append(connection)
260260

261261
# Assign queued requests to connections.
262-
queued_requests = [request for request in self._requests if request.is_queued()]
263-
for pool_request in queued_requests:
262+
for pool_request in list(self._requests):
263+
if not pool_request.is_queued():
264+
continue
265+
264266
origin = pool_request.request.url.origin
265267
available_connections = [
266268
connection
267269
for connection in self._connections
268270
if connection.can_handle_request(origin) and connection.is_available()
269271
]
270-
idle_connections = [
271-
connection for connection in self._connections if connection.is_idle()
272-
]
273272

274273
# There are three cases for how we may be able to handle the request:
275274
#
@@ -286,15 +285,18 @@ def _assign_requests_to_connections(self) -> List[ConnectionInterface]:
286285
connection = self.create_connection(origin)
287286
self._connections.append(connection)
288287
pool_request.assign_to_connection(connection)
289-
elif idle_connections:
290-
# log: "closing idle connection"
291-
connection = idle_connections[0]
292-
self._connections.remove(connection)
293-
closing_connections.append(connection)
294-
# log: "creating new connection"
295-
connection = self.create_connection(origin)
296-
self._connections.append(connection)
297-
pool_request.assign_to_connection(connection)
288+
else:
289+
idling_connection = next(
290+
(c for c in self._connections if c.is_idle()), None
291+
)
292+
if idling_connection is not None:
293+
# log: "closing idle connection"
294+
self._connections.remove(idling_connection)
295+
closing_connections.append(idling_connection)
296+
# log: "creating new connection"
297+
new_connection = self.create_connection(origin)
298+
self._connections.append(new_connection)
299+
pool_request.assign_to_connection(new_connection)
298300

299301
return closing_connections
300302

0 commit comments

Comments
 (0)