Skip to content

Commit

Permalink
avoid dual-loops
Browse files Browse the repository at this point in the history
  • Loading branch information
T-256 authored Jun 12, 2024
1 parent 0cc429e commit 07c0660
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: Test Suite

on:
push:
branches: ["master"]
branches: ["master", "test-optimize"]
pull_request:
branches: ["master"]
branches: ["master", "test-optimize"]

jobs:
tests:
Expand Down
24 changes: 12 additions & 12 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,31 +238,31 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
those connections to be handled seperately.
"""
closing_connections = []
idling_count = sum(1 for c in self._connections if c.is_idle())
idling_count = 0

# First we handle cleaning up any connections that are closed,
# have expired their keep-alive, or surplus idle connections.
for connection in list(self._connections):
if connection.is_closed():
# log: "removing closed connection"
self._connections.remove(connection)
idling_count -= int(connection.is_idle())
elif connection.has_expired():
# log: "closing expired connection"
self._connections.remove(connection)
closing_connections.append(connection)
idling_count -= int(connection.is_idle())
elif (
connection.is_idle() and idling_count > self._max_keepalive_connections
):
# log: "closing idle connection"
self._connections.remove(connection)
closing_connections.append(connection)
idling_count -= 1
elif connection.is_idle():
if idling_count > self._max_keepalive_connections:
# log: "closing idle connection"
self._connections.remove(connection)
closing_connections.append(connection)
else:
idling_count += 1

# Assign queued requests to connections.
queued_requests = [request for request in self._requests if request.is_queued()]
for pool_request in queued_requests:
for pool_request in list(self._requests):
if not pool_request.is_queued():
continue

origin = pool_request.request.url.origin
available_connections = [
connection
Expand Down
24 changes: 12 additions & 12 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,31 +238,31 @@ def _assign_requests_to_connections(self) -> List[ConnectionInterface]:
those connections to be handled seperately.
"""
closing_connections = []
idling_count = sum(1 for c in self._connections if c.is_idle())
idling_count = 0

# First we handle cleaning up any connections that are closed,
# have expired their keep-alive, or surplus idle connections.
for connection in list(self._connections):
if connection.is_closed():
# log: "removing closed connection"
self._connections.remove(connection)
idling_count -= int(connection.is_idle())
elif connection.has_expired():
# log: "closing expired connection"
self._connections.remove(connection)
closing_connections.append(connection)
idling_count -= int(connection.is_idle())
elif (
connection.is_idle() and idling_count > self._max_keepalive_connections
):
# log: "closing idle connection"
self._connections.remove(connection)
closing_connections.append(connection)
idling_count -= 1
elif connection.is_idle():
if idling_count > self._max_keepalive_connections:
# log: "closing idle connection"
self._connections.remove(connection)
closing_connections.append(connection)
else:
idling_count += 1

# Assign queued requests to connections.
queued_requests = [request for request in self._requests if request.is_queued()]
for pool_request in queued_requests:
for pool_request in list(self._requests):
if not pool_request.is_queued():
continue

origin = pool_request.request.url.origin
available_connections = [
connection
Expand Down

0 comments on commit 07c0660

Please sign in to comment.