1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 05:11:31 +00:00

add any level compress

This commit is contained in:
JessonChan 2016-01-03 15:35:32 +08:00
parent 59fa248292
commit ee2322e83b

View File

@ -40,9 +40,9 @@ func (n nopResetWriter) Reset(w io.Writer) {
//do nothing //do nothing
} }
type encodeFunc func(io.Writer, int) (resetWriter, error)
type acceptEncoder struct { type acceptEncoder struct {
name string name string
levelEncode func(int) resetWriter
bestSpeedPool *sync.Pool bestSpeedPool *sync.Pool
bestCompressionPool *sync.Pool bestCompressionPool *sync.Pool
} }
@ -52,10 +52,13 @@ func (ac acceptEncoder) encode(wr io.Writer, level int) (resetWriter, error) {
return nopResetWriter{wr}, nil return nopResetWriter{wr}, nil
} }
var rwr resetWriter var rwr resetWriter
if level == flate.BestSpeed { switch level {
case flate.BestSpeed:
rwr = ac.bestSpeedPool.Get().(resetWriter) rwr = ac.bestSpeedPool.Get().(resetWriter)
} else { case flate.BestCompression:
rwr = ac.bestCompressionPool.Get().(resetWriter) rwr = ac.bestCompressionPool.Get().(resetWriter)
default:
rwr = ac.levelEncode(level)
} }
rwr.Reset(wr) rwr.Reset(wr)
return rwr, nil return rwr, nil
@ -75,8 +78,9 @@ func (ac acceptEncoder) put(wr resetWriter, level int) {
} }
var ( var (
noneCompressEncoder = acceptEncoder{"", nil, nil} noneCompressEncoder = acceptEncoder{"", nil, nil, nil}
gzipCompressEncoder = acceptEncoder{"gzip", gzipCompressEncoder = acceptEncoder{"gzip",
func(level int) resetWriter { wr, _ := gzip.NewWriterLevel(nil, level); return wr },
&sync.Pool{ &sync.Pool{
New: func() interface{} { wr, _ := gzip.NewWriterLevel(nil, flate.BestSpeed); return wr }, New: func() interface{} { wr, _ := gzip.NewWriterLevel(nil, flate.BestSpeed); return wr },
}, },
@ -90,6 +94,7 @@ var (
//The "zlib" format defined in RFC 1950 [31] in combination with //The "zlib" format defined in RFC 1950 [31] in combination with
//the "deflate" compression mechanism described in RFC 1951 [29]. //the "deflate" compression mechanism described in RFC 1951 [29].
deflateCompressEncoder = acceptEncoder{"deflate", deflateCompressEncoder = acceptEncoder{"deflate",
func(level int) resetWriter { wr, _ := zlib.NewWriterLevel(nil, level); return wr },
&sync.Pool{ &sync.Pool{
New: func() interface{} { wr, _ := zlib.NewWriterLevel(nil, flate.BestSpeed); return wr }, New: func() interface{} { wr, _ := zlib.NewWriterLevel(nil, flate.BestSpeed); return wr },
}, },