Skip to content

Commit

Permalink
max_connections should be optional (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie authored Nov 12, 2021
1 parent e2ddf09 commit fd2d865
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
19 changes: 12 additions & 7 deletions httpcore/_async/connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ssl
import sys
from types import TracebackType
from typing import AsyncIterable, AsyncIterator, List, Optional, Type

Expand Down Expand Up @@ -44,7 +45,7 @@ class AsyncConnectionPool(AsyncRequestInterface):
def __init__(
self,
ssl_context: ssl.SSLContext = None,
max_connections: int = 10,
max_connections: Optional[int] = 10,
max_keepalive_connections: int = None,
keepalive_expiry: float = None,
http1: bool = True,
Expand Down Expand Up @@ -81,17 +82,21 @@ def __init__(
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
network_backend: A backend instance to use for handling network I/O.
"""
if max_keepalive_connections is None:
max_keepalive_connections = max_connections

if ssl_context is None:
ssl_context = default_ssl_context()

self._ssl_context = ssl_context

self._max_connections = max_connections
self._max_connections = (
sys.maxsize if max_connections is None else max_connections
)
self._max_keepalive_connections = (
sys.maxsize
if max_keepalive_connections is None
else max_keepalive_connections
)
self._max_keepalive_connections = min(
max_keepalive_connections, max_connections
self._max_connections, self._max_keepalive_connections
)

self._keepalive_expiry = keepalive_expiry
Expand Down Expand Up @@ -209,7 +214,7 @@ async def handle_async_request(self, request: Request) -> Response:
)
if scheme not in ("http", "https"):
raise UnsupportedProtocol(
"Request URL has an unsupported protocol '{scheme}://'."
f"Request URL has an unsupported protocol '{scheme}://'."
)

status = RequestStatus(request)
Expand Down
4 changes: 2 additions & 2 deletions httpcore/_async/http_proxy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ssl
from typing import List, Mapping, Sequence, Tuple, Union
from typing import List, Mapping, Optional, Sequence, Tuple, Union

from .._exceptions import ProxyError
from .._models import URL, Origin, Request, Response, enforce_headers, enforce_url
Expand Down Expand Up @@ -44,7 +44,7 @@ def __init__(
proxy_url: Union[URL, bytes, str],
proxy_headers: Union[HeadersAsMapping, HeadersAsSequence] = None,
ssl_context: ssl.SSLContext = None,
max_connections: int = 10,
max_connections: Optional[int] = 10,
max_keepalive_connections: int = None,
keepalive_expiry: float = None,
retries: int = 0,
Expand Down
19 changes: 12 additions & 7 deletions httpcore/_sync/connection_pool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ssl
import sys
from types import TracebackType
from typing import Iterable, Iterator, List, Optional, Type

Expand Down Expand Up @@ -44,7 +45,7 @@ class ConnectionPool(RequestInterface):
def __init__(
self,
ssl_context: ssl.SSLContext = None,
max_connections: int = 10,
max_connections: Optional[int] = 10,
max_keepalive_connections: int = None,
keepalive_expiry: float = None,
http1: bool = True,
Expand Down Expand Up @@ -81,17 +82,21 @@ def __init__(
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
network_backend: A backend instance to use for handling network I/O.
"""
if max_keepalive_connections is None:
max_keepalive_connections = max_connections

if ssl_context is None:
ssl_context = default_ssl_context()

self._ssl_context = ssl_context

self._max_connections = max_connections
self._max_connections = (
sys.maxsize if max_connections is None else max_connections
)
self._max_keepalive_connections = (
sys.maxsize
if max_keepalive_connections is None
else max_keepalive_connections
)
self._max_keepalive_connections = min(
max_keepalive_connections, max_connections
self._max_connections, self._max_keepalive_connections
)

self._keepalive_expiry = keepalive_expiry
Expand Down Expand Up @@ -209,7 +214,7 @@ def handle_request(self, request: Request) -> Response:
)
if scheme not in ("http", "https"):
raise UnsupportedProtocol(
"Request URL has an unsupported protocol '{scheme}://'."
f"Request URL has an unsupported protocol '{scheme}://'."
)

status = RequestStatus(request)
Expand Down
4 changes: 2 additions & 2 deletions httpcore/_sync/http_proxy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ssl
from typing import List, Mapping, Sequence, Tuple, Union
from typing import List, Mapping, Optional, Sequence, Tuple, Union

from .._exceptions import ProxyError
from .._models import URL, Origin, Request, Response, enforce_headers, enforce_url
Expand Down Expand Up @@ -44,7 +44,7 @@ def __init__(
proxy_url: Union[URL, bytes, str],
proxy_headers: Union[HeadersAsMapping, HeadersAsSequence] = None,
ssl_context: ssl.SSLContext = None,
max_connections: int = 10,
max_connections: Optional[int] = 10,
max_keepalive_connections: int = None,
keepalive_expiry: float = None,
retries: int = 0,
Expand Down

0 comments on commit fd2d865

Please sign in to comment.