Skip to content

feat: enable zstd compression auto-negotiation - #3108

Merged
oferchen merged 2 commits into
masterfrom
feat/zstd-auto-negotiation
Apr 6, 2026
Merged

oferchen merged 2 commits into
masterfrom
feat/zstd-auto-negotiation

Conversation

@oferchen

@oferchen oferchen commented Apr 5, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Add zstd as the first entry in supported_compressions() negotiation list, gated on the zstd feature flag, matching upstream rsync's compat.c:100-112 preference order (zstd > zlibx > zlib > none)
  • When both peers support zstd, it is automatically selected as the compression codec instead of falling back to zlibx/zlib
  • Fix sender-side CompressedTokenEncoder lifecycle: create once at session level in run_transfer_loop() and reuse across all files, matching upstream token.c's single compression context pattern. Previously each file created a new encoder (new ZSTD_CCtx), breaking zstd's continuous-stream contract where the receiver expects one DCtx for the entire session
  • Graceful fallback to zlibx/zlib when the remote peer does not advertise zstd support
  • LZ4 remains excluded from auto-negotiation until wire format is interop-validated (task Add executable fallback to brand detection #1380)

Changes

crates/protocol/src/negotiation/capabilities/algorithms.rs

  • Add "zstd" as first entry in supported_compressions() vstring list (behind #[cfg(feature = "zstd")])
  • Update negotiation tests to expect zstd when feature is enabled

crates/transfer/src/generator/delta.rs

  • Change create_token_encoder() visibility to pub(super) so transfer.rs can call it at session start
  • Change stream_whole_file_transfer() signature: replace compression: Option<CompressionAlgorithm> with encoder: Option<&mut CompressedTokenEncoder> (caller-provided)
  • Change write_delta_with_compression() signature: replace compression: Option<CompressionAlgorithm> with encoder: Option<&mut CompressedTokenEncoder> + is_zlib: bool
  • Remove per-file encoder creation (create_token_encoder() calls) from both functions

crates/transfer/src/generator/transfer.rs

  • Create CompressedTokenEncoder once at session level before the transfer loop
  • Pass encoder as Option<&mut CompressedTokenEncoder> to per-file functions
  • Use file_compression() to gate per-file encoder usage (skip-compress files get None)

crates/transfer/src/generator/tests.rs

  • Update test call sites for new function signatures

Upstream reference

  • token.c - single compression context per session, finish() resets run state between files but preserves CCtx
  • compat.c:100-112 - zstd > zlibx > zlib negotiation preference order

Test plan

  • CI passes on all platforms (fmt, clippy, nextest, Windows, macOS, Linux musl)
  • Compression Codec Interop passes (previously failing due to per-file encoder bug)
  • Interop with upstream rsync passes
  • Compression negotiation tests updated to expect zstd when feature is enabled
  • Fallback behavior verified when zstd feature disabled or remote doesn't offer zstd

@github-actions github-actions Bot added the enhancement New feature or request label Apr 5, 2026
@oferchen oferchen closed this Apr 5, 2026
@oferchen oferchen reopened this Apr 6, 2026
@oferchen
oferchen force-pushed the feat/zstd-auto-negotiation branch from f81d261 to b900313 Compare April 6, 2026 06:17
@oferchen
oferchen marked this pull request as draft April 6, 2026 06:19
Add zstd to the supported compression list advertised during vstring
negotiation, gated on the `zstd` feature flag. When both peers support
zstd, it is automatically selected as the preferred compression codec
instead of falling back to zlibx/zlib. This mirrors upstream rsync's
compat.c:100-112 where SUPPORT_ZSTD gates zstd in the preference list.

The zstd per-token wire framing was validated in PR #3081, making it
safe for auto-negotiation. Lz4 remains excluded until its wire format
is interop-validated (task #1380). Graceful fallback to zlibx/zlib
occurs when the remote peer does not advertise zstd support.
@oferchen
oferchen force-pushed the feat/zstd-auto-negotiation branch from b900313 to 79316cb Compare April 6, 2026 06:31
@oferchen
oferchen marked this pull request as ready for review April 6, 2026 06:31
Create the CompressedTokenEncoder once at session level in
run_transfer_loop() and pass it to per-file functions, matching
upstream token.c's single compression context pattern. Previously
each file created a new encoder (new ZSTD_CCtx), breaking zstd's
continuous-stream contract where the receiver expects one DCtx
for the entire session.

upstream: token.c uses a single compression context across all files
@oferchen
oferchen merged commit 52e0f79 into master Apr 6, 2026
38 checks passed
@oferchen
oferchen deleted the feat/zstd-auto-negotiation branch April 6, 2026 08:25
oferchen added a commit that referenced this pull request May 1, 2026
* feat: enable zstd compression auto-negotiation

Add zstd to the supported compression list advertised during vstring
negotiation, gated on the `zstd` feature flag. When both peers support
zstd, it is automatically selected as the preferred compression codec
instead of falling back to zlibx/zlib. This mirrors upstream rsync's
compat.c:100-112 where SUPPORT_ZSTD gates zstd in the preference list.

The zstd per-token wire framing was validated in PR #3081, making it
safe for auto-negotiation. Lz4 remains excluded until its wire format
is interop-validated (task #1380). Graceful fallback to zlibx/zlib
occurs when the remote peer does not advertise zstd support.

* fix: reuse sender-side CompressedTokenEncoder across files for zstd

Create the CompressedTokenEncoder once at session level in
run_transfer_loop() and pass it to per-file functions, matching
upstream token.c's single compression context pattern. Previously
each file created a new encoder (new ZSTD_CCtx), breaking zstd's
continuous-stream contract where the receiver expects one DCtx
for the entire session.

upstream: token.c uses a single compression context across all files
oferchen added a commit that referenced this pull request May 5, 2026
* feat: enable zstd compression auto-negotiation

Add zstd to the supported compression list advertised during vstring
negotiation, gated on the `zstd` feature flag. When both peers support
zstd, it is automatically selected as the preferred compression codec
instead of falling back to zlibx/zlib. This mirrors upstream rsync's
compat.c:100-112 where SUPPORT_ZSTD gates zstd in the preference list.

The zstd per-token wire framing was validated in PR #3081, making it
safe for auto-negotiation. Lz4 remains excluded until its wire format
is interop-validated (task #1380). Graceful fallback to zlibx/zlib
occurs when the remote peer does not advertise zstd support.

* fix: reuse sender-side CompressedTokenEncoder across files for zstd

Create the CompressedTokenEncoder once at session level in
run_transfer_loop() and pass it to per-file functions, matching
upstream token.c's single compression context pattern. Previously
each file created a new encoder (new ZSTD_CCtx), breaking zstd's
continuous-stream contract where the receiver expects one DCtx
for the entire session.

upstream: token.c uses a single compression context across all files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant