Add blocksize parameter to PoolManager
MODIFY
src/urllib3/poolmanager.py
src/urllib3/poolmanager.py
@@ -56,6 +56,9 @@ SSL_KEYWORDS = (
"ssl_context",
"key_password",
)
+# Default value for `blocksize` - a new parameter introduced to
+# http.client.HTTPConnection & http.client.HTTPSConnection in Python 3.7
+_DEFAULT_BLOCKSIZE = 16384
_SelfT = TypeVar("_SelfT")
@@ -95,6 +98,7 @@ class PoolKey(NamedTuple):
key_assert_hostname: Optional[Union[bool, str]]
key_assert_fingerprint: Optional[str]
key_server_hostname: Optional[str]
+ key_blocksize: Optional[int]
def _default_key_normalizer(
@@ -145,6 +149,10 @@ def _default_key_normalizer(
if field not in context:
context[field] = None
+ # Default key_blocksize to _DEFAULT_BLOCKSIZE if missing from the context
+ if context.get("key_blocksize") is None:
+ context["key_blocksize"] = _DEFAULT_BLOCKSIZE
+
return key_class(**context)
@@ -250,6 +258,11 @@ class PoolManager(RequestMethods):
if request_context is None:
request_context = self.connection_pool_kw.copy()
+ # Default blocksize to _DEFAULT_BLOCKSIZE if missing or explicitly
+ # set to 'None' in the request_context.
+ if request_context.get("blocksize") is None:
+ request_context["blocksize"] = _DEFAULT_BLOCKSIZE
+
# Although the context has everything necessary to create the pool,
# this function has historically only used the scheme, host, and port
# in the positional args. When an API change is acceptable these can

MODIFY
test/test_poolmanager.py
test/test_poolmanager.py
@@ -9,7 +9,12 @@ import pytest
from urllib3 import connection_from_url
from urllib3.connectionpool import HTTPSConnectionPool
from urllib3.exceptions import ClosedPoolError, LocationValueError
-from urllib3.poolmanager import PoolKey, PoolManager, key_fn_by_scheme
+from urllib3.poolmanager import (
+ _DEFAULT_BLOCKSIZE,
+ PoolKey,
+ PoolManager,
+ key_fn_by_scheme,
+)
from urllib3.util import retry, timeout
from urllib3.util.url import Url
@@ -113,6 +118,7 @@ class TestPoolManager:
"retries": retry.Retry(total=6, connect=2),
"block": True,
"source_address": "127.0.0.1",
+ "blocksize": _DEFAULT_BLOCKSIZE + 1,
}
p = PoolManager()
conn_pools = [
@@ -145,6 +151,7 @@ class TestPoolManager:
"cert_reqs": "CERT_REQUIRED",
"ca_certs": "/root/path_to_pem",
"ssl_version": "SSLv23_METHOD",
+ "blocksize": _DEFAULT_BLOCKSIZE + 1,
}
p = PoolManager()
conn_pools = [
@@ -401,3 +408,23 @@ class TestPoolManager:
p = PoolManager()
assert p._proxy_requires_url_absolute_form(Url("http://example.com")) is False
assert p._proxy_requires_url_absolute_form(Url("https://example.com")) is False
+
+ @pytest.mark.parametrize(
+ "input_blocksize,expected_blocksize",
+ [
+ (_DEFAULT_BLOCKSIZE, _DEFAULT_BLOCKSIZE),
+ (None, _DEFAULT_BLOCKSIZE),
+ (8192, 8192),
+ ],
+ )
+ def test_poolmanager_blocksize(
+ self, input_blocksize: int, expected_blocksize: int
+ ) -> None:
+ """Assert PoolManager sets blocksize properly"""
+ p = PoolManager()
+
+ pool_blocksize = p.connection_from_url(
+ "http://example.com", {"blocksize": input_blocksize}
+ )
+ assert pool_blocksize.conn_kw["blocksize"] == expected_blocksize
+ assert pool_blocksize._get_conn().blocksize == expected_blocksize # type: ignore[attr-defined]

Add blocksize parameter to PoolManager

Co-authored-by: Seth Michael Larson <sethmichaellarson@gmail.com>