1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-04-01 08:51:18 -07:00
parent 1113413441
commit cfb60bebd3
5 changed files with 60 additions and 42 deletions

View File

@ -125,25 +125,26 @@ func (s *Server) startCustomListener() error {
func (s *Server) ServeHTTP(c *fasthttp.RequestCtx) {
// Request
rq := 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())
rq.reset(c, reqHdr, reqURL)
rqHdr := s.pool.requestHeader.Get().(*RequestHeader)
rqURL := s.pool.url.Get().(*URL)
rqHdr.reset(&c.Request.Header)
rqURL.reset(c.URI())
rq.reset(c, rqHdr, rqURL)
// Response
rs := s.pool.response.Get().(*Response)
resHdr := s.pool.responseHeader.Get().(*ResponseHeader)
resHdr.reset(&c.Response.Header)
rs.reset(c, resHdr)
rsHdr := s.pool.responseHeader.Get().(*ResponseHeader)
rsHdr.reset(&c.Response.Header)
rs.reset(c, rsHdr)
s.handler.ServeHTTP(rq, rs)
// Return to pool
s.pool.request.Put(rq)
s.pool.requestHeader.Put(reqHdr)
s.pool.url.Put(reqURL)
s.pool.requestHeader.Put(rqHdr)
s.pool.url.Put(rqURL)
s.pool.response.Put(rs)
s.pool.responseHeader.Put(resHdr)
s.pool.responseHeader.Put(rsHdr)
}
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.

View File

@ -99,8 +99,8 @@ func (r *Response) CloseNotify() <-chan bool {
return r.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
r.ResponseWriter = w
func (r *Response) reset(w http.ResponseWriter, a *responseAdapter, h engine.Header) {
r.ResponseWriter = a
r.header = h
r.status = http.StatusOK
r.size = 0
@ -127,3 +127,8 @@ func (r *responseAdapter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
func (r *responseAdapter) CloseNotify() <-chan bool {
return r.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
func (r *responseAdapter) reset(rw http.ResponseWriter, rq *Response) {
r.ResponseWriter = rw
r.Response = rq
}

View File

@ -20,10 +20,11 @@ type (
}
pool struct {
request sync.Pool
response sync.Pool
header sync.Pool
url sync.Pool
request sync.Pool
response sync.Pool
responseAdapter sync.Pool
header sync.Pool
url sync.Pool
}
)
@ -59,6 +60,11 @@ func NewFromConfig(c engine.Config) (s *Server) {
return &Response{logger: s.logger}
},
},
responseAdapter: sync.Pool{
New: func() interface{} {
return &responseAdapter{}
},
},
header: sync.Pool{
New: func() interface{} {
return &Header{}
@ -114,25 +120,28 @@ func (s *Server) startCustomListener() error {
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Request
rq := s.pool.request.Get().(*Request)
reqHdr := s.pool.header.Get().(*Header)
reqURL := s.pool.url.Get().(*URL)
reqHdr.reset(r.Header)
reqURL.reset(r.URL)
rq.reset(r, reqHdr, reqURL)
rqHdr := s.pool.header.Get().(*Header)
rqURL := s.pool.url.Get().(*URL)
rqHdr.reset(r.Header)
rqURL.reset(r.URL)
rq.reset(r, rqHdr, rqURL)
// Response
rs := s.pool.response.Get().(*Response)
resHdr := s.pool.header.Get().(*Header)
resHdr.reset(w.Header())
rs.reset(w, resHdr)
rsAdpt := s.pool.responseAdapter.Get().(*responseAdapter)
rsAdpt.reset(w, rs)
rsHdr := s.pool.header.Get().(*Header)
rsHdr.reset(w.Header())
rs.reset(w, rsAdpt, rsHdr)
s.handler.ServeHTTP(rq, rs)
// Return to pool
s.pool.request.Put(rq)
s.pool.header.Put(reqHdr)
s.pool.url.Put(reqURL)
s.pool.header.Put(rqHdr)
s.pool.url.Put(rqURL)
s.pool.response.Put(rs)
s.pool.header.Put(resHdr)
s.pool.header.Put(rsHdr)
}
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
@ -140,11 +149,7 @@ func WrapHandler(h http.Handler) echo.HandlerFunc {
return func(c echo.Context) error {
rq := c.Request().(*Request)
rs := c.Response().(*Response)
w := &responseAdapter{
ResponseWriter: rs.ResponseWriter,
Response: rs,
}
h.ServeHTTP(w, rq.Request)
h.ServeHTTP(rs.ResponseWriter, rq.Request)
return nil
}
}
@ -155,13 +160,9 @@ func WrapMiddleware(m func(http.Handler) http.Handler) echo.MiddlewareFunc {
return echo.HandlerFunc(func(c echo.Context) (err error) {
rq := c.Request().(*Request)
rs := c.Response().(*Response)
w := &responseAdapter{
ResponseWriter: rs.ResponseWriter,
Response: rs,
}
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = next.Handle(c)
})).ServeHTTP(w, rq.Request)
})).ServeHTTP(rs.ResponseWriter, rq.Request)
return
})
}

16
glide.lock generated
View File

@ -1,5 +1,5 @@
hash: e740b7b4143d21e271e337a73d785c7c17a3c7ae1c775b7513287bb1599f3f0a
updated: 2016-03-29T07:21:14.848378979-07:00
hash: 44dfc8aaffca5078e71afdb209a0ef0a359a35f69fb98c7b6a2fb87a5a70e757
updated: 2016-04-01T08:48:26.453457926-07:00
imports:
- name: github.com/klauspost/compress
version: 9d711f4445beb7f6488148ce04e3b4dc6e72242d
@ -11,6 +11,10 @@ imports:
version: 09cded8978dc9e80714c4d85b0322337b0a1e5e0
- name: github.com/klauspost/crc32
version: 19b0b332c9e4516a6370a0456e6182c3b5036720
- name: github.com/labstack/echo
version: 11134134413a234260d4e9c501803c3cc6c874d1
subpackages:
- engine
- name: github.com/labstack/gommon
version: f4ba73f8bcf88df1524069c117f7878cf530ee4e
subpackages:
@ -20,12 +24,16 @@ imports:
version: 9cbef7c35391cca05f15f8181dc0b18bc9736dbb
- name: github.com/mattn/go-isatty
version: 56b76bdf51f7708750eac80fa38b952bb9f32639
- name: github.com/stretchr/testify
version: 6fe211e493929a8aac0469b93f28b1d0688a9a3a
subpackages:
- assert
- name: github.com/valyala/fasthttp
version: 12ba9899844060189449e9768dc0900a56d3624b
version: ca21b21eba267eac4489fb6b42260129727c4a1b
- name: github.com/valyala/fasttemplate
version: 3b874956e03f1636d171bda64b130f9135f42cff
- name: golang.org/x/net
version: 31df19d69da8728e9220def59b80ee577c3e48bf
version: 3e8a7b0329d536af18e227bb21b6da4d1dbbe180
subpackages:
- context
- websocket

View File

@ -10,3 +10,6 @@ import:
- context
- websocket
- package: github.com/valyala/fasttemplate
- package: github.com/stretchr/testify
subpackages:
- assert