Skip to content

Commit

Permalink
feat: add gzip pool to reuse the reader (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm authored Jan 14, 2025
1 parent d55d1f6 commit 5823c50
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,12 @@ func decodeXML(r io.Reader, v any) error {
return nil
}

func decompressGzip(r io.ReadCloser) (io.ReadCloser, error) {
nr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}

gz := &gzipReader{s: r, r: nr}
var gzipPool = sync.Pool{New: func() any { return new(gzip.Reader) }}

return gz, nil
func decompressGzip(r io.ReadCloser) (io.ReadCloser, error) {
gr := gzipPool.Get().(*gzip.Reader)
err := gr.Reset(r)
return &gzipReader{s: r, r: gr}, err
}

type gzipReader struct {
Expand All @@ -102,7 +99,8 @@ func (gz *gzipReader) Read(p []byte) (n int, err error) {
}

func (gz *gzipReader) Close() error {
closeq(gz.r)
gz.r.Reset(nopReader{})
gzipPool.Put(gz.r)
closeq(gz.s)
return nil
}
Expand All @@ -125,9 +123,8 @@ func (d *deflateReader) Read(p []byte) (n int, err error) {
}

func (d *deflateReader) Close() error {
if err := d.r.(flate.Resetter).Reset(nopReader{}, nil); err == nil {
flatePool.Put(d.r)
}
d.r.(flate.Resetter).Reset(nopReader{}, nil)
flatePool.Put(d.r)
closeq(d.s)
return nil
}
Expand Down

0 comments on commit 5823c50

Please sign in to comment.