Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename pool limit options #968

Merged
merged 12 commits into from
May 21, 2020
6 changes: 3 additions & 3 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,14 @@ response = client.get('http://example.com/')
You can control the connection pool size using the `pool_limits` keyword
argument on the client. It takes instances of `httpx.PoolLimits` which define:

- `soft_limit`, number of allowable keep-alive connections, or `None` to always
- `max_keepalive`, number of allowable keep-alive connections, or `None` to always
allow. (Defaults 10)
- `hard_limit`, maximum number of allowable connections, or` None` for no limits.
- `max_connections`, maximum number of allowable connections, or` None` for no limits.
(Default 100)


```python
limits = httpx.PoolLimits(soft_limit=5, hard_limit=10)
limits = httpx.PoolLimits(max_keepalive=5, max_connections=10)
client = httpx.Client(pool_limits=limits)
```

Expand Down
24 changes: 8 additions & 16 deletions httpx/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,11 @@ def init_transport(
ssl_context = SSLConfig(
verify=verify, cert=cert, trust_env=trust_env
).ssl_context
max_keepalive = pool_limits.soft_limit
max_connections = pool_limits.hard_limit

return httpcore.SyncConnectionPool(
ssl_context=ssl_context,
max_keepalive=max_keepalive,
max_connections=max_connections,
max_keepalive=pool_limits.max_keepalive,
max_connections=pool_limits.max_connections,
)

def init_proxy_transport(
Expand All @@ -524,16 +522,14 @@ def init_proxy_transport(
ssl_context = SSLConfig(
verify=verify, cert=cert, trust_env=trust_env
).ssl_context
max_keepalive = pool_limits.soft_limit
max_connections = pool_limits.hard_limit

return httpcore.SyncHTTPProxy(
proxy_url=proxy.url.raw,
proxy_headers=proxy.headers.raw,
proxy_mode=proxy.mode,
ssl_context=ssl_context,
max_keepalive=max_keepalive,
max_connections=max_connections,
max_keepalive=pool_limits.max_keepalive,
max_connections=pool_limits.max_connections,
)

def transport_for_url(self, url: URL) -> httpcore.SyncHTTPTransport:
Expand Down Expand Up @@ -1050,13 +1046,11 @@ def init_transport(
ssl_context = SSLConfig(
verify=verify, cert=cert, trust_env=trust_env
).ssl_context
max_keepalive = pool_limits.soft_limit
max_connections = pool_limits.hard_limit

return httpcore.AsyncConnectionPool(
ssl_context=ssl_context,
max_keepalive=max_keepalive,
max_connections=max_connections,
max_keepalive=pool_limits.max_keepalive,
max_connections=pool_limits.max_connections,
http2=http2,
)

Expand All @@ -1072,16 +1066,14 @@ def init_proxy_transport(
ssl_context = SSLConfig(
verify=verify, cert=cert, trust_env=trust_env
).ssl_context
max_keepalive = pool_limits.soft_limit
max_connections = pool_limits.hard_limit

return httpcore.AsyncHTTPProxy(
proxy_url=proxy.url.raw,
proxy_headers=proxy.headers.raw,
proxy_mode=proxy.mode,
ssl_context=ssl_context,
max_keepalive=max_keepalive,
max_connections=max_connections,
max_keepalive=pool_limits.max_keepalive,
max_connections=pool_limits.max_connections,
http2=http2,
)

Expand Down
32 changes: 23 additions & 9 deletions httpx/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ._models import URL, Headers
from ._types import CertTypes, HeaderTypes, TimeoutTypes, URLTypes, VerifyTypes
from ._utils import get_ca_bundle_from_env, get_logger
from ._utils import get_ca_bundle_from_env, get_logger, warn_deprecated

DEFAULT_CIPHERS = ":".join(
[
Expand Down Expand Up @@ -288,29 +288,43 @@ class PoolLimits:

**Parameters:**

* **soft_limit** - Allow the connection pool to maintain keep-alive connections
* **max_keepalive** - Allow the connection pool to maintain keep-alive connections
below this point.
* **hard_limit** - The maximum number of concurrent connections that may be
* **max_connections** - The maximum number of concurrent connections that may be
established.
"""

def __init__(
self, *, soft_limit: int = None, hard_limit: int = None,
self,
*,
max_keepalive: int = None,
max_connections: int = None,
soft_limit: int = None,
hard_limit: int = None,
):
self.soft_limit = soft_limit
self.hard_limit = hard_limit
self.max_keepalive = max_keepalive
self.max_connections = max_connections
if soft_limit is not None: # pragma: nocover
self.max_keepalive = soft_limit
warn_deprecated("'soft_limit' is deprecated. Use 'max_keepalive' instead.",)
if hard_limit is not None: # pragma: nocover
self.max_connections = hard_limit
warn_deprecated(
"'hard_limit' is deprecated. Use 'max_connections' instead.",
)
yeraydiazdiaz marked this conversation as resolved.
Show resolved Hide resolved
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved

def __eq__(self, other: typing.Any) -> bool:
return (
isinstance(other, self.__class__)
and self.soft_limit == other.soft_limit
and self.hard_limit == other.hard_limit
and self.max_keepalive == other.max_keepalive
and self.max_connections == other.max_connections
)

def __repr__(self) -> str:
class_name = self.__class__.__name__
return (
f"{class_name}(soft_limit={self.soft_limit}, hard_limit={self.hard_limit})"
f"{class_name}(max_keepalive={self.max_keepalive}, "
f"max_connections={self.max_connections})"
)


Expand Down
16 changes: 8 additions & 8 deletions httpx/_transports/urllib3.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ def __init__(
ssl_config = SSLConfig(
verify=verify, cert=cert, trust_env=trust_env, http2=False
)
hard_limit = pool_limits.hard_limit
soft_limit = pool_limits.soft_limit
max_connections = pool_limits.max_connections
max_keepalive = pool_limits.max_keepalive

# Our connection pool configuration doesn't quite match up with urllib3's
# controls, but we can put sensible mappings in place:
if hard_limit is None:
if max_connections is None:
block = False
if soft_limit is None:
if max_keepalive is None:
num_pools = 1000
maxsize = 1000
else:
num_pools = int(math.sqrt(soft_limit))
maxsize = int(math.sqrt(soft_limit))
num_pools = int(math.sqrt(max_keepalive))
maxsize = int(math.sqrt(max_keepalive))
else:
block = True
num_pools = int(math.sqrt(hard_limit))
maxsize = int(math.sqrt(hard_limit))
num_pools = int(math.sqrt(max_connections))
maxsize = int(math.sqrt(max_connections))

self.pool = self.init_pool_manager(
proxy=proxy,
Expand Down
8 changes: 4 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ def test_ssl_eq():


def test_limits_repr():
limits = httpx.PoolLimits(hard_limit=100)
assert repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100)"
limits = httpx.PoolLimits(max_connections=100)
assert repr(limits) == "PoolLimits(max_keepalive=None, max_connections=100)"


def test_limits_eq():
limits = httpx.PoolLimits(hard_limit=100)
assert limits == httpx.PoolLimits(hard_limit=100)
limits = httpx.PoolLimits(max_connections=100)
assert limits == httpx.PoolLimits(max_connections=100)


def test_timeout_eq():
Expand Down