Skip to content

Add WithMinLength option to control when responses are gzipped #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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

Attention: Patch coverage is 97.43590% with 1 line in your changes missing coverage. Please review.

Project coverage is 78.50%. Comparing base (ef7d482) to head (eeeb87b).
Report is 54 commits behind head on master.

Files with missing lines Patch % Lines
handler.go 93.75% 1 Missing ⚠️

❗ 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%   78.50%   -2.86%     
==========================================
  Files           3        3              
  Lines         118      214      +96     
==========================================
+ Hits           96      168      +72     
- Misses         19       39      +20     
- Partials        3        7       +4     
Flag Coverage Δ
go- ?
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.22 ?
go-1.23 78.50% <97.43%> (?)
go-1.24 78.50% <97.43%> (?)
macos-latest ?
ubuntu-latest 78.50% <97.43%> (-2.86%) ⬇️

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 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) {

gzip.go Outdated
g.Header().Del("Content-Length")
// 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"))
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

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

Ignoring the strconv.Atoi error may treat invalid headers as length zero. Consider handling the parse error or validating the header value before using it.

Suggested change
contentLen, _ := strconv.Atoi(g.Header().Get("Content-Length"))
contentLenStr := g.Header().Get("Content-Length")
contentLen, err := strconv.Atoi(contentLenStr)
if err != nil {
// If Content-Length is invalid, skip compression and write directly
return g.ResponseWriter.Write(data)
}

Copilot uses AI. Check for mistakes.

Copy link
Author

Choose a reason for hiding this comment

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

This is intentionally ignored. Invalid headers will result in no compression being used since it's undefined behavior

@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

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