Skip to content

Commit 262eca6

Browse files
committed
refactor: improve gzip handling and optimize resource management
- Use `strings.Contains` to check for "gzip" in `Content-Encoding` header - Combine `g.gzPool.Put(gz)` and `gz.Reset(io.Discard)` into a single deferred function Signed-off-by: appleboy <[email protected]>
1 parent 1b7e801 commit 262eca6

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

handler.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func newGzipHandler(level int, opts ...Option) *gzipHandler {
6767
// and wraps the response writer with a gzipWriter. After the request is processed, it ensures the gzip.Writer
6868
// is properly closed and the "Content-Length" header is set based on the response size.
6969
func (g *gzipHandler) Handle(c *gin.Context) {
70-
if fn := g.decompressFn; fn != nil && c.Request.Header.Get("Content-Encoding") == "gzip" {
70+
if fn := g.decompressFn; fn != nil && strings.Contains(c.Request.Header.Get("Content-Encoding"), "gzip") {
7171
fn(c)
7272
}
7373

@@ -78,8 +78,10 @@ func (g *gzipHandler) Handle(c *gin.Context) {
7878
}
7979

8080
gz := g.gzPool.Get().(*gzip.Writer)
81-
defer g.gzPool.Put(gz)
82-
defer gz.Reset(io.Discard)
81+
defer func() {
82+
g.gzPool.Put(gz)
83+
gz.Reset(io.Discard)
84+
}()
8385
gz.Reset(c.Writer)
8486

8587
c.Header(headerContentEncoding, "gzip")

0 commit comments

Comments
 (0)