2019-08-22 09:27:18 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
2020-01-17 21:57:18 +00:00
|
|
|
|
|
|
|
"github.com/klauspost/compress/gzip"
|
2019-08-22 09:27:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetGzipReader returns new gzip reader from the pool.
|
|
|
|
//
|
|
|
|
// Return back the gzip reader when it no longer needed with PutGzipReader.
|
|
|
|
func GetGzipReader(r io.Reader) (*gzip.Reader, error) {
|
|
|
|
v := gzipReaderPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return gzip.NewReader(r)
|
|
|
|
}
|
|
|
|
zr := v.(*gzip.Reader)
|
|
|
|
if err := zr.Reset(r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return zr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutGzipReader returns back gzip reader obtained via GetGzipReader.
|
|
|
|
func PutGzipReader(zr *gzip.Reader) {
|
|
|
|
_ = zr.Close()
|
|
|
|
gzipReaderPool.Put(zr)
|
|
|
|
}
|
|
|
|
|
|
|
|
var gzipReaderPool sync.Pool
|