mirror of
https://github.com/kataras/iris.git
synced 2025-12-19 02:47:04 +00:00
Organising kataras/go-fs. No api changes for these changes don't worry. See previous commit's description for more info.
Former-commit-id: 8af960e5e4e5f7c8816140ac912328b9c524370b
This commit is contained in:
70
compression.go
Normal file
70
compression.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package iris
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/klauspost/compress/gzip"
|
||||
)
|
||||
|
||||
// compressionPool is a wrapper of sync.Pool, to initialize a new compression writer pool
|
||||
type compressionPool struct {
|
||||
sync.Pool
|
||||
Level int
|
||||
}
|
||||
|
||||
// +------------------------------------------------------------+
|
||||
// | |
|
||||
// | GZIP |
|
||||
// | |
|
||||
// +------------------------------------------------------------+
|
||||
|
||||
// writes gzip compressed content to an underline io.Writer. It uses sync.Pool to reduce memory allocations.
|
||||
// Better performance through klauspost/compress package which provides us a gzip.Writer which is faster than Go standard's gzip package's writer.
|
||||
|
||||
// These constants are copied from the standard flate package
|
||||
// available Compressors
|
||||
const (
|
||||
NoCompressionLevel = 0
|
||||
BestSpeedLevel = 1
|
||||
BestCompressionLevel = 9
|
||||
DefaultCompressionLevel = -1
|
||||
ConstantCompressionLevel = -2 // Does only Huffman encoding
|
||||
)
|
||||
|
||||
// default writer pool with Compressor's level setted to DefaultCompressionLevel
|
||||
var gzipPool = &compressionPool{Level: DefaultCompressionLevel}
|
||||
|
||||
// AcquireGzipWriter prepares a gzip writer and returns it
|
||||
//
|
||||
// see ReleaseGzipWriter
|
||||
func acquireGzipWriter(w io.Writer) *gzip.Writer {
|
||||
v := gzipPool.Get()
|
||||
if v == nil {
|
||||
gzipWriter, err := gzip.NewWriterLevel(w, gzipPool.Level)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return gzipWriter
|
||||
}
|
||||
gzipWriter := v.(*gzip.Writer)
|
||||
gzipWriter.Reset(w)
|
||||
return gzipWriter
|
||||
}
|
||||
|
||||
// ReleaseGzipWriter called when flush/close and put the gzip writer back to the pool
|
||||
//
|
||||
// see AcquireGzipWriter
|
||||
func releaseGzipWriter(gzipWriter *gzip.Writer) {
|
||||
gzipWriter.Close()
|
||||
gzipPool.Put(gzipWriter)
|
||||
}
|
||||
|
||||
// WriteGzip writes a compressed form of p to the underlying io.Writer. The
|
||||
// compressed bytes are not necessarily flushed until the Writer is closed
|
||||
func writeGzip(w io.Writer, b []byte) (int, error) {
|
||||
gzipWriter := acquireGzipWriter(w)
|
||||
n, err := gzipWriter.Write(b)
|
||||
releaseGzipWriter(gzipWriter)
|
||||
return n, err
|
||||
}
|
||||
Reference in New Issue
Block a user