This commit is contained in:
Gerasimos Maropoulos 2016-07-25 14:45:12 +03:00
parent 3ee38a363c
commit d10273391a
4 changed files with 19 additions and 15 deletions

View File

@ -462,12 +462,10 @@ func (ctx *Context) Write(format string, a ...interface{}) {
// Gzip accepts bytes, which are compressed to gzip format and sent to the client
func (ctx *Context) Gzip(b []byte, status int) {
ctx.RequestCtx.Response.Header.Add("Content-Encoding", "gzip")
gzipWriter := ctx.framework.gzipWriterPool.Get().(*gzip.Writer)
gzipWriter.Reset(ctx.RequestCtx.Response.BodyWriter())
gzipWriter.Write(b)
gzipWriter.Close()
ctx.framework.gzipWriterPool.Put(gzipWriter)
_, err := fasthttp.WriteGzip(ctx.RequestCtx.Response.BodyWriter(), b)
if err == nil {
ctx.RequestCtx.Response.Header.Add("Content-Encoding", "gzip")
}
}
// RenderWithStatus builds up the response from the specified template or a response engine.

15
iris.go
View File

@ -199,10 +199,7 @@ func New(cfg ...config.Iris) *Framework {
// we always use 's' no 'f' because 's' is easier for me to remember because of 'station'
// some things never change :)
s := &Framework{
Config: &c,
gzipWriterPool: sync.Pool{New: func() interface{} {
return &gzip.Writer{}
}},
Config: &c,
responses: &responseEngines{},
Available: make(chan bool),
}
@ -852,7 +849,15 @@ func (s *Framework) URL(routeName string, args ...interface{}) (url string) {
// Note that: each iris station has its own pool
// see ReleaseGzip
func (s *Framework) AcquireGzip(w io.Writer) *gzip.Writer {
gzipWriter := s.gzipWriterPool.Get().(*gzip.Writer)
v := s.gzipWriterPool.Get()
if v == nil {
gzipWriter, err := gzip.NewWriterLevel(w, gzip.DefaultCompression)
if err != nil {
return nil
}
return gzipWriter
}
gzipWriter := v.(*gzip.Writer)
gzipWriter.Reset(w)
return gzipWriter
}

View File

@ -4,6 +4,7 @@ import (
"strings"
"github.com/iris-contrib/errors"
"github.com/valyala/fasthttp"
)
type (
@ -151,9 +152,7 @@ func (r *responseEngineMap) render(ctx *Context, obj interface{}, options ...map
ctx.SetContentType(ctype)
if gzipEnabled {
gzipWriter := ctx.framework.AcquireGzip(ctx.Response.BodyWriter())
defer ctx.framework.ReleaseGzip(gzipWriter)
_, err := gzipWriter.Write(finalResult)
_, err := fasthttp.WriteGzip(ctx.RequestCtx.Response.BodyWriter(), finalResult)
if err != nil {
return err
}

View File

@ -190,6 +190,7 @@ func (t *templateEngineWrapper) execute(ctx *Context, filename string, binding i
var out io.Writer
if gzipEnabled {
ctx.Response.Header.Add("Content-Encoding", "gzip")
gzipWriter := ctx.framework.AcquireGzip(ctx.Response.BodyWriter())
defer ctx.framework.ReleaseGzip(gzipWriter)
out = gzipWriter
@ -197,7 +198,8 @@ func (t *templateEngineWrapper) execute(ctx *Context, filename string, binding i
out = ctx.Response.BodyWriter()
}
return t.ExecuteWriter(out, filename, binding, options...)
err = t.ExecuteWriter(out, filename, binding, options...)
return err
}
// executeToString executes a template from a specific template engine and returns its contents result as string, it doesn't renders