From 3cd1d5be653f6441a4bffe621051d779e39c171d Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 8 Feb 2016 17:26:00 -0800 Subject: [PATCH] Fixed sync.Pool Signed-off-by: Vishal Rana --- echo.go | 10 +++++++++- engine/standard/server.go | 8 ++++---- middleware/auth.go | 2 +- middleware/compress.go | 2 +- middleware/log.go | 2 +- middleware/middleware.go | 15 --------------- middleware/recover.go | 2 +- 7 files changed, 17 insertions(+), 24 deletions(-) delete mode 100644 middleware/middleware.go diff --git a/echo.go b/echo.go index 16ce1971..5fd1d7d2 100644 --- a/echo.go +++ b/echo.go @@ -55,8 +55,12 @@ type ( message string } - // Middleware interface{} + Middleware interface { + Process(HandlerFunc) HandlerFunc + } + MiddlewareFunc func(HandlerFunc) HandlerFunc + // Handler interface{} HandlerFunc func(Context) error @@ -224,6 +228,10 @@ func New() (e *Echo) { return } +func (f MiddlewareFunc) Process(h HandlerFunc) HandlerFunc { + return f(h) +} + // Router returns router. func (e *Echo) Router() *Router { return e.router diff --git a/engine/standard/server.go b/engine/standard/server.go index d33736ce..267c8fbb 100644 --- a/engine/standard/server.go +++ b/engine/standard/server.go @@ -61,15 +61,15 @@ func (s *Server) Start() { s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Request req := s.pool.request.Get().(*Request) - reqHdr := s.pool.request.Get().(*Header) - reqURL := s.pool.request.Get().(*URL) + reqHdr := s.pool.header.Get().(*Header) + reqURL := s.pool.url.Get().(*URL) reqHdr.reset(r.Header) reqURL.reset(r.URL) req.reset(r, reqHdr, reqURL) // Response - res := s.pool.request.Get().(*Response) - resHdr := s.pool.request.Get().(*Header) + res := s.pool.response.Get().(*Response) + resHdr := s.pool.header.Get().(*Header) res.reset(w, reqHdr) s.handler(req, res) diff --git a/middleware/auth.go b/middleware/auth.go index f6d567a7..7a8a4afc 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -19,7 +19,7 @@ const ( // // For valid credentials it calls the next handler. // For invalid credentials, it sends "401 - Unauthorized" response. -func BasicAuth(fn BasicValidateFunc) MiddlewareFunc { +func BasicAuth(fn BasicValidateFunc) echo.MiddlewareFunc { return func(h echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { // Skip WebSocket diff --git a/middleware/compress.go b/middleware/compress.go index 5066d367..42d1fc66 100644 --- a/middleware/compress.go +++ b/middleware/compress.go @@ -48,7 +48,7 @@ var writerPool = sync.Pool{ // Gzip returns a middleware which compresses HTTP response using gzip compression // scheme. -func Gzip() MiddlewareFunc { +func Gzip() echo.MiddlewareFunc { return func(h echo.HandlerFunc) echo.HandlerFunc { scheme := "gzip" diff --git a/middleware/log.go b/middleware/log.go index 1fe39816..37dee998 100644 --- a/middleware/log.go +++ b/middleware/log.go @@ -8,7 +8,7 @@ import ( "github.com/labstack/gommon/color" ) -func Log() MiddlewareFunc { +func Log() echo.MiddlewareFunc { return func(h echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { req := c.Request() diff --git a/middleware/middleware.go b/middleware/middleware.go deleted file mode 100644 index a6edffe8..00000000 --- a/middleware/middleware.go +++ /dev/null @@ -1,15 +0,0 @@ -package middleware - -import "github.com/labstack/echo" - -type ( - Middleware interface { - Process(echo.HandlerFunc) echo.HandlerFunc - } - - MiddlewareFunc func(echo.HandlerFunc) echo.HandlerFunc -) - -func (f MiddlewareFunc) Process(h echo.HandlerFunc) echo.HandlerFunc { - return f(h) -} diff --git a/middleware/recover.go b/middleware/recover.go index 27de5141..f641f276 100644 --- a/middleware/recover.go +++ b/middleware/recover.go @@ -10,7 +10,7 @@ import ( // Recover returns a middleware which recovers from panics anywhere in the chain // and handles the control to the centralized HTTPErrorHandler. -func Recover() MiddlewareFunc { +func Recover() echo.MiddlewareFunc { return func(h echo.HandlerFunc) echo.HandlerFunc { // TODO: Provide better stack trace `https://github.com/go-errors/errors` `https://github.com/docker/libcontainer/tree/master/stacktrace` return func(c echo.Context) error {