|
1 | 1 | import ssl |
2 | 2 | import sys |
3 | 3 | 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 | +) |
5 | 13 |
|
6 | 14 | from .._backends.auto import AutoBackend |
7 | 15 | from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend |
@@ -264,22 +272,22 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]: |
264 | 272 | continue |
265 | 273 |
|
266 | 274 | origin = pool_request.request.url.origin |
267 | | - available_connections = [ |
| 275 | + available_connections_iter: Iterator[AsyncConnectionInterface] = ( |
268 | 276 | connection |
269 | 277 | for connection in self._connections |
270 | 278 | if connection.can_handle_request(origin) and connection.is_available() |
271 | | - ] |
| 279 | + ) |
| 280 | + available_connection = next(available_connections_iter, None) |
272 | 281 |
|
273 | 282 | # There are three cases for how we may be able to handle the request: |
274 | 283 | # |
275 | 284 | # 1. There is an existing connection that can handle the request. |
276 | 285 | # 2. We can create a new connection to handle the request. |
277 | 286 | # 3. We can close an idle connection and then create a new connection |
278 | 287 | # to handle the request. |
279 | | - if available_connections: |
| 288 | + if available_connection is not None: |
280 | 289 | # log: "reusing existing connection" |
281 | | - connection = available_connections[0] |
282 | | - pool_request.assign_to_connection(connection) |
| 290 | + pool_request.assign_to_connection(available_connection) |
283 | 291 | elif len(self._connections) < self._max_connections: |
284 | 292 | # log: "creating new connection" |
285 | 293 | connection = self.create_connection(origin) |
|
0 commit comments