Skip to content

fix: improve command channel buffer sizing under high write load - #1199

Merged
FlorentinDUBOIS merged 2 commits into
mainfrom
fix/command-buffer-sizing
Mar 14, 2026
Merged

FlorentinDUBOIS merged 2 commits into
mainfrom
fix/command-buffer-sizing

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Summary

  • Doubling growth strategy (O(log n)) for channel buffers, capped at max_buffer_size
  • readable() now grows front buffer before giving up instead of silently stopping
  • Auto-shrink after idle (when <25% of initial size in use)
  • High watermark warning at 80% of max (one-shot, resets on shrink)
  • New MessageTooLarge and BufferFull error variants with diagnostics
  • Buffer::shrink() method in growable.rs
  • 4 new tests

Review fixes applied

  • Bug fix: doubling loop in write_delimited_message was a no-op — min(new_length, needed) inside the loop meant buffer grew to exact size every time, defeating amortization. Removed inner cap.
  • Replaced misleading test with one that exercises actual write path

Closes #1050

Test plan

  • cargo test --workspace passes (4 new tests)
  • cargo clippy --workspace clean
  • E2e tests pass (20/20)

Replace linear +5000 buffer growth with doubling strategy to reduce
reallocations under sustained load (O(log n) vs O(n) allocations).

Key changes:
- readable() now grows front buffer before giving up, fixing #1050
  where the channel silently stopped reading when the buffer was full
- Add Buffer::shrink() to reclaim memory after peak usage subsides
- Auto-shrink buffers back to initial_size when drained
- Log warnings at 80% buffer utilization (high watermark)
- Improve error variants with capacity, message_len, and max context
- Add 4 targeted tests for growth, cap, shrink, and readable behavior

Closes #1050
…prove tests

The back buffer growth loop in write_delimited_message was capping
each iteration at `needed` via `min(..., needed)`, which defeated the
doubling strategy — the buffer always grew to exactly the minimum
required size, losing amortization for future writes.

Remove the inner cap so the loop overshoots to the next power-of-two
boundary (still bounded by max_buffer_size on exit).

Also:
- Replace the misleading `front_buffer_grows_on_readable` test (which
  only tested `grow_size()`, duplicating `buffer_grows_with_doubling_strategy`)
  with `back_buffer_grows_with_doubling_on_write` that exercises the
  actual write path and asserts doubling behavior.
- Remove unnecessary `mut` on `writing_channel` in test.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 58fb59d into main Mar 14, 2026
22 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/command-buffer-sizing branch March 14, 2026 12:29
jrcribb pushed a commit to jrcribb/sozu that referenced this pull request Aug 10, 2026
…-sizing

fix: improve command channel buffer sizing under high write load
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.

should the command buffer size really be capped?

2 participants