Skip to content

Commit

Permalink
Take first available
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusSintonen committed Jun 15, 2024
1 parent 55dad25 commit da9636b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
20 changes: 14 additions & 6 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import ssl
import sys
from types import TracebackType
from typing import AsyncIterable, AsyncIterator, Iterable, List, Optional, Type
from typing import (
AsyncIterable,
AsyncIterator,
Iterable,
Iterator,
List,
Optional,
Type,
)

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

origin = pool_request.request.url.origin
available_connections = [
available_connections_iter: Iterator[AsyncConnectionInterface] = (
connection
for connection in self._connections
if connection.can_handle_request(origin) and connection.is_available()
]
)
available_connection = next(available_connections_iter, None)

# There are three cases for how we may be able to handle the request:
#
# 1. There is an existing connection that can handle the request.
# 2. We can create a new connection to handle the request.
# 3. We can close an idle connection and then create a new connection
# to handle the request.
if available_connections:
if available_connection is not None:
# log: "reusing existing connection"
connection = available_connections[0]
pool_request.assign_to_connection(connection)
pool_request.assign_to_connection(available_connection)
elif len(self._connections) < self._max_connections:
# log: "creating new connection"
connection = self.create_connection(origin)
Expand Down
20 changes: 14 additions & 6 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import ssl
import sys
from types import TracebackType
from typing import Iterable, Iterator, Iterable, List, Optional, Type
from typing import (
Iterable,
Iterator,
Iterable,
Iterator,
List,
Optional,
Type,
)

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

origin = pool_request.request.url.origin
available_connections = [
available_connections_iter: Iterator[ConnectionInterface] = (
connection
for connection in self._connections
if connection.can_handle_request(origin) and connection.is_available()
]
)
available_connection = next(available_connections_iter, None)

# There are three cases for how we may be able to handle the request:
#
# 1. There is an existing connection that can handle the request.
# 2. We can create a new connection to handle the request.
# 3. We can close an idle connection and then create a new connection
# to handle the request.
if available_connections:
if available_connection is not None:
# log: "reusing existing connection"
connection = available_connections[0]
pool_request.assign_to_connection(connection)
pool_request.assign_to_connection(available_connection)
elif len(self._connections) < self._max_connections:
# log: "creating new connection"
connection = self.create_connection(origin)
Expand Down

0 comments on commit da9636b

Please sign in to comment.