2 Star 7 Fork 6

user_499098 / imgproxy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gzippool.go 903 Bytes
一键复制 编辑 原始数据 按行查看 历史
DarthSim 提交于 2019-01-31 18:43 . Reset gzip encoder on put to pool
package main
import (
"compress/gzip"
"io"
"io/ioutil"
"sync"
)
type gzipPool struct {
mutex sync.Mutex
top *gzipPoolEntry
}
type gzipPoolEntry struct {
gz *gzip.Writer
next *gzipPoolEntry
}
func newGzipPool(n int) *gzipPool {
pool := new(gzipPool)
for i := 0; i < n; i++ {
pool.grow()
}
return pool
}
func (p *gzipPool) grow() {
gz, err := gzip.NewWriterLevel(ioutil.Discard, conf.GZipCompression)
if err != nil {
logFatal("Can't init GZip compression: %s", err)
}
p.top = &gzipPoolEntry{
gz: gz,
next: p.top,
}
}
func (p *gzipPool) Get(w io.Writer) *gzip.Writer {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.top == nil {
p.grow()
}
gz := p.top.gz
gz.Reset(w)
p.top = p.top.next
return gz
}
func (p *gzipPool) Put(gz *gzip.Writer) {
p.mutex.Lock()
defer p.mutex.Unlock()
gz.Reset(ioutil.Discard)
p.top = &gzipPoolEntry{gz: gz, next: p.top}
}
Go
1
https://gitee.com/yunwisdoms/imgproxy.git
git@gitee.com:yunwisdoms/imgproxy.git
yunwisdoms
imgproxy
imgproxy
master

搜索帮助