Skip to content

Commit fd2d865

Browse files
max_connections should be optional (#429)
1 parent e2ddf09 commit fd2d865

4 files changed

Lines changed: 28 additions & 18 deletions

File tree

httpcore/_async/connection_pool.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ssl
2+
import sys
23
from types import TracebackType
34
from typing import AsyncIterable, AsyncIterator, List, Optional, Type
45

@@ -44,7 +45,7 @@ class AsyncConnectionPool(AsyncRequestInterface):
4445
def __init__(
4546
self,
4647
ssl_context: ssl.SSLContext = None,
47-
max_connections: int = 10,
48+
max_connections: Optional[int] = 10,
4849
max_keepalive_connections: int = None,
4950
keepalive_expiry: float = None,
5051
http1: bool = True,
@@ -81,17 +82,21 @@ def __init__(
8182
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
8283
network_backend: A backend instance to use for handling network I/O.
8384
"""
84-
if max_keepalive_connections is None:
85-
max_keepalive_connections = max_connections
86-
8785
if ssl_context is None:
8886
ssl_context = default_ssl_context()
8987

9088
self._ssl_context = ssl_context
9189

92-
self._max_connections = max_connections
90+
self._max_connections = (
91+
sys.maxsize if max_connections is None else max_connections
92+
)
93+
self._max_keepalive_connections = (
94+
sys.maxsize
95+
if max_keepalive_connections is None
96+
else max_keepalive_connections
97+
)
9398
self._max_keepalive_connections = min(
94-
max_keepalive_connections, max_connections
99+
self._max_connections, self._max_keepalive_connections
95100
)
96101

97102
self._keepalive_expiry = keepalive_expiry
@@ -209,7 +214,7 @@ async def handle_async_request(self, request: Request) -> Response:
209214
)
210215
if scheme not in ("http", "https"):
211216
raise UnsupportedProtocol(
212-
"Request URL has an unsupported protocol '{scheme}://'."
217+
f"Request URL has an unsupported protocol '{scheme}://'."
213218
)
214219

215220
status = RequestStatus(request)

httpcore/_async/http_proxy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ssl
2-
from typing import List, Mapping, Sequence, Tuple, Union
2+
from typing import List, Mapping, Optional, Sequence, Tuple, Union
33

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

httpcore/_sync/connection_pool.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ssl
2+
import sys
23
from types import TracebackType
34
from typing import Iterable, Iterator, List, Optional, Type
45

@@ -44,7 +45,7 @@ class ConnectionPool(RequestInterface):
4445
def __init__(
4546
self,
4647
ssl_context: ssl.SSLContext = None,
47-
max_connections: int = 10,
48+
max_connections: Optional[int] = 10,
4849
max_keepalive_connections: int = None,
4950
keepalive_expiry: float = None,
5051
http1: bool = True,
@@ -81,17 +82,21 @@ def __init__(
8182
uds: Path to a Unix Domain Socket to use instead of TCP sockets.
8283
network_backend: A backend instance to use for handling network I/O.
8384
"""
84-
if max_keepalive_connections is None:
85-
max_keepalive_connections = max_connections
86-
8785
if ssl_context is None:
8886
ssl_context = default_ssl_context()
8987

9088
self._ssl_context = ssl_context
9189

92-
self._max_connections = max_connections
90+
self._max_connections = (
91+
sys.maxsize if max_connections is None else max_connections
92+
)
93+
self._max_keepalive_connections = (
94+
sys.maxsize
95+
if max_keepalive_connections is None
96+
else max_keepalive_connections
97+
)
9398
self._max_keepalive_connections = min(
94-
max_keepalive_connections, max_connections
99+
self._max_connections, self._max_keepalive_connections
95100
)
96101

97102
self._keepalive_expiry = keepalive_expiry
@@ -209,7 +214,7 @@ def handle_request(self, request: Request) -> Response:
209214
)
210215
if scheme not in ("http", "https"):
211216
raise UnsupportedProtocol(
212-
"Request URL has an unsupported protocol '{scheme}://'."
217+
f"Request URL has an unsupported protocol '{scheme}://'."
213218
)
214219

215220
status = RequestStatus(request)

httpcore/_sync/http_proxy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ssl
2-
from typing import List, Mapping, Sequence, Tuple, Union
2+
from typing import List, Mapping, Optional, Sequence, Tuple, Union
33

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

0 commit comments

Comments
 (0)