Implement stateful per-chunk streaming compression#2703
Conversation
Implement streamCompressor for stateful, per-chunk streaming compression
|
Warning Review limit reached
Next review available in: 23 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughChangesThe PR adds stateful streaming recompression for gzip and Brotli request/response chunks, preserves passthrough for unknown encodings, handles stream closure and errors, and adds regression tests for continuous gzip/Brotli output. Streaming recompression
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant RequestOrResponseChunk
participant StreamingTranslator
participant PolicyExecutionContext
participant streamCompressor
RequestOrResponseChunk->>StreamingTranslator: Translate streaming chunk
StreamingTranslator->>PolicyExecutionContext: Get or create stream compressor
StreamingTranslator->>streamCompressor: FeedChunk(outputBody, endOfStream)
streamCompressor-->>StreamingTranslator: Compressed output
StreamingTranslator-->>RequestOrResponseChunk: Forward translated chunk
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@gateway/gateway-runtime/policy-engine/internal/kernel/translator.go`:
- Around line 1527-1533: The request recompression failure path in translator.go
around lines 1527-1533 must fail or terminate the stream instead of sending
uncompressed data; apply the same change to the response recompression failure
path around lines 1615-1621. Preserve the forwarded Content-Encoding state and
propagate the recompression error or terminate the corresponding
request/response stream rather than falling back to raw chunks.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 26680381-4b10-4551-99e1-c5776bd558a9
📒 Files selected for processing (4)
gateway/gateway-runtime/policy-engine/internal/kernel/decompression.gogateway/gateway-runtime/policy-engine/internal/kernel/decompression_test.gogateway/gateway-runtime/policy-engine/internal/kernel/execution_context.gogateway/gateway-runtime/policy-engine/internal/kernel/translator.go
Purpose
Description
This pull request introduces a new
streamCompressorimplementation to correctly handle streaming compression for both request and response bodies in the policy engine. Previously, each chunk was compressed independently, resulting in multiple compressed members and causing downstream clients to drop all but the first chunk. The new approach maintains a single continuous compressed stream across all chunks, ensuring compatibility with HTTP clients and intermediaries. Comprehensive tests have been added to verify correct behavior for gzip and brotli encodings.Streaming Compression Improvements:
streamCompressorandstreamWriteFlushClosertypes indecompression.goto provide stateful, per-chunk streaming compression, maintaining a single continuous compressed stream across all chunks. This replaces the previous per-chunk compression approach that resulted in multiple members and compatibility issues with downstream decoders.PolicyExecutionContextstruct to includerequestStreamCompandresponseStreamCompfields for managing streaming compression state during request and response processing. [1] [2]Request and Response Handling:
TranslateStreamingRequestChunkActionandTranslateStreamingResponseChunkActionintranslator.goto use the newstreamCompressorfor re-compressing streaming bodies, ensuring the output is a single valid compressed stream. Falls back to uncompressed output on error and cleans up compressor state. [1] [2] [3]Testing and Validation:
decompression_test.goto verify that streaming compression produces a single gzip/brotli member across multiple chunks, correctly round-trips data, and handles edge cases such as empty final chunks, unknown encodings, and write-after-close errors. Tests also document the previous buggy behavior for regression tracking. [1] [2]