Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions b2sdk/_internal/b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,5 +628,16 @@ class NotDecompressingHTTPAdapter(HTTPAdapter):
:code:`requests.Response` class.
"""

# urllib3 defaults to 16 KiB, which makes upload bodies go through the stream wrappers in tiny steps
SEND_BLOCKSIZE = 1024 * 1024

def init_poolmanager(self, *args, **kwargs):
kwargs.setdefault('blocksize', self.SEND_BLOCKSIZE)
super().init_poolmanager(*args, **kwargs)

def proxy_manager_for(self, proxy, **proxy_kwargs):
proxy_kwargs.setdefault('blocksize', self.SEND_BLOCKSIZE)
return super().proxy_manager_for(proxy, **proxy_kwargs)

def build_response(self, req, resp):
return NotDecompressingResponse.from_builtin_response(super().build_response(req, resp))
1 change: 1 addition & 0 deletions changelog.d/+upload-send-blocksize.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Upload request bodies are now sent in 1 MiB blocks instead of urllib3's default 16 KiB, which cuts CPU overhead per uploaded byte.
8 changes: 8 additions & 0 deletions test/unit/b2http/test_b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,14 @@ def user_agent_append(self):
return self.UA_APPEND


def test_request_body_is_sent_in_large_blocks():
adapter = B2Http().session.get_adapter('https://api.backblazeb2.com')
pool = adapter.poolmanager.connection_from_url('https://api.backblazeb2.com')
assert pool.conn_kw['blocksize'] == 1024 * 1024
proxy_manager = adapter.proxy_manager_for('http://proxy.example.com:3128')
assert proxy_manager.connection_pool_kw['blocksize'] == 1024 * 1024


class TestSetLocaleContextManager(TestBase):
def test_set_locale_context_manager(self):
# C.UTF-8 on Ubuntu 18.04 Bionic, C.utf8 on Ubuntu 22.04 Jammy
Expand Down