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 // Gzip accepts bytes, which are compressed to gzip format and sent to the client
func (ctx *Context) Gzip(b []byte, status int) { func (ctx *Context) Gzip(b []byte, status int) {
ctx.RequestCtx.Response.Header.Add("Content-Encoding", "gzip") _, err := fasthttp.WriteGzip(ctx.RequestCtx.Response.BodyWriter(), b)
gzipWriter := ctx.framework.gzipWriterPool.Get().(*gzip.Writer) if err == nil {
gzipWriter.Reset(ctx.RequestCtx.Response.BodyWriter()) ctx.RequestCtx.Response.Header.Add("Content-Encoding", "gzip")
gzipWriter.Write(b) }
gzipWriter.Close()
ctx.framework.gzipWriterPool.Put(gzipWriter)
} }
// RenderWithStatus builds up the response from the specified template or a response engine. // 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' // we always use 's' no 'f' because 's' is easier for me to remember because of 'station'
// some things never change :) // some things never change :)
s := &Framework{ s := &Framework{
Config: &c, Config: &c,
gzipWriterPool: sync.Pool{New: func() interface{} {
return &gzip.Writer{}
}},
responses: &responseEngines{}, responses: &responseEngines{},
Available: make(chan bool), 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 // Note that: each iris station has its own pool
// see ReleaseGzip // see ReleaseGzip
func (s *Framework) AcquireGzip(w io.Writer) *gzip.Writer { 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) gzipWriter.Reset(w)
return gzipWriter return gzipWriter
} }

View File

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

View File

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