Skip to content

Commit bb3cbe1

Browse files
Improve idle check by code review suggestion
1 parent 2c6f235 commit bb3cbe1

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

httpcore/_async/connection_pool.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -238,31 +238,31 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
238238
those connections to be handled seperately.
239239
"""
240240
closing_connections = []
241-
idling_count = sum(1 for c in self._connections if c.is_idle())
241+
idling_count = 0
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_count -= int(connection.is_idle())
250249
elif connection.has_expired():
251250
# log: "closing expired connection"
252251
self._connections.remove(connection)
253252
closing_connections.append(connection)
254-
idling_count -= int(connection.is_idle())
255-
elif (
256-
connection.is_idle() and idling_count > self._max_keepalive_connections
257-
):
253+
elif connection.is_idle():
254+
if idling_count < self._max_keepalive_connections:
255+
idling_count += 1
256+
continue
258257
# log: "closing idle connection"
259258
self._connections.remove(connection)
260259
closing_connections.append(connection)
261-
idling_count -= 1
262260

263261
# Assign queued requests to connections.
264-
queued_requests = [request for request in self._requests if request.is_queued()]
265-
for pool_request in queued_requests:
262+
for pool_request in list(self._requests):
263+
if not pool_request.is_queued():
264+
continue
265+
266266
origin = pool_request.request.url.origin
267267
available_connections = [
268268
connection

httpcore/_sync/connection_pool.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -238,31 +238,31 @@ def _assign_requests_to_connections(self) -> List[ConnectionInterface]:
238238
those connections to be handled seperately.
239239
"""
240240
closing_connections = []
241-
idling_count = sum(1 for c in self._connections if c.is_idle())
241+
idling_count = 0
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_count -= int(connection.is_idle())
250249
elif connection.has_expired():
251250
# log: "closing expired connection"
252251
self._connections.remove(connection)
253252
closing_connections.append(connection)
254-
idling_count -= int(connection.is_idle())
255-
elif (
256-
connection.is_idle() and idling_count > self._max_keepalive_connections
257-
):
253+
elif connection.is_idle():
254+
if idling_count < self._max_keepalive_connections:
255+
idling_count += 1
256+
continue
258257
# log: "closing idle connection"
259258
self._connections.remove(connection)
260259
closing_connections.append(connection)
261-
idling_count -= 1
262260

263261
# Assign queued requests to connections.
264-
queued_requests = [request for request in self._requests if request.is_queued()]
265-
for pool_request in queued_requests:
262+
for pool_request in list(self._requests):
263+
if not pool_request.is_queued():
264+
continue
265+
266266
origin = pool_request.request.url.origin
267267
available_connections = [
268268
connection

0 commit comments

Comments
 (0)