Rename PoolLimits to Limits (#1113)
* Rename PoolLimits to Limits * Lint Co-authored-by: Tom Christie <tom@tomchristie.com>
This commit is contained in:
parent
26cd4f54e8
commit
f67e925f72
@ -401,8 +401,8 @@ response = client.get('http://example.com/')
|
||||
|
||||
## Pool limit configuration
|
||||
|
||||
You can control the connection pool size using the `pool_limits` keyword
|
||||
argument on the client. It takes instances of `httpx.PoolLimits` which define:
|
||||
You can control the connection pool size using the `limits` keyword
|
||||
argument on the client. It takes instances of `httpx.Limits` which define:
|
||||
|
||||
- `max_keepalive`, number of allowable keep-alive connections, or `None` to always
|
||||
allow. (Defaults 10)
|
||||
@ -411,8 +411,8 @@ allow. (Defaults 10)
|
||||
|
||||
|
||||
```python
|
||||
limits = httpx.PoolLimits(max_keepalive=5, max_connections=10)
|
||||
client = httpx.Client(pool_limits=limits)
|
||||
limits = httpx.Limits(max_keepalive=5, max_connections=10)
|
||||
client = httpx.Client(limits=limits)
|
||||
```
|
||||
|
||||
## Multipart file encoding
|
||||
|
||||
@ -2,7 +2,7 @@ from .__version__ import __description__, __title__, __version__
|
||||
from ._api import delete, get, head, options, patch, post, put, request, stream
|
||||
from ._auth import Auth, BasicAuth, DigestAuth
|
||||
from ._client import AsyncClient, Client
|
||||
from ._config import PoolLimits, Proxy, Timeout, create_ssl_context
|
||||
from ._config import Limits, PoolLimits, Proxy, Timeout, create_ssl_context
|
||||
from ._exceptions import (
|
||||
CloseError,
|
||||
ConnectError,
|
||||
@ -59,6 +59,7 @@ __all__ = [
|
||||
"BasicAuth",
|
||||
"Client",
|
||||
"DigestAuth",
|
||||
"Limits",
|
||||
"PoolLimits",
|
||||
"Proxy",
|
||||
"Timeout",
|
||||
|
||||
@ -7,11 +7,11 @@ import httpcore
|
||||
|
||||
from ._auth import Auth, BasicAuth, FunctionAuth
|
||||
from ._config import (
|
||||
DEFAULT_LIMITS,
|
||||
DEFAULT_MAX_REDIRECTS,
|
||||
DEFAULT_POOL_LIMITS,
|
||||
DEFAULT_TIMEOUT_CONFIG,
|
||||
UNSET,
|
||||
PoolLimits,
|
||||
Limits,
|
||||
Proxy,
|
||||
Timeout,
|
||||
UnsetType,
|
||||
@ -50,6 +50,7 @@ from ._utils import (
|
||||
get_logger,
|
||||
same_origin,
|
||||
should_not_be_proxied,
|
||||
warn_deprecated,
|
||||
)
|
||||
|
||||
logger = get_logger(__name__)
|
||||
@ -422,8 +423,7 @@ class Client(BaseClient):
|
||||
URLs.
|
||||
* **timeout** - *(optional)* The timeout configuration to use when sending
|
||||
requests.
|
||||
* **pool_limits** - *(optional)* The connection pool configuration to use
|
||||
when determining the maximum number of concurrently open HTTP connections.
|
||||
* **limits** - *(optional)* The limits configuration to use.
|
||||
* **max_redirects** - *(optional)* The maximum number of redirect responses
|
||||
that should be followed.
|
||||
* **base_url** - *(optional)* A URL to use as the base when building
|
||||
@ -448,7 +448,8 @@ class Client(BaseClient):
|
||||
http2: bool = False,
|
||||
proxies: ProxiesTypes = None,
|
||||
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
|
||||
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
|
||||
limits: Limits = DEFAULT_LIMITS,
|
||||
pool_limits: Limits = None,
|
||||
max_redirects: int = DEFAULT_MAX_REDIRECTS,
|
||||
base_url: URLTypes = None,
|
||||
transport: httpcore.SyncHTTPTransport = None,
|
||||
@ -466,13 +467,20 @@ class Client(BaseClient):
|
||||
trust_env=trust_env,
|
||||
)
|
||||
|
||||
if pool_limits is not None:
|
||||
warn_deprecated(
|
||||
"Client(..., pool_limits=...) is deprecated and will raise "
|
||||
"errors in the future. Use Client(..., limits=...) instead."
|
||||
)
|
||||
limits = pool_limits
|
||||
|
||||
proxy_map = self._get_proxy_map(proxies, trust_env)
|
||||
|
||||
self._transport = self._init_transport(
|
||||
verify=verify,
|
||||
cert=cert,
|
||||
http2=http2,
|
||||
pool_limits=pool_limits,
|
||||
limits=limits,
|
||||
transport=transport,
|
||||
app=app,
|
||||
trust_env=trust_env,
|
||||
@ -487,7 +495,7 @@ class Client(BaseClient):
|
||||
verify=verify,
|
||||
cert=cert,
|
||||
http2=http2,
|
||||
pool_limits=pool_limits,
|
||||
limits=limits,
|
||||
trust_env=trust_env,
|
||||
)
|
||||
for key, proxy in proxy_map.items()
|
||||
@ -499,7 +507,7 @@ class Client(BaseClient):
|
||||
verify: VerifyTypes = True,
|
||||
cert: CertTypes = None,
|
||||
http2: bool = False,
|
||||
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
|
||||
limits: Limits = DEFAULT_LIMITS,
|
||||
transport: httpcore.SyncHTTPTransport = None,
|
||||
app: typing.Callable = None,
|
||||
trust_env: bool = True,
|
||||
@ -514,8 +522,8 @@ class Client(BaseClient):
|
||||
|
||||
return httpcore.SyncConnectionPool(
|
||||
ssl_context=ssl_context,
|
||||
max_keepalive=pool_limits.max_keepalive,
|
||||
max_connections=pool_limits.max_connections,
|
||||
max_keepalive=limits.max_keepalive,
|
||||
max_connections=limits.max_connections,
|
||||
keepalive_expiry=KEEPALIVE_EXPIRY,
|
||||
http2=http2,
|
||||
)
|
||||
@ -526,7 +534,7 @@ class Client(BaseClient):
|
||||
verify: VerifyTypes = True,
|
||||
cert: CertTypes = None,
|
||||
http2: bool = False,
|
||||
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
|
||||
limits: Limits = DEFAULT_LIMITS,
|
||||
trust_env: bool = True,
|
||||
) -> httpcore.SyncHTTPTransport:
|
||||
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
|
||||
@ -536,8 +544,8 @@ class Client(BaseClient):
|
||||
proxy_headers=proxy.headers.raw,
|
||||
proxy_mode=proxy.mode,
|
||||
ssl_context=ssl_context,
|
||||
max_keepalive=pool_limits.max_keepalive,
|
||||
max_connections=pool_limits.max_connections,
|
||||
max_keepalive=limits.max_keepalive,
|
||||
max_connections=limits.max_connections,
|
||||
keepalive_expiry=KEEPALIVE_EXPIRY,
|
||||
http2=http2,
|
||||
)
|
||||
@ -946,8 +954,7 @@ class AsyncClient(BaseClient):
|
||||
URLs.
|
||||
* **timeout** - *(optional)* The timeout configuration to use when sending
|
||||
requests.
|
||||
* **pool_limits** - *(optional)* The connection pool configuration to use
|
||||
when determining the maximum number of concurrently open HTTP connections.
|
||||
* **limits** - *(optional)* The limits configuration to use.
|
||||
* **max_redirects** - *(optional)* The maximum number of redirect responses
|
||||
that should be followed.
|
||||
* **base_url** - *(optional)* A URL to use as the base when building
|
||||
@ -972,7 +979,8 @@ class AsyncClient(BaseClient):
|
||||
http2: bool = False,
|
||||
proxies: ProxiesTypes = None,
|
||||
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
|
||||
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
|
||||
limits: Limits = DEFAULT_LIMITS,
|
||||
pool_limits: Limits = None,
|
||||
max_redirects: int = DEFAULT_MAX_REDIRECTS,
|
||||
base_url: URLTypes = None,
|
||||
transport: httpcore.AsyncHTTPTransport = None,
|
||||
@ -990,13 +998,20 @@ class AsyncClient(BaseClient):
|
||||
trust_env=trust_env,
|
||||
)
|
||||
|
||||
if pool_limits is not None:
|
||||
warn_deprecated(
|
||||
"AsyncClient(..., pool_limits=...) is deprecated and will raise "
|
||||
"errors in the future. Use AsyncClient(..., limits=...) instead."
|
||||
)
|
||||
limits = pool_limits
|
||||
|
||||
proxy_map = self._get_proxy_map(proxies, trust_env)
|
||||
|
||||
self._transport = self._init_transport(
|
||||
verify=verify,
|
||||
cert=cert,
|
||||
http2=http2,
|
||||
pool_limits=pool_limits,
|
||||
limits=limits,
|
||||
transport=transport,
|
||||
app=app,
|
||||
trust_env=trust_env,
|
||||
@ -1011,7 +1026,7 @@ class AsyncClient(BaseClient):
|
||||
verify=verify,
|
||||
cert=cert,
|
||||
http2=http2,
|
||||
pool_limits=pool_limits,
|
||||
limits=limits,
|
||||
trust_env=trust_env,
|
||||
)
|
||||
for key, proxy in proxy_map.items()
|
||||
@ -1023,7 +1038,7 @@ class AsyncClient(BaseClient):
|
||||
verify: VerifyTypes = True,
|
||||
cert: CertTypes = None,
|
||||
http2: bool = False,
|
||||
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
|
||||
limits: Limits = DEFAULT_LIMITS,
|
||||
transport: httpcore.AsyncHTTPTransport = None,
|
||||
app: typing.Callable = None,
|
||||
trust_env: bool = True,
|
||||
@ -1038,8 +1053,8 @@ class AsyncClient(BaseClient):
|
||||
|
||||
return httpcore.AsyncConnectionPool(
|
||||
ssl_context=ssl_context,
|
||||
max_keepalive=pool_limits.max_keepalive,
|
||||
max_connections=pool_limits.max_connections,
|
||||
max_keepalive=limits.max_keepalive,
|
||||
max_connections=limits.max_connections,
|
||||
keepalive_expiry=KEEPALIVE_EXPIRY,
|
||||
http2=http2,
|
||||
)
|
||||
@ -1050,7 +1065,7 @@ class AsyncClient(BaseClient):
|
||||
verify: VerifyTypes = True,
|
||||
cert: CertTypes = None,
|
||||
http2: bool = False,
|
||||
pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
|
||||
limits: Limits = DEFAULT_LIMITS,
|
||||
trust_env: bool = True,
|
||||
) -> httpcore.AsyncHTTPTransport:
|
||||
ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
|
||||
@ -1060,8 +1075,8 @@ class AsyncClient(BaseClient):
|
||||
proxy_headers=proxy.headers.raw,
|
||||
proxy_mode=proxy.mode,
|
||||
ssl_context=ssl_context,
|
||||
max_keepalive=pool_limits.max_keepalive,
|
||||
max_connections=pool_limits.max_connections,
|
||||
max_keepalive=limits.max_keepalive,
|
||||
max_connections=limits.max_connections,
|
||||
keepalive_expiry=KEEPALIVE_EXPIRY,
|
||||
http2=http2,
|
||||
)
|
||||
|
||||
@ -317,9 +317,9 @@ class Timeout:
|
||||
)
|
||||
|
||||
|
||||
class PoolLimits:
|
||||
class Limits:
|
||||
"""
|
||||
Limits on the number of connections in a connection pool.
|
||||
Configuration for limits to various client behaviors.
|
||||
|
||||
**Parameters:**
|
||||
|
||||
@ -350,6 +350,17 @@ class PoolLimits:
|
||||
)
|
||||
|
||||
|
||||
class PoolLimits(Limits):
|
||||
def __init__(
|
||||
self, *, max_keepalive: int = None, max_connections: int = None,
|
||||
) -> None:
|
||||
warn_deprecated(
|
||||
"httpx.PoolLimits(...) is deprecated and will raise errors in the future. "
|
||||
"Use httpx.Limits(...) instead."
|
||||
)
|
||||
super().__init__(max_keepalive=max_keepalive, max_connections=max_connections)
|
||||
|
||||
|
||||
class Proxy:
|
||||
def __init__(
|
||||
self, url: URLTypes, *, headers: HeaderTypes = None, mode: str = "DEFAULT",
|
||||
@ -389,5 +400,5 @@ class Proxy:
|
||||
|
||||
|
||||
DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=5.0)
|
||||
DEFAULT_POOL_LIMITS = PoolLimits(max_keepalive=10, max_connections=100)
|
||||
DEFAULT_LIMITS = Limits(max_keepalive=10, max_connections=100)
|
||||
DEFAULT_MAX_REDIRECTS = 20
|
||||
|
||||
@ -194,3 +194,13 @@ def test_merge_url_hsts(url: str, scheme: str, is_ssl: bool):
|
||||
request = client.build_request("GET", url)
|
||||
assert request.url.scheme == scheme
|
||||
assert request.url.is_ssl == is_ssl
|
||||
|
||||
|
||||
def test_pool_limits_deprecated():
|
||||
limits = httpx.Limits()
|
||||
|
||||
with pytest.warns(DeprecationWarning):
|
||||
httpx.Client(pool_limits=limits)
|
||||
|
||||
with pytest.warns(DeprecationWarning):
|
||||
httpx.AsyncClient(pool_limits=limits)
|
||||
|
||||
@ -101,13 +101,18 @@ def test_create_ssl_context_with_get_request(server, cert_pem_file):
|
||||
|
||||
|
||||
def test_limits_repr():
|
||||
limits = httpx.PoolLimits(max_connections=100)
|
||||
assert repr(limits) == "PoolLimits(max_keepalive=None, max_connections=100)"
|
||||
limits = httpx.Limits(max_connections=100)
|
||||
assert repr(limits) == "Limits(max_keepalive=None, max_connections=100)"
|
||||
|
||||
|
||||
def test_limits_eq():
|
||||
limits = httpx.PoolLimits(max_connections=100)
|
||||
assert limits == httpx.PoolLimits(max_connections=100)
|
||||
limits = httpx.Limits(max_connections=100)
|
||||
assert limits == httpx.Limits(max_connections=100)
|
||||
|
||||
|
||||
def test_pool_limits_deprecated():
|
||||
with pytest.warns(DeprecationWarning):
|
||||
httpx.PoolLimits()
|
||||
|
||||
|
||||
def test_timeout_eq():
|
||||
|
||||
@ -34,10 +34,10 @@ async def test_connect_timeout(server):
|
||||
|
||||
@pytest.mark.usefixtures("async_environment")
|
||||
async def test_pool_timeout(server):
|
||||
pool_limits = httpx.PoolLimits(max_connections=1)
|
||||
limits = httpx.Limits(max_connections=1)
|
||||
timeout = httpx.Timeout(None, pool=1e-4)
|
||||
|
||||
async with httpx.AsyncClient(pool_limits=pool_limits, timeout=timeout) as client:
|
||||
async with httpx.AsyncClient(limits=limits, timeout=timeout) as client:
|
||||
async with client.stream("GET", server.url):
|
||||
with pytest.raises(httpx.PoolTimeout):
|
||||
await client.get("http://localhost:8000/")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user