Skip to content
Myr

Rename pool limit options (#968)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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]'

authored by Tom Christie · committed by GitHub · parent 9b6605c · Browse files · 5 files+46 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+46 added−40 removed

  • tests:+4 added−4 removed
  • other:+42 added−36 removed

5 files+46 added−40 removed

modified:docs/advanced.md+3 added-3 removed
402 unmodified lines
403404You can control the connection pool size using the `pool_limits` keyword405argument on the client. It takes instances of `httpx.PoolLimits` which define:406407- `soft_limit`, number of allowable keep-alive connections, or `None` to always407- `max_keepalive`, number of allowable keep-alive connections, or `None` to always408allow. (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.410(Default 100)411412413```python414limits = httpx.PoolLimits(soft_limit=5, hard_limit=10)414limits = httpx.PoolLimits(max_keepalive=5, max_connections=10)415client = httpx.Client(pool_limits=limits)416```417418## Multipart file encoding
256 unmodified lines
modified:httpx/_transports/urllib3.py+8 added-8 removed
32 unmodified lines
3334 ssl_config = SSLConfig(35 verify=verify, cert=cert, trust_env=trust_env, http2=False36 )37 hard_limit = pool_limits.hard_limit38 soft_limit = pool_limits.soft_limit37 max_connections = pool_limits.max_connections38 max_keepalive = pool_limits.max_keepalive3940 # Our connection pool configuration doesn't quite match up with urllib3's41 # controls, but we can put sensible mappings in place:42 if hard_limit is None:42 if max_connections is None:43 block = False44 if soft_limit is None:44 if max_keepalive is None:45 num_pools = 100046 maxsize = 100047 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))50 else:51 block = True52 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))5455 self.pool = self.init_pool_manager(56 proxy=proxy,57 ssl_context=ssl_config.ssl_context,
126 unmodified lines
modified:httpx/_client.py+8 added-16 removed
502 unmodified lines
503504 ssl_context = SSLConfig(505 verify=verify, cert=cert, trust_env=trust_env506 ).ssl_context507 max_keepalive = pool_limits.soft_limit508 max_connections = pool_limits.hard_limit507508 return httpcore.SyncConnectionPool(509 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,512 )513514 def init_proxy_transport(515 self,
5 unmodified lines
521 ) -> httpcore.SyncHTTPTransport:522 ssl_context = SSLConfig(523 verify=verify, cert=cert, trust_env=trust_env524 ).ssl_context527 max_keepalive = pool_limits.soft_limit528 max_connections = pool_limits.hard_limit525526 return httpcore.SyncHTTPProxy(527 proxy_url=proxy.url.raw,528 proxy_headers=proxy.headers.raw,529 proxy_mode=proxy.mode,530 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,533 )534535 def transport_for_url(self, url: URL) -> httpcore.SyncHTTPTransport:536 """
508 unmodified lines
10451046 ssl_context = SSLConfig(1047 verify=verify, cert=cert, trust_env=trust_env1048 ).ssl_context1053 max_keepalive = pool_limits.soft_limit1054 max_connections = pool_limits.hard_limit10491050 return httpcore.AsyncConnectionPool(1051 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,1054 http2=http2,1055 )10561057 def init_proxy_transport(
7 unmodified lines
1065 ) -> httpcore.AsyncHTTPTransport:1066 ssl_context = SSLConfig(1067 verify=verify, cert=cert, trust_env=trust_env1068 ).ssl_context1075 max_keepalive = pool_limits.soft_limit1076 max_connections = pool_limits.hard_limit10691070 return httpcore.AsyncHTTPProxy(1071 proxy_url=proxy.url.raw,1072 proxy_headers=proxy.headers.raw,1073 proxy_mode=proxy.mode,1074 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,1077 http2=http2,1078 )10791080 def transport_for_url(self, url: URL) -> httpcore.AsyncHTTPTransport:
446 unmodified lines
modified:httpx/_config.py+23 added-9 removed
6 unmodified lines
7import certifi89from ._models import URL, Headers10from ._types import CertTypes, HeaderTypes, TimeoutTypes, URLTypes, VerifyTypes11from ._utils import get_ca_bundle_from_env, get_logger11from ._utils import get_ca_bundle_from_env, get_logger, warn_deprecated1213DEFAULT_CIPHERS = ":".join(14 [15 "ECDHE+AESGCM",
271 unmodified lines
287 Limits on the number of connections in a connection pool.288289 **Parameters:**290291 * **soft_limit** - Allow the connection pool to maintain keep-alive connections291 * **max_keepalive** - Allow the connection pool to maintain keep-alive connections292 below this point.293 * **hard_limit** - The maximum number of concurrent connections that may be293 * **max_connections** - The maximum number of concurrent connections that may be294 established.295 """296297 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,304 ):300 self.soft_limit = soft_limit301 self.hard_limit = hard_limit305 self.max_keepalive = max_keepalive306 self.max_connections = max_connections307 if soft_limit is not None: # pragma: nocover308 self.max_keepalive = soft_limit309 warn_deprecated("'soft_limit' is deprecated. Use 'max_keepalive' instead.",)310 if hard_limit is not None: # pragma: nocover311 self.max_connections = hard_limit312 warn_deprecated(313 "'hard_limit' is deprecated. Use 'max_connections' instead.",314 )315316 def __eq__(self, other: typing.Any) -> bool:317 return (318 isinstance(other, self.__class__)306 and self.soft_limit == other.soft_limit307 and self.hard_limit == other.hard_limit319 and self.max_keepalive == other.max_keepalive320 and self.max_connections == other.max_connections321 )322323 def __repr__(self) -> str:324 class_name = self.__class__.__name__325 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})"328 )329330331class Proxy:
40 unmodified lines
modified:tests/test_config.py+4 added-4 removed
116 unmodified lines
117 assert ssl == SSLConfig(verify=False)118119120def 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)"123124125def 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)128129130def test_timeout_eq():131 timeout = httpx.Timeout(timeout=5.0)
110 unmodified lines