Skip to content

Commit 3529136

Browse files
authored
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 <appleboy.tw@gmail.com>
1 parent c881664 commit 3529136

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

handler.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ func newGzipHandler(level int, opts ...Option) *gzipHandler {
4848
config: cfg,
4949
gzPool: sync.Pool{
5050
New: func() interface{} {
51-
gz, err := gzip.NewWriterLevel(io.Discard, level)
52-
if err != nil {
53-
panic(err)
54-
}
51+
gz, _ := gzip.NewWriterLevel(io.Discard, level)
5552
return gz
5653
},
5754
},
@@ -79,8 +76,8 @@ func (g *gzipHandler) Handle(c *gin.Context) {
7976

8077
gz := g.gzPool.Get().(*gzip.Writer)
8178
defer func() {
82-
g.gzPool.Put(gz)
8379
gz.Reset(io.Discard)
80+
g.gzPool.Put(gz)
8481
}()
8582
gz.Reset(c.Writer)
8683

0 commit comments

Comments
 (0)