]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Use URLLib3 configuration options for URLLib3 transport (#970)
authorTom Christie <tom@tomchristie.com>
Thu, 21 May 2020 13:20:16 +0000 (14:20 +0100)
committerGitHub <noreply@github.com>
Thu, 21 May 2020 13:20:16 +0000 (14:20 +0100)
* Use URLLib3 configuration options for URLLib3 transport

* Drop redundant code

httpx/_transports/urllib3.py

index 714c925369c4e12bc0a40f79b64029964bfd25c2..b8c917017c42ce9fbe536af8b923f540be5630b7 100644 (file)
@@ -1,11 +1,10 @@
-import math
 import socket
 import ssl
 from typing import Dict, Iterator, List, Optional, Tuple, Union
 
 import httpcore
 
-from .._config import DEFAULT_POOL_LIMITS, PoolLimits, Proxy, SSLConfig
+from .._config import Proxy, SSLConfig
 from .._content_streams import ByteStream, IteratorStream
 from .._types import CertTypes, VerifyTypes
 from .._utils import as_network_error, warn_deprecated
@@ -25,7 +24,9 @@ class URLLib3Transport(httpcore.SyncHTTPTransport):
         verify: VerifyTypes = True,
         cert: CertTypes = None,
         trust_env: bool = None,
-        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
+        pool_connections: int = 10,
+        pool_maxsize: int = 10,
+        pool_block: bool = False,
     ):
         assert (
             urllib3 is not None
@@ -34,55 +35,38 @@ class URLLib3Transport(httpcore.SyncHTTPTransport):
         ssl_config = SSLConfig(
             verify=verify, cert=cert, trust_env=trust_env, http2=False
         )
-        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 max_connections is None:
-            block = False
-            if max_keepalive is None:
-                num_pools = 1000
-                maxsize = 1000
-            else:
-                num_pools = int(math.sqrt(max_keepalive))
-                maxsize = int(math.sqrt(max_keepalive))
-        else:
-            block = True
-            num_pools = int(math.sqrt(max_connections))
-            maxsize = int(math.sqrt(max_connections))
 
         self.pool = self.init_pool_manager(
             proxy=proxy,
             ssl_context=ssl_config.ssl_context,
-            num_pools=num_pools,
-            maxsize=maxsize,
-            block=block,
+            pool_connections=pool_connections,
+            pool_maxsize=pool_maxsize,
+            pool_block=pool_block,
         )
 
     def init_pool_manager(
         self,
         proxy: Optional[Proxy],
         ssl_context: ssl.SSLContext,
-        num_pools: int,
-        maxsize: int,
-        block: bool,
+        pool_connections: int,
+        pool_maxsize: int,
+        pool_block: bool,
     ) -> Union[urllib3.PoolManager, urllib3.ProxyManager]:
         if proxy is None:
             return urllib3.PoolManager(
                 ssl_context=ssl_context,
-                num_pools=num_pools,
-                maxsize=maxsize,
-                block=block,
+                num_pools=pool_connections,
+                maxsize=pool_maxsize,
+                block=pool_block,
             )
         else:
             return urllib3.ProxyManager(
                 proxy_url=str(proxy.url),
                 proxy_headers=dict(proxy.headers),
                 ssl_context=ssl_context,
-                num_pools=num_pools,
-                maxsize=maxsize,
-                block=block,
+                num_pools=pool_connections,
+                maxsize=pool_maxsize,
+                block=pool_block,
             )
 
     def request(
@@ -171,7 +155,9 @@ class URLLib3Dispatch(URLLib3Transport):
         verify: VerifyTypes = True,
         cert: CertTypes = None,
         trust_env: bool = None,
-        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
+        pool_connections: int = 10,
+        pool_maxsize: int = 10,
+        pool_block: bool = False,
     ):
         warn_deprecated("URLLib3Dispatch is deprecated, please use URLLib3Transport")
         super().__init__(
@@ -179,5 +165,7 @@ class URLLib3Dispatch(URLLib3Transport):
             verify=verify,
             cert=cert,
             trust_env=trust_env,
-            pool_limits=pool_limits,
+            pool_connections=pool_connections,
+            pool_maxsize=pool_maxsize,
+            pool_block=pool_block,
         )