Skip to content

feat: Compress requests using brotli algo#927

Open
mixalturek wants to merge 4 commits into
masterfrom
brotli-compression
Open

feat: Compress requests using brotli algo#927
mixalturek wants to merge 4 commits into
masterfrom
brotli-compression

Conversation

@mixalturek

@mixalturek mixalturek commented Jul 8, 2026

Copy link
Copy Markdown
  • https://github.com/apify/apify-core/pull/28971 added support for brotli compression to BE.
  • Pros: higher compression, cons: more CPU intensive.
  • The brotli dependencies are defined as optional, one has to explicitly enable it and choose one. If none is available, the code falls back to gzip.
  • JS client doesn't compress requests that are too small, this Python client compresses just everything. No change done, I only noticed and stating it.

Related PRs

@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.52%. Comparing base (7f61a10) to head (3fffa62).
⚠️ Report is 11 commits behind head on master.

Files with missing lines Patch % Lines
src/apify_client/http_clients/_base.py 84.61% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #927      +/-   ##
==========================================
- Coverage   94.54%   94.52%   -0.03%     
==========================================
  Files          48       48              
  Lines        5100     5130      +30     
==========================================
+ Hits         4822     4849      +27     
- Misses        278      281       +3     
Flag Coverage Δ
integration 92.61% <69.23%> (-0.31%) ⬇️
unit 83.43% <84.61%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mixalturek mixalturek force-pushed the brotli-compression branch 2 times, most recently from 49bf689 to aabd87c Compare July 8, 2026 10:05
- apify/apify-core#28971 added support for brotli compression to BE.
- Pros: higher compression, cons: more CPU intensive.
- The brotli dependencies are defined as optional, one has to explicitly enable it and choose one. If none is available, the code falls back to gzip.
- JS client doesn't compress requests that are too small, this Python client compresses just everything. No change done, I only noticed and stating it.
Comment thread src/apify_client/http_clients/_base.py
Comment thread src/apify_client/http_clients/_base.py Outdated
Comment thread tests/unit/test_http_clients.py Outdated
Comment thread tests/unit/test_run_charge.py Outdated
Comment thread pyproject.toml Outdated
Comment thread README.md

or any other Python package manager that consumes PyPI.

To enable brotli request-body compression (better than gzip, especially for large payloads), install the optional extra:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering whether this should really be the only way to configure the compression method (only via installation of extra), or whether we should introduce a new flag on the ApifyClient base class instead.

@Pijukatel WDYT?

Comment thread pyproject.toml Outdated
Comment thread tests/unit/test_run_charge.py Outdated
Comment thread pyproject.toml Outdated
@vdusek vdusek requested a review from Pijukatel July 8, 2026 13:36
@vdusek

vdusek commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

This has a significant impact. @Pijukatel, please check it as well.

- Remove `brotlicffi`, only CPython runtime is supported.
- Add a test with bytearray as an input. This would break only in `brotlicffi`, which is no longer in the dependencies. `brotli` dependency handles it correctly and doesn't throw.
- Add `brotli` to dev dependencies and use it directly in tests.
mixalturek added a commit to apify/apify-client-js that referenced this pull request Jul 8, 2026
- apify/apify-client-python#927 (comment)
- The default was 11 (max). Now it uses quality 6 - roughly 2–4× faster with only a modest increase in compressed size.
@mixalturek

Copy link
Copy Markdown
Author

@vdusek Nearly all comments are fixed now, thanks again for your great review.

One more thing: I forgot to mention that the JS client skips compression if the payload size is below 1024 B. Do we want to apply it to the Python client as well?

mixalturek added a commit to apify/apify-client-js that referenced this pull request Jul 8, 2026
- apify/apify-client-python#927 (comment)
- The default was 11 (max). Now it uses quality 6 - roughly 2–4× faster with only a modest increase in compressed size.
mixalturek added a commit to apify/apify-client-js that referenced this pull request Jul 8, 2026
- apify/apify-client-python#927 (comment)
- The default was 11 (max). Now it uses quality 6 - roughly 2–4× faster with only a modest increase in compressed size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants