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)
```
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(
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:
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,
)
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,
)
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(
[
**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.",
+ )
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})"
)
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,
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():