Skip to content

Commit

Permalink
refactor: improve error handling and resource management in gzip utils (
Browse files Browse the repository at this point in the history
#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 <[email protected]>
  • Loading branch information
appleboy authored Jan 27, 2025
1 parent c881664 commit 3529136
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
},
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 3529136

Please sign in to comment.