Skip to content

Conversation

@takanuva15
Copy link

Enables conditional compression based on response size. The response is stored in a temporary buffer within the gzipWriter if it does not meet the provided limit (required in case the server executes additional writes to the response later in its handler control flow). When the handler finally returns, we check whether the limit was met. If not, the buffer contents are written directly to the underlying gin Writer with no gzip compression.

Credit to #20 for the main implementation details

Closes #18
Closes #20
Closes #34
Closes #81
Resolves part of #70

@takanuva15
Copy link
Author

@appleboy hi, can you please approve the workflows to run tests on this PR?

@appleboy appleboy requested a review from Copilot May 21, 2025 03:10
@codecov-commenter
Copy link

codecov-commenter commented May 21, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.58%. Comparing base (ef7d482) to head (6843f0c).
⚠️ Report is 65 commits behind head on master.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #106      +/-   ##
==========================================
- Coverage   81.35%   80.58%   -0.78%     
==========================================
  Files           3        3              
  Lines         118      206      +88     
==========================================
+ Hits           96      166      +70     
- Misses         19       31      +12     
- Partials        3        9       +6     
Flag Coverage Δ
go- ?
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.22 ?
go-1.24 80.58% <100.00%> (?)
go-1.25 80.58% <100.00%> (?)
macos-latest ?
ubuntu-latest 80.58% <100.00%> (-0.78%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces a new WithMinLength option to defer gzip compression until the response body reaches a configurable byte threshold. It updates the core handler and writer to buffer small responses and only apply gzip once the limit is met.

  • Added minLength field and WithMinLength option in options.go
  • Extended gzipWriter in gzip.go with buffering and length-check logic
  • Updated handler.go to conditionally flush buffered data when threshold isn’t reached
  • Added tests in gzip_test.go covering short, long, and multi-write responses
  • Expanded README.md with usage examples and notes for WithMinLength

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
options.go Added minLength config and WithMinLength Option
gzip.go Buffers response data, checks length before gzip
handler.go Defers final write/decompression decision
gzip_test.go New tests for min-length-based compression logic
README.md Documentation and example for WithMinLength
Comments suppressed due to low confidence (1)

gzip.go:44

  • [nitpick] The Write method has nested compression and buffering logic that can be hard to follow. Refactoring length checks and buffer handling into helper methods would improve readability and testability.
func (g *gzipWriter) Write(data []byte) (int, error) {

@takanuva15
Copy link
Author

@appleboy pushed an update to fix coverage and comments. Request approval to run PR workflows

gzip.go Outdated
// minLength is the minimum length of the response body (in bytes) to enable compression
minLength int
// wasMinLengthMetForCompression indicates whether the minimum length for compression has been met
wasMinLengthMetForCompression bool
Copy link
Member

Choose a reason for hiding this comment

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

Is shouldCompress a more appropriate name?

Copy link
Author

Choose a reason for hiding this comment

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

fixed

gzip.go Outdated
// If a Content-Length header is set, use that to decide whether to compress the response.
if g.Header().Get("Content-Length") != "" {
contentLen, _ := strconv.Atoi(g.Header().Get("Content-Length")) // err intentionally ignored for invalid headers
if contentLen >= g.minLength {
Copy link
Member

Choose a reason for hiding this comment

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

Don't use the if-else pattern.

Copy link
Author

Choose a reason for hiding this comment

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

fixed

@appleboy
Copy link
Member

Also fix the lint error

@takanuva15
Copy link
Author

@appleboy Hi, request your review on this PR to add length-based gzipping for gin?

@takanuva15 takanuva15 force-pushed the master branch 3 times, most recently from 5d6022e to acff587 Compare October 23, 2025 17:49
@takanuva15
Copy link
Author

@appleboy Hi, I fixed merge conflicts. Request your approval on this PR to add length-based gzipping for gin?

@appleboy
Copy link
Member

I will take it.

@appleboy
Copy link
Member

appleboy commented Oct 26, 2025

@takanuva15 Please help resolve the conflicts from #47 issue. See the PR #124

@takanuva15
Copy link
Author

@takanuva15 Please help resolve the conflicts from #47 issue. See the PR #124

fixed conflicts

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.

Set a minimum value for the GZIP compression to take effect. Min GZIP value Gzip makes the response larger

3 participants