mirror of
https://github.com/datarhei/core.git
synced 2025-10-08 17:30:52 +08:00
Move content encoding in the beginning of the middleware chain, update dependencies
This commit is contained in:
55
http/middleware/compress/gogzip.go
Normal file
55
http/middleware/compress/gogzip.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package compress
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type gogzipImpl struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
func NewGoGzip(level Level) Compression {
|
||||
gzipLevel := gzip.DefaultCompression
|
||||
if level == BestCompression {
|
||||
gzipLevel = gzip.BestCompression
|
||||
} else if level == BestSpeed {
|
||||
gzipLevel = gzip.BestSpeed
|
||||
}
|
||||
|
||||
g := &gogzipImpl{
|
||||
pool: sync.Pool{
|
||||
New: func() interface{} {
|
||||
w, err := gzip.NewWriterLevel(io.Discard, gzipLevel)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return w
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *gogzipImpl) Acquire() Compressor {
|
||||
c := g.pool.Get()
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
x, ok := c.(Compressor)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
x.Reset(io.Discard)
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
func (g *gogzipImpl) Release(c Compressor) {
|
||||
c.Reset(io.Discard)
|
||||
g.pool.Put(c)
|
||||
}
|
Reference in New Issue
Block a user