Skip to content

Commit 991915a

Browse files
Rename pool limit options (#968)
* Pass proxy_url * Rename hard_limit/soft_limit * Use 'warn_deprecated' function * Update PoolLimits docs * Linting * Update httpcore dependancy * Update port in Transport API to be 'Optional[int]'
1 parent 9b6605c commit 991915a

File tree

5 files changed

+46
-40
lines changed

5 files changed

+46
-40
lines changed

docs/advanced.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,14 @@ response = client.get('http://example.com/')
404404
You can control the connection pool size using the `pool_limits` keyword
405405
argument on the client. It takes instances of `httpx.PoolLimits` which define:
406406

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

412412

413413
```python
414-
limits = httpx.PoolLimits(soft_limit=5, hard_limit=10)
414+
limits = httpx.PoolLimits(max_keepalive=5, max_connections=10)
415415
client = httpx.Client(pool_limits=limits)
416416
```
417417

httpx/_client.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,11 @@ def init_transport(
504504
ssl_context = SSLConfig(
505505
verify=verify, cert=cert, trust_env=trust_env
506506
).ssl_context
507-
max_keepalive = pool_limits.soft_limit
508-
max_connections = pool_limits.hard_limit
509507

510508
return httpcore.SyncConnectionPool(
511509
ssl_context=ssl_context,
512-
max_keepalive=max_keepalive,
513-
max_connections=max_connections,
510+
max_keepalive=pool_limits.max_keepalive,
511+
max_connections=pool_limits.max_connections,
514512
)
515513

516514
def init_proxy_transport(
@@ -524,16 +522,14 @@ def init_proxy_transport(
524522
ssl_context = SSLConfig(
525523
verify=verify, cert=cert, trust_env=trust_env
526524
).ssl_context
527-
max_keepalive = pool_limits.soft_limit
528-
max_connections = pool_limits.hard_limit
529525

530526
return httpcore.SyncHTTPProxy(
531527
proxy_url=proxy.url.raw,
532528
proxy_headers=proxy.headers.raw,
533529
proxy_mode=proxy.mode,
534530
ssl_context=ssl_context,
535-
max_keepalive=max_keepalive,
536-
max_connections=max_connections,
531+
max_keepalive=pool_limits.max_keepalive,
532+
max_connections=pool_limits.max_connections,
537533
)
538534

539535
def transport_for_url(self, url: URL) -> httpcore.SyncHTTPTransport:
@@ -1050,13 +1046,11 @@ def init_transport(
10501046
ssl_context = SSLConfig(
10511047
verify=verify, cert=cert, trust_env=trust_env
10521048
).ssl_context
1053-
max_keepalive = pool_limits.soft_limit
1054-
max_connections = pool_limits.hard_limit
10551049

10561050
return httpcore.AsyncConnectionPool(
10571051
ssl_context=ssl_context,
1058-
max_keepalive=max_keepalive,
1059-
max_connections=max_connections,
1052+
max_keepalive=pool_limits.max_keepalive,
1053+
max_connections=pool_limits.max_connections,
10601054
http2=http2,
10611055
)
10621056

@@ -1072,16 +1066,14 @@ def init_proxy_transport(
10721066
ssl_context = SSLConfig(
10731067
verify=verify, cert=cert, trust_env=trust_env
10741068
).ssl_context
1075-
max_keepalive = pool_limits.soft_limit
1076-
max_connections = pool_limits.hard_limit
10771069

10781070
return httpcore.AsyncHTTPProxy(
10791071
proxy_url=proxy.url.raw,
10801072
proxy_headers=proxy.headers.raw,
10811073
proxy_mode=proxy.mode,
10821074
ssl_context=ssl_context,
1083-
max_keepalive=max_keepalive,
1084-
max_connections=max_connections,
1075+
max_keepalive=pool_limits.max_keepalive,
1076+
max_connections=pool_limits.max_connections,
10851077
http2=http2,
10861078
)
10871079

httpx/_config.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ._models import URL, Headers
1010
from ._types import CertTypes, HeaderTypes, TimeoutTypes, URLTypes, VerifyTypes
11-
from ._utils import get_ca_bundle_from_env, get_logger
11+
from ._utils import get_ca_bundle_from_env, get_logger, warn_deprecated
1212

1313
DEFAULT_CIPHERS = ":".join(
1414
[
@@ -288,29 +288,43 @@ class PoolLimits:
288288
289289
**Parameters:**
290290
291-
* **soft_limit** - Allow the connection pool to maintain keep-alive connections
291+
* **max_keepalive** - Allow the connection pool to maintain keep-alive connections
292292
below this point.
293-
* **hard_limit** - The maximum number of concurrent connections that may be
293+
* **max_connections** - The maximum number of concurrent connections that may be
294294
established.
295295
"""
296296

297297
def __init__(
298-
self, *, soft_limit: int = None, hard_limit: int = None,
298+
self,
299+
*,
300+
max_keepalive: int = None,
301+
max_connections: int = None,
302+
soft_limit: int = None,
303+
hard_limit: int = None,
299304
):
300-
self.soft_limit = soft_limit
301-
self.hard_limit = hard_limit
305+
self.max_keepalive = max_keepalive
306+
self.max_connections = max_connections
307+
if soft_limit is not None: # pragma: nocover
308+
self.max_keepalive = soft_limit
309+
warn_deprecated("'soft_limit' is deprecated. Use 'max_keepalive' instead.",)
310+
if hard_limit is not None: # pragma: nocover
311+
self.max_connections = hard_limit
312+
warn_deprecated(
313+
"'hard_limit' is deprecated. Use 'max_connections' instead.",
314+
)
302315

303316
def __eq__(self, other: typing.Any) -> bool:
304317
return (
305318
isinstance(other, self.__class__)
306-
and self.soft_limit == other.soft_limit
307-
and self.hard_limit == other.hard_limit
319+
and self.max_keepalive == other.max_keepalive
320+
and self.max_connections == other.max_connections
308321
)
309322

310323
def __repr__(self) -> str:
311324
class_name = self.__class__.__name__
312325
return (
313-
f"{class_name}(soft_limit={self.soft_limit}, hard_limit={self.hard_limit})"
326+
f"{class_name}(max_keepalive={self.max_keepalive}, "
327+
f"max_connections={self.max_connections})"
314328
)
315329

316330

httpx/_transports/urllib3.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ def __init__(
3434
ssl_config = SSLConfig(
3535
verify=verify, cert=cert, trust_env=trust_env, http2=False
3636
)
37-
hard_limit = pool_limits.hard_limit
38-
soft_limit = pool_limits.soft_limit
37+
max_connections = pool_limits.max_connections
38+
max_keepalive = pool_limits.max_keepalive
3939

4040
# Our connection pool configuration doesn't quite match up with urllib3's
4141
# controls, but we can put sensible mappings in place:
42-
if hard_limit is None:
42+
if max_connections is None:
4343
block = False
44-
if soft_limit is None:
44+
if max_keepalive is None:
4545
num_pools = 1000
4646
maxsize = 1000
4747
else:
48-
num_pools = int(math.sqrt(soft_limit))
49-
maxsize = int(math.sqrt(soft_limit))
48+
num_pools = int(math.sqrt(max_keepalive))
49+
maxsize = int(math.sqrt(max_keepalive))
5050
else:
5151
block = True
52-
num_pools = int(math.sqrt(hard_limit))
53-
maxsize = int(math.sqrt(hard_limit))
52+
num_pools = int(math.sqrt(max_connections))
53+
maxsize = int(math.sqrt(max_connections))
5454

5555
self.pool = self.init_pool_manager(
5656
proxy=proxy,

tests/test_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ def test_ssl_eq():
118118

119119

120120
def test_limits_repr():
121-
limits = httpx.PoolLimits(hard_limit=100)
122-
assert repr(limits) == "PoolLimits(soft_limit=None, hard_limit=100)"
121+
limits = httpx.PoolLimits(max_connections=100)
122+
assert repr(limits) == "PoolLimits(max_keepalive=None, max_connections=100)"
123123

124124

125125
def test_limits_eq():
126-
limits = httpx.PoolLimits(hard_limit=100)
127-
assert limits == httpx.PoolLimits(hard_limit=100)
126+
limits = httpx.PoolLimits(max_connections=100)
127+
assert limits == httpx.PoolLimits(max_connections=100)
128128

129129

130130
def test_timeout_eq():

0 commit comments

Comments
 (0)