Skip to content

Request compression - #185

Merged
turtledreams merged 6 commits into
stagingfrom
request-compression
Dec 2, 2026
Merged

turtledreams merged 6 commits into
stagingfrom
request-compression

Conversation

@turtledreams

@turtledreams turtledreams commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds enableRequestCompression(): the SDK sends request bodies of 1 KB to 1 MB compressed with gzip, following the same protocol as the other Countly SDKs.

Behaviour

  • Off by default. The app calls enableRequestCompression() before start().
  • Compression needs an SDK built with the new CMake option COUNTLY_USE_ZLIB (OFF by default), which links zlib. vcpkg: feature compression. Conan: option with_zlib. In a build without it the call logs a warning and requests stay plain.
  • With compression on, the SDK behavior settings request carries gzip=1. The SDK compresses only after an answer with the top-level "gzip": true. The answer is kept in memory only, every answer replaces the one before, and a server URL change resets it.
  • A body is compressed when it is 1,024 to 1,048,576 bytes long with the checksum, its path has no query, and the gzip data is smaller. It goes to <endpoint>?content_encoding=gzip with the Content-Type of the plain body and no Content-Encoding header. The request queue, the stored requests, the checksum and the logs keep the plain body.
  • When a compressed request is answered without being accepted, it goes plain on its next try and compression stops until a later behavior settings answer carries "gzip": true. Network errors do not stop compression with the built-in HTTP clients. With a client set with setHTTPClient, every failed compressed request stops it, because such a client may report no status code.
  • A client set with setHTTPClient gets the marker at the end of the path and the gzip data as data; it must send all data.size() bytes.
  • The built-in libcurl client now gives libcurl the body size, so a body with zero bytes is sent whole.
  • The extra send time that a large request gets on a slow connection counts the bytes that are sent, so a compressed request gets the time of its compressed size.

What does not change

Builds without COUNTLY_USE_ZLIB, and apps that do not call enableRequestCompression(), send the same requests as before. The request records under api/requests/ are unchanged. No public header depends on the new definition.

Public API

  • Countly::enableRequestCompression()
  • CountlyConfiguration::requestCompressionEnabled
  • RequestModule::currentServer(), RequestModule::setCompressedBodiesAccepted(bool, const std::string &)

Tests

tests/request_compression.cpp, 14 test cases with zlib and 2 without:

  • off by default; gzip=1 on the behavior settings request; a large request goes compressed and decodes to the plain body, with the checksum inside; a small request stays plain
  • answers without "gzip": true (missing, false, "true", 1, inside c) keep requests plain
  • a refused compressed request goes plain on its next try (the retry equals the decoded body), later requests stay plain, and the next accepting answer starts compression again
  • a later answer without gzip, a server URL change, provided behavior settings and a call after start()
  • size limits (1,023 / 1,024 / 1,048,576 / 1,048,577 bytes, with the checksum counted), a body gzip does not make smaller, a path with a query
  • immediate requests: an answer outside 2xx, a 2xx answer that is not JSON, and a failure without a status code stop compression
  • without zlib: the warning, no gzip=1, plain requests

tests/request.cpp, "Built-in HTTP client with request compression" (builds with zlib): the built-in HTTP client, libcurl or WinHTTP, against LocalHTTPServer, which can now answer the behavior settings request with given settings and reply with 400 or with no answer:

  • the whole gzip data arrives and decodes to the plain body with its checksum
  • a request that gets no answer keeps compression on, and its retry goes compressed
  • a 400 answer stops compression, and the retry goes plain with the same body

How it was checked

Check Result
Full suite, GCC 13.3, libcurl, COUNTLY_USE_ZLIB=ON (zlib 1.3), Breakpad, SQLite 245 of 245 test cases
Full suite, MSVC 19.51, WinHTTP, Breakpad, SQLite, no zlib 232 of 232
Compression tests and built-in HTTP client tests, MinGW GCC 13.2, WinHTTP, COUNTLY_USE_ZLIB=ON (zlib 1.3.1) 14 of 14 and 5 of 5 test cases
The built-in client test with compression, once without the body size in the libcurl request and once with network errors counted as answered fails in both cases
Built-in WinHTTP client with zlib against a local HTTP server that decodes like the Countly server a 13,421 byte request arrived as 1,104 bytes at /i?content_encoding=gzip and decoded to 40 events with its checksum; a 400 answer gave a plain retry; an outage did not stop compression
Built-in libcurl client with zlib against the same server the same three cases pass
Built-in WinHTTP client with zlib against a Countly server with compression support, for an app with and an app without a salt the behavior settings answer carried "gzip": true; the two 40-event requests went compressed (15,484 bytes as 1,125) and were answered with Success, the 1-event request went plain, and the server stored 81 of 81 events for each app; for the app with a salt the server accepted the checksum on the decoded body
The same against a Countly server without compression support the behavior settings request with gzip=1 was answered without gzip, every request went plain and was answered with Success, and the server stored 81 of 81 events for each app

Build, packaging and CI files

  • CMakeLists.txt: option COUNTLY_USE_ZLIB, find_package(ZLIB), a private compile definition, and find_dependency(ZLIB) in the package config.
  • packaging/: vcpkg feature compression and Conan option with_zlib.
  • scripts/generate_sbom.py: zlib as an optional system component.
  • .github/workflows/tests.yml: two flavors, zlib and zlib-sanitizer-asan; the coverage build turns the option on so that the new code is measured.
  • .github/workflows/static_analysis.yml: the analyzed build turns the option on so that clang-tidy sees the new code.
  • .github/workflows/packaging.yml and .github/package-check/app.cpp: the port is installed and used with the compression feature, and the recipe is created with with_zlib=True.

enableRequestCompression() sends request bodies of 1 KB to 1 MB compressed
with gzip, once the server's SDK behavior settings answer says that it reads
compressed bodies. Compression needs a build with the new CMake option
COUNTLY_USE_ZLIB (off by default), which links zlib; in other builds the call
logs a warning and requests stay plain.

The built-in libcurl client now gives libcurl the body size, so a body with
zero bytes is sent whole.
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

CI report

Check Result Summary
CodeQL ✅ passed
Package Managers ✅ passed
Public API changelog ✅ passed
Public API changes ✅ passed Public API: 0 removed or changed, 4 added
Run Tests ✅ passed Coverage: Line coverage 86.3%
Static analysis ✅ passed No new findings
Public API changes: Public API, 0 removed or changed, 4 added

Added: 4. Check that each one is meant to be public.

cly::Countly::enableRequestCompression method void () public
cly::CountlyConfiguration::requestCompressionEnabled field bool public
cly::RequestModule::currentServer method std::string () public
cly::RequestModule::setCompressedBodiesAccepted method void (bool, const std::string &) public
Run Tests: Coverage, Line coverage 86.3%

Unit test coverage

Metric Covered Count
lines 86.3% 4433/5137
functions 92.7% 532/574
branches 51.1% 5720/11195
Static analysis, No new findings

clang-tidy: 74 findings

Rule Level Count Description
performance-unnecessary-value-param warning 54 https://clang.llvm.org/extra/clang-tidy/checks/performance/unnecessary-value-param.html
performance-no-int-to-ptr warning 6 https://clang.llvm.org/extra/clang-tidy/checks/performance/no-int-to-ptr.html
bugprone-narrowing-conversions warning 4 https://clang.llvm.org/extra/clang-tidy/checks/bugprone/narrowing-conversions.html
bugprone-implicit-widening-of-multiplication-result warning 3 https://clang.llvm.org/extra/clang-tidy/checks/bugprone/implicit-widening-of-multiplication-result.html
performance-enum-size warning 3 https://clang.llvm.org/extra/clang-tidy/checks/performance/enum-size.html
performance-for-range-copy warning 3 https://clang.llvm.org/extra/clang-tidy/checks/performance/for-range-copy.html
performance-avoid-endl warning 1 https://clang.llvm.org/extra/clang-tidy/checks/performance/avoid-endl.html

First 50 findings

Location Rule Message
include/countly.hpp:573 performance-for-range-copy loop variable is copied but only used as const reference; consider making it a const reference
include/countly.hpp:583 performance-for-range-copy loop variable is copied but only used as const reference; consider making it a const reference
include/countly.hpp:593 performance-for-range-copy loop variable is copied but only used as const reference; consider making it a const reference
include/countly.hpp:995 performance-enum-size enum 'ConsentChangeSource' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base t
include/countly/constants.hpp:121 bugprone-narrowing-conversions narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'int' is implementation-defined
include/countly/countly_configuration.hpp:13 performance-enum-size enum 'DeviceIdType' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base type to
include/countly/countly_configuration.hpp:207 performance-unnecessary-value-param the const qualified parameter 'appKey' is copied for each invocation; consider making it a reference
include/countly/countly_configuration.hpp:209 performance-unnecessary-value-param parameter 'serverUrl' is passed by value and only copied once; consider moving it to avoid unnecessary copies
include/countly/logger_module.hpp:8 performance-enum-size enum 'LogLevel' uses a larger base type ('int', size: 4 bytes) than necessary for its value set, consider using 'std::uint8_t' (1 byte) as the base type to redu
include/countly/storage_module_base.hpp:42 performance-unnecessary-value-param parameter 'config' is passed by value and only copied once; consider moving it to avoid unnecessary copies
include/countly/storage_module_base.hpp:43 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:129 performance-unnecessary-value-param parameter 'config' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:129 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:129 performance-unnecessary-value-param parameter 'requestBuilder' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:129 performance-unnecessary-value-param parameter 'storageModule' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:129 performance-unnecessary-value-param parameter 'requestModule' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:129 performance-unnecessary-value-param parameter 'mutex' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:528 performance-unnecessary-value-param the parameter 'session_params' is copied for each invocation but only used as a const reference; consider making it a const reference
src/configuration_module.cpp:583 performance-unnecessary-value-param parameter 'config' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:583 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:583 performance-unnecessary-value-param parameter 'requestBuilder' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:583 performance-unnecessary-value-param parameter 'storageModule' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:583 performance-unnecessary-value-param parameter 'requestModule' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:583 performance-unnecessary-value-param parameter 'mutex' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/configuration_module.cpp:589 performance-unnecessary-value-param the parameter 'session_params' is copied for each invocation but only used as a const reference; consider making it a const reference
src/configuration_module.cpp:619 performance-unnecessary-value-param parameter 'session_params' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/countly.cpp:269 performance-unnecessary-value-param the parameter 'fetch' is copied for each invocation but only used as a const reference; consider making it a const reference
src/countly.cpp:747 performance-unnecessary-value-param parameter 'fun' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/countly.cpp:757 performance-unnecessary-value-param parameter 'fun' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/countly.cpp:1299 performance-unnecessary-value-param parameter 'callback' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/countly.cpp:2900 bugprone-implicit-widening-of-multiplication-result result of multiplication in type 'int' is used as a pointer offset after an implicit widening conversion to type 'ptrdiff_t'
src/countly.cpp:2986 performance-no-int-to-ptr integer to pointer cast pessimizes optimization opportunities
src/crash_module.cpp:33 performance-unnecessary-value-param parameter 'config' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/crash_module.cpp:33 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/crash_module.cpp:33 performance-unnecessary-value-param parameter 'requestModule' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/crash_module.cpp:33 performance-unnecessary-value-param parameter 'mutex' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/crash_module.cpp:177 performance-unnecessary-value-param parameter 'config' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/crash_module.cpp:177 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/crash_module.cpp:177 performance-unnecessary-value-param parameter 'requestModule' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/crash_module.cpp:177 performance-unnecessary-value-param parameter 'mutex' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/health_check_module.cpp:73 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/health_check_module.cpp:87 performance-unnecessary-value-param the parameter 'storage' is copied for each invocation but only used as a const reference; consider making it a const reference
src/logger_module.cpp:41 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/logger_module.cpp:94 performance-avoid-endl do not use 'std::endl' with streams; use '\n' instead
src/logger_module.cpp:104 performance-unnecessary-value-param the parameter 'healthCheck' is copied for each invocation but only used as a const reference; consider making it a const reference
src/request_builder.cpp:38 performance-unnecessary-value-param parameter 'config' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/request_builder.cpp:38 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/request_module.cpp:132 performance-unnecessary-value-param parameter 'config' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/request_module.cpp:132 performance-unnecessary-value-param parameter 'logger' is passed by value and only copied once; consider moving it to avoid unnecessary copies
src/request_module.cpp:132 performance-unnecessary-value-param parameter 'requestBuilder' is passed by value and only copied once; consider moving it to avoid unnecessary copies

New compared with the base branch: 0

No finding that the base branch does not have.

For f548d54, updated 2026-12-01 14:37 UTC. Each check's run page has its full log.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

Public API changes

Added: 4. Check that each one is meant to be public.

cly::Countly::enableRequestCompression method void () public
cly::CountlyConfiguration::requestCompressionEnabled field bool public
cly::RequestModule::currentServer method std::string () public
cly::RequestModule::setCompressedBodiesAccepted method void (bool, const std::string &) public

cppcheck read the list as always empty, because the HTTP client callback fills it.
# Conflicts:
#	CHANGELOG.md
#	CMakeLists.txt
#	src/request_module.cpp
A local server answers the behavior settings request with gzip true. The test
checks that the whole gzip body arrives, that a request without an answer keeps
compression on, and that a 400 answer gives a plain retry with the same body.
…sion

The header now names the rule for clients set with setHTTPClient, and the log
line also fits a request that got no answer.
@turtledreams
turtledreams merged commit 6acb2d6 into staging Dec 2, 2026
56 checks passed
@turtledreams
turtledreams deleted the request-compression branch December 2, 2026 09:41
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.

1 participant