diff --git a/stream.go b/stream.go index 1153a2e..6c8d737 100644 --- a/stream.go +++ b/stream.go @@ -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 { @@ -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 } @@ -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 }