1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

Sync pool for fasthttp

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-02-21 19:58:19 -08:00
parent e1a7549855
commit c19442d023
7 changed files with 88 additions and 11 deletions

View File

@ -300,6 +300,7 @@ func (c *context) Error(err error) {
c.echo.httpErrorHandler(err, c)
}
// Echo returns the `Echo` instance.
func (c *context) Echo() *Echo {
return c.echo
}

View File

@ -34,6 +34,10 @@ func (h *RequestHeader) Object() interface{} {
return h.header
}
func (h *RequestHeader) reset(hdr fasthttp.RequestHeader) {
h.header = hdr
}
func (h *ResponseHeader) Add(key, val string) {
// h.header.Add(key, val)
}
@ -53,3 +57,7 @@ func (h *ResponseHeader) Set(key, val string) {
func (h *ResponseHeader) Object() interface{} {
return h.header
}
func (h *ResponseHeader) reset(hdr fasthttp.ResponseHeader) {
h.header = hdr
}

View File

@ -64,3 +64,9 @@ func (r *Request) FormValue(name string) string {
func (r *Request) Object() interface{} {
return r.context
}
func (r *Request) reset(c *fasthttp.RequestCtx, h engine.Header, u engine.URL) {
r.context = c
r.header = h
r.url = u
}

View File

@ -4,6 +4,7 @@ package fasthttp
import (
"io"
"net/http"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
@ -73,3 +74,12 @@ func (r *Response) SetWriter(w io.Writer) {
func (r *Response) Writer() io.Writer {
return r.writer
}
func (r *Response) reset(c *fasthttp.RequestCtx, h engine.Header) {
r.context = c
r.header = h
r.status = http.StatusOK
r.size = 0
r.committed = false
r.writer = c
}

View File

@ -4,6 +4,7 @@ package fasthttp
import (
"net/http"
"sync"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/logger"
@ -16,8 +17,17 @@ type (
*http.Server
config *engine.Config
handler engine.HandlerFunc
pool *Pool
logger logger.Logger
}
Pool struct {
request sync.Pool
response sync.Pool
requestHeader sync.Pool
responseHeader sync.Pool
url sync.Pool
}
)
func New(addr string) *Server {
@ -38,8 +48,35 @@ func NewConfig(c *engine.Config) (s *Server) {
s = &Server{
Server: new(http.Server),
config: c,
pool: &Pool{
request: sync.Pool{
New: func() interface{} {
return &Request{}
},
},
response: sync.Pool{
New: func() interface{} {
return &Response{logger: s.logger}
},
},
requestHeader: sync.Pool{
New: func() interface{} {
return &RequestHeader{}
},
},
responseHeader: sync.Pool{
New: func() interface{} {
return &ResponseHeader{}
},
},
url: sync.Pool{
New: func() interface{} {
return &URL{}
},
},
},
handler: func(req engine.Request, res engine.Response) {
s.logger.Info("handler not set")
s.logger.Warn("handler not set")
},
logger: log.New("echo"),
}
@ -56,16 +93,27 @@ func (s *Server) SetLogger(l logger.Logger) {
func (s *Server) Start() {
fasthttp.ListenAndServe(s.config.Address, func(c *fasthttp.RequestCtx) {
req := &Request{
context: c,
url: &URL{c.URI()},
header: &RequestHeader{c.Request.Header},
}
res := &Response{
context: c,
header: &ResponseHeader{c.Response.Header},
}
// Request
req := s.pool.request.Get().(*Request)
reqHdr := s.pool.requestHeader.Get().(*RequestHeader)
reqURL := s.pool.url.Get().(*URL)
reqHdr.reset(c.Request.Header)
reqURL.reset(c.URI())
req.reset(c, reqHdr, reqURL)
// Response
res := s.pool.response.Get().(*Response)
resHdr := s.pool.responseHeader.Get().(*ResponseHeader)
resHdr.reset(c.Response.Header)
res.reset(c, resHdr)
s.handler(req, res)
s.pool.request.Put(req)
s.pool.requestHeader.Put(reqHdr)
s.pool.url.Put(reqURL)
s.pool.response.Put(res)
s.pool.responseHeader.Put(resHdr)
})
s.logger.Fatal(s.ListenAndServe())
}

View File

@ -33,3 +33,7 @@ func (u *URL) QueryValue(name string) string {
func (u *URL) Object() interface{} {
return u.url
}
func (u *URL) reset(url *fasthttp.URI) {
u.url = url
}

View File

@ -67,7 +67,7 @@ func NewConfig(c *engine.Config) (s *Server) {
},
},
handler: func(req engine.Request, res engine.Response) {
s.logger.Info("handler not set")
s.logger.Warn("handler not set")
},
logger: log.New("echo"),
}