From 3529136c16a1f39b15e558b1e7a6dff6a28d8ae7 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 27 Jan 2025 10:02:01 +0800 Subject: [PATCH] refactor: improve error handling and resource management in gzip utils (#101) - Simplify error handling by ignoring the error from `gzip.NewWriterLevel` - Reorder `gzPool.Put(gz)` after `gz.Reset(io.Discard)` in the defer function Signed-off-by: appleboy --- handler.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/handler.go b/handler.go index ef5a810..0a8090d 100644 --- a/handler.go +++ b/handler.go @@ -48,10 +48,7 @@ func newGzipHandler(level int, opts ...Option) *gzipHandler { config: cfg, gzPool: sync.Pool{ New: func() interface{} { - gz, err := gzip.NewWriterLevel(io.Discard, level) - if err != nil { - panic(err) - } + gz, _ := gzip.NewWriterLevel(io.Discard, level) return gz }, }, @@ -79,8 +76,8 @@ func (g *gzipHandler) Handle(c *gin.Context) { gz := g.gzPool.Get().(*gzip.Writer) defer func() { - g.gzPool.Put(gz) gz.Reset(io.Discard) + g.gzPool.Put(gz) }() gz.Reset(c.Writer)