https://github.com/mostynb/zstdpool-freelist
https://github.com/mostynb/zstdpool-freelist
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/mostynb/zstdpool-freelist
- Owner: mostynb
- License: apache-2.0
- Created: 2020-11-19T21:30:55.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-29T11:32:18.000Z (over 5 years ago)
- Last Synced: 2025-02-01T13:29:49.125Z (over 1 year ago)
- Language: Go
- Size: 11.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zstdpool
Zstdpool provides flexible pool implementations for the Encoder and Decoder
types in github.com/klauspost/compress/zstd which do not leak goroutines.
## Why not use sync.Pool?
`zstd.Encoder` leaks goroutines if it is garbage collected without `Close()`
being called first. So we can't safely put an unclosed encoder in a sync.Pool.
But encoders cannot be reused after being closed, so we can't put a closed
encoder in a sync.Pool either.
`zstd.Decoder`s can be reused after being closed, so you can close them before
placing them in a sync.Pool, but doing so frees resources that we would want
to keep until the decoder is no longer used.
These problems might be possible to work around with finalizers, but it is
difficult to confirm is working as expected, and could silently break
with internal changes in github.com/klauspost/compress/zstd.
## Status
This code is not yet well tested, and the API may change at any time.
## Usage
### Encoding
```Go
var encPool = zstdpool.NewEncoderPool()
func compressStream(in io.Reader, out io.Writer) error {
enc, err := encPool.Get(out)
if err != nil {
return err
}
defer encPool.Put(enc)
_, err = enc.ReadFrom(in)
return err
}
```
### Decoding
```Go
var decPool = zstdpool.NewDecoderPool()
func decompressStream(in io.Reader, out io.Writer) error {
dec, err := decPool.Get(in)
if err != nil {
return err
}
defer decPool.Put(dec)
_, err = dec.WriteTo(out)
return err
}
```
## Contributions
Contributions are always welcome.
## License
This code is released under the [Apache 2.0 license](LICENSE).