Skip to content

Conversation

@nossrannug
Copy link

@nossrannug nossrannug commented Aug 26, 2025

Add configurable compression thresholds for Tonic

This PR adds runtime-configurable compression thresholds to improve performance for large message compression scenarios.
Changes:

  • Compression threshold: Messages smaller than TONIC_COMPRESSION_THRESHOLD (default 1024 bytes) are sent uncompressed
  • Spawn-blocking threshold: Messages larger than TONIC_SPAWN_BLOCKING_THRESHOLD can be compressed on a blocking thread pool to avoid blocking the async runtime

Configuration:
Set via environment variables:

  • TONIC_COMPRESSION_THRESHOLD=4096 # Skip compression for messages < 4KB
  • TONIC_SPAWN_BLOCKING_THRESHOLD=65536 # Use blocking threads for messages >= 64KB

Benefits:

  • Avoids unnecessary compression overhead for small messages
  • Prevents large message compression from blocking async tasks
  • Maintains backward compatibility (defaults preserve existing behavior)

Implementation notes:

  • Added conditional compilation to support builds with/without tokio and compression features
  • Compression task polling happens immediately after spawning for optimal waker registration
  • Tests updated to verify threshold behavior
  • Fixes performance issues with large gRPC message compression blocking the tokio runtime.

@nossrannug nossrannug force-pushed the conditional-stream-compression branch from 76a268b to 449c7cc Compare August 26, 2025 12:35
Messages smaller than 1024 bytes are no longer compressed even when
compression is enabled. This optimization avoids the overhead of
compression for small messages where the compression ratio would be
minimal or potentially increase the message size.

The threshold is set to 1024 bytes (matching UNCOMPRESSED_MIN_BODY_SIZE
used in tests). Messages below this threshold are sent uncompressed
with the compression flag unset in the gRPC header.
@nossrannug nossrannug force-pushed the conditional-stream-compression branch from 449c7cc to 7d51ce6 Compare October 2, 2025 12:44
When compressing large payloads we use spawn_blocking to move the job to a tread to not block the async worker thread
@nossrannug nossrannug force-pushed the conditional-stream-compression branch 2 times, most recently from 5aa700e to 14a3a64 Compare October 27, 2025 12:07
@nossrannug nossrannug force-pushed the conditional-stream-compression branch from 14a3a64 to 472812a Compare October 27, 2025 13:04
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