mirror of
https://github.com/datarhei/core.git
synced 2025-09-26 20:11:29 +08:00
Use pool for buffer
This commit is contained in:
@@ -35,7 +35,7 @@ type gzipResponseWriter struct {
|
||||
wroteBody bool
|
||||
minLength int
|
||||
minLengthExceeded bool
|
||||
buffer bytes.Buffer
|
||||
buffer *bytes.Buffer
|
||||
code int
|
||||
}
|
||||
|
||||
@@ -101,6 +101,7 @@ func NewWithConfig(config Config) echo.MiddlewareFunc {
|
||||
}
|
||||
|
||||
pool := gzipPool(config)
|
||||
bpool := bufferPool()
|
||||
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
@@ -119,7 +120,11 @@ func NewWithConfig(config Config) echo.MiddlewareFunc {
|
||||
}
|
||||
rw := res.Writer
|
||||
w.Reset(rw)
|
||||
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw, minLength: config.MinLength}
|
||||
|
||||
buf := bpool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
|
||||
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw, minLength: config.MinLength, buffer: buf}
|
||||
|
||||
defer func() {
|
||||
if !grw.wroteBody {
|
||||
@@ -141,6 +146,7 @@ func NewWithConfig(config Config) echo.MiddlewareFunc {
|
||||
w.Reset(io.Discard)
|
||||
}
|
||||
w.Close()
|
||||
bpool.Put(buf)
|
||||
pool.Put(w)
|
||||
}()
|
||||
|
||||
@@ -232,3 +238,12 @@ func gzipPool(config Config) sync.Pool {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func bufferPool() sync.Pool {
|
||||
return sync.Pool{
|
||||
New: func() interface{} {
|
||||
b := &bytes.Buffer{}
|
||||
return b
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user