From 62d3587b6fa2e927f6a9a58f9920d31afd2b9661 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Wed, 14 Mar 2018 14:03:59 -0700 Subject: [PATCH] Minor performance fixes Signed-off-by: Vishal Rana --- echo.go | 4 +++- response.go | 24 +++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/echo.go b/echo.go index 713f005c..209e6ae2 100644 --- a/echo.go +++ b/echo.go @@ -560,7 +560,6 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Acquire context c := e.pool.Get().(*context) - defer e.pool.Put(c) c.Reset(r, w) // Middleware @@ -587,6 +586,9 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err := h(c); err != nil { e.HTTPErrorHandler(err, c) } + + // Release context + e.pool.Put(c) } // Start starts an HTTP server. diff --git a/response.go b/response.go index 93204098..6244783b 100644 --- a/response.go +++ b/response.go @@ -4,7 +4,6 @@ import ( "bufio" "net" "net/http" - "strconv" ) type ( @@ -12,14 +11,13 @@ type ( // by an HTTP handler to construct an HTTP response. // See: https://golang.org/pkg/net/http/#ResponseWriter Response struct { - echo *Echo - contentLength int64 - beforeFuncs []func() - afterFuncs []func() - Writer http.ResponseWriter - Status int - Size int64 - Committed bool + echo *Echo + beforeFuncs []func() + afterFuncs []func() + Writer http.ResponseWriter + Status int + Size int64 + Committed bool } ) @@ -64,7 +62,6 @@ func (r *Response) WriteHeader(code int) { r.Status = code r.Writer.WriteHeader(code) r.Committed = true - r.contentLength, _ = strconv.ParseInt(r.Header().Get(HeaderContentLength), 10, 0) } // Write writes the data to the connection as part of an HTTP reply. @@ -74,10 +71,8 @@ func (r *Response) Write(b []byte) (n int, err error) { } n, err = r.Writer.Write(b) r.Size += int64(n) - if r.Size == r.contentLength { - for _, fn := range r.afterFuncs { - fn() - } + for _, fn := range r.afterFuncs { + fn() } return } @@ -106,7 +101,6 @@ func (r *Response) CloseNotify() <-chan bool { } func (r *Response) reset(w http.ResponseWriter) { - r.contentLength = 0 r.beforeFuncs = nil r.afterFuncs = nil r.Writer = w