Skip to content

Implement stateful per-chunk streaming compression#2703

Open
SavinduDimal wants to merge 2 commits into
wso2:mainfrom
SavinduDimal:stream-compressor
Open

Implement stateful per-chunk streaming compression#2703
SavinduDimal wants to merge 2 commits into
wso2:mainfrom
SavinduDimal:stream-compressor

Conversation

@SavinduDimal

Copy link
Copy Markdown

Purpose

Description

This pull request introduces a new streamCompressor implementation 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:

  • Added streamCompressor and streamWriteFlushCloser types in decompression.go to 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.
  • Updated the PolicyExecutionContext struct to include requestStreamComp and responseStreamComp fields for managing streaming compression state during request and response processing. [1] [2]

Request and Response Handling:

  • Modified TranslateStreamingRequestChunkAction and TranslateStreamingResponseChunkAction in translator.go to use the new streamCompressor for 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:

  • Added extensive tests in decompression_test.go to 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]

Implement streamCompressor for stateful, per-chunk streaming compression
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@SavinduDimal, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 23 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 51c18d16-0f45-4704-ade8-6f259d12e1b6

📥 Commits

Reviewing files that changed from the base of the PR and between f10ff74 and d33ad8b.

📒 Files selected for processing (1)
  • gateway/gateway-runtime/policy-engine/internal/kernel/translator.go
📝 Walkthrough

Walkthrough

Changes

The 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

Layer / File(s) Summary
Stateful compressor lifecycle
gateway/gateway-runtime/policy-engine/internal/kernel/decompression.go
Adds persistent gzip/Brotli writers, chunk flushing, end-of-stream closure, passthrough handling, and write-after-close errors.
Chunked compression validation
gateway/gateway-runtime/policy-engine/internal/kernel/decompression_test.go
Tests continuous gzip and Brotli streams, empty final chunks, passthrough behavior, lifecycle errors, and prior per-chunk gzip behavior.
Request and response translator integration
gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go, gateway/gateway-runtime/policy-engine/internal/kernel/translator.go
Stores request/response compressors per execution context and feeds streaming chunks through them with termination-aware error fallback.

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
Loading

Suggested reviewers: renuka-fernando

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers purpose and implementation, but most required template sections are missing or only implied. Add the missing template sections: Goals, Approach, User stories, Documentation, Automation tests, Security checks, Samples, Related PRs, and Test environment.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: adding stateful per-chunk streaming compression.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between f59bd67 and f10ff74.

📒 Files selected for processing (4)
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/decompression_test.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/execution_context.go
  • gateway/gateway-runtime/policy-engine/internal/kernel/translator.go

Comment thread gateway/gateway-runtime/policy-engine/internal/kernel/translator.go Outdated
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