Skip to content

Commit da9636b

Browse files
Take first available
1 parent 55dad25 commit da9636b

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

httpcore/_async/connection_pool.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import ssl
22
import sys
33
from types import TracebackType
4-
from typing import AsyncIterable, AsyncIterator, Iterable, List, Optional, Type
4+
from typing import (
5+
AsyncIterable,
6+
AsyncIterator,
7+
Iterable,
8+
Iterator,
9+
List,
10+
Optional,
11+
Type,
12+
)
513

614
from .._backends.auto import AutoBackend
715
from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend
@@ -264,22 +272,22 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
264272
continue
265273

266274
origin = pool_request.request.url.origin
267-
available_connections = [
275+
available_connections_iter: Iterator[AsyncConnectionInterface] = (
268276
connection
269277
for connection in self._connections
270278
if connection.can_handle_request(origin) and connection.is_available()
271-
]
279+
)
280+
available_connection = next(available_connections_iter, None)
272281

273282
# There are three cases for how we may be able to handle the request:
274283
#
275284
# 1. There is an existing connection that can handle the request.
276285
# 2. We can create a new connection to handle the request.
277286
# 3. We can close an idle connection and then create a new connection
278287
# to handle the request.
279-
if available_connections:
288+
if available_connection is not None:
280289
# log: "reusing existing connection"
281-
connection = available_connections[0]
282-
pool_request.assign_to_connection(connection)
290+
pool_request.assign_to_connection(available_connection)
283291
elif len(self._connections) < self._max_connections:
284292
# log: "creating new connection"
285293
connection = self.create_connection(origin)

httpcore/_sync/connection_pool.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import ssl
22
import sys
33
from types import TracebackType
4-
from typing import Iterable, Iterator, Iterable, List, Optional, Type
4+
from typing import (
5+
Iterable,
6+
Iterator,
7+
Iterable,
8+
Iterator,
9+
List,
10+
Optional,
11+
Type,
12+
)
513

614
from .._backends.sync import SyncBackend
715
from .._backends.base import SOCKET_OPTION, NetworkBackend
@@ -264,22 +272,22 @@ def _assign_requests_to_connections(self) -> List[ConnectionInterface]:
264272
continue
265273

266274
origin = pool_request.request.url.origin
267-
available_connections = [
275+
available_connections_iter: Iterator[ConnectionInterface] = (
268276
connection
269277
for connection in self._connections
270278
if connection.can_handle_request(origin) and connection.is_available()
271-
]
279+
)
280+
available_connection = next(available_connections_iter, None)
272281

273282
# There are three cases for how we may be able to handle the request:
274283
#
275284
# 1. There is an existing connection that can handle the request.
276285
# 2. We can create a new connection to handle the request.
277286
# 3. We can close an idle connection and then create a new connection
278287
# to handle the request.
279-
if available_connections:
288+
if available_connection is not None:
280289
# log: "reusing existing connection"
281-
connection = available_connections[0]
282-
pool_request.assign_to_connection(connection)
290+
pool_request.assign_to_connection(available_connection)
283291
elif len(self._connections) < self._max_connections:
284292
# log: "creating new connection"
285293
connection = self.create_connection(origin)

0 commit comments

Comments
 (0)