11import ssl
22import sys
33from types import TracebackType
4- from typing import AsyncIterable , AsyncIterator , Iterable , List , Optional , Type
4+ from typing import (
5+ AsyncIterable ,
6+ AsyncIterator ,
7+ Iterable ,
8+ List ,
9+ Optional ,
10+ Type ,
11+ )
512
613from .._backends .auto import AutoBackend
714from .._backends .base import SOCKET_OPTION , AsyncNetworkBackend
@@ -238,6 +245,7 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
238245 those connections to be handled seperately.
239246 """
240247 closing_connections = []
248+ idling_count = 0
241249
242250 # First we handle cleaning up any connections that are closed,
243251 # have expired their keep-alive, or surplus idle connections.
@@ -249,52 +257,56 @@ def _assign_requests_to_connections(self) -> List[AsyncConnectionInterface]:
249257 # log: "closing expired connection"
250258 self ._connections .remove (connection )
251259 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- ):
260+ elif connection .is_idle ():
261+ if idling_count < self ._max_keepalive_connections :
262+ idling_count += 1
263+ continue
257264 # log: "closing idle connection"
258265 self ._connections .remove (connection )
259266 closing_connections .append (connection )
260267
261268 # 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 :
269+ for pool_request in list (self ._requests ):
270+ if not pool_request .is_queued ():
271+ continue
272+
264273 origin = pool_request .request .url .origin
265- available_connections = [
266- connection
267- for connection in self ._connections
268- if connection .can_handle_request (origin ) and connection .is_available ()
269- ]
270- idle_connections = [
271- connection for connection in self ._connections if connection .is_idle ()
272- ]
274+ available_connection = next (
275+ (
276+ connection
277+ for connection in self ._connections
278+ if connection .can_handle_request (origin )
279+ and connection .is_available ()
280+ ),
281+ None ,
282+ )
273283
274284 # There are three cases for how we may be able to handle the request:
275285 #
276286 # 1. There is an existing connection that can handle the request.
277287 # 2. We can create a new connection to handle the request.
278288 # 3. We can close an idle connection and then create a new connection
279289 # to handle the request.
280- if available_connections :
290+ if available_connection is not None :
281291 # log: "reusing existing connection"
282- connection = available_connections [0 ]
283- pool_request .assign_to_connection (connection )
292+ pool_request .assign_to_connection (available_connection )
284293 elif len (self ._connections ) < self ._max_connections :
285294 # log: "creating new connection"
286295 connection = self .create_connection (origin )
287296 self ._connections .append (connection )
288297 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 )
298+ else :
299+ idling_connection = next (
300+ (c for c in self ._connections if c .is_idle ()), None
301+ )
302+ if idling_connection is not None :
303+ # log: "closing idle connection"
304+ self ._connections .remove (idling_connection )
305+ closing_connections .append (idling_connection )
306+ # log: "creating new connection"
307+ new_connection = self .create_connection (origin )
308+ self ._connections .append (new_connection )
309+ pool_request .assign_to_connection (new_connection )
298310
299311 return closing_connections
300312
0 commit comments