mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
New API Echo#GetContext & Echo#PutContext.
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
e159f4d7ef
commit
3946a1839c
31
echo.go
31
echo.go
@ -187,7 +187,6 @@ var (
|
||||
func New() (e *Echo) {
|
||||
e = &Echo{maxParam: new(int)}
|
||||
e.pool.New = func() interface{} {
|
||||
// NOTE: v2
|
||||
return NewContext(nil, nil, e)
|
||||
}
|
||||
e.router = NewRouter(e)
|
||||
@ -198,14 +197,9 @@ func New() (e *Echo) {
|
||||
e.pristineHead = e.head
|
||||
e.chainMiddleware()
|
||||
|
||||
//----------
|
||||
// Defaults
|
||||
//----------
|
||||
|
||||
e.SetHTTPErrorHandler(e.DefaultHTTPErrorHandler)
|
||||
e.SetBinder(&binder{})
|
||||
|
||||
// Logger
|
||||
e.logger = log.New("echo")
|
||||
e.logger.SetLevel(log.FATAL)
|
||||
|
||||
@ -388,9 +382,6 @@ func (e *Echo) add(method, path string, handler Handler, middleware ...Middlewar
|
||||
for i := len(middleware) - 1; i >= 0; i-- {
|
||||
h = middleware[i].Handle(h)
|
||||
}
|
||||
// for _, m := range middleware {
|
||||
// h = m.Handle(h)
|
||||
// }
|
||||
return h.Handle(c)
|
||||
}), e)
|
||||
r := Route{
|
||||
@ -443,6 +434,18 @@ func (e *Echo) Routes() []Route {
|
||||
return e.router.routes
|
||||
}
|
||||
|
||||
// GetContext returns `Context` from the sync.Pool. You must return the context by
|
||||
// calling `PutContext()`.
|
||||
func (e *Echo) GetContext() Context {
|
||||
return e.pool.Get().(Context)
|
||||
}
|
||||
|
||||
// PutContext returns `Context` instance back to the sync.Pool. You must call it after
|
||||
// `GetContext()`.
|
||||
func (e *Echo) PutContext(c Context) {
|
||||
e.pool.Put(c)
|
||||
}
|
||||
|
||||
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
||||
c := e.pool.Get().(*context)
|
||||
c.reset(req, res)
|
||||
@ -455,11 +458,11 @@ func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
||||
e.pool.Put(c)
|
||||
}
|
||||
|
||||
// Run starts the HTTP engine.
|
||||
func (e *Echo) Run(eng engine.Engine) {
|
||||
eng.SetHandler(e)
|
||||
eng.SetLogger(e.logger)
|
||||
eng.Start()
|
||||
// Run starts the HTTP server.
|
||||
func (e *Echo) Run(s engine.Server) {
|
||||
s.SetHandler(e)
|
||||
s.SetLogger(e.logger)
|
||||
s.Start()
|
||||
}
|
||||
|
||||
func NewHTTPError(code int, msg ...string) *HTTPError {
|
||||
|
@ -9,8 +9,8 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// Engine defines the interface for HTTP server.
|
||||
Engine interface {
|
||||
// Server defines the interface for HTTP server.
|
||||
Server interface {
|
||||
// SetHandler sets the handler for the HTTP server.
|
||||
SetHandler(Handler)
|
||||
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// Server implements `engine.Engine`.
|
||||
// Server implements `engine.Server`.
|
||||
Server struct {
|
||||
config engine.Config
|
||||
handler engine.Handler
|
||||
@ -84,17 +84,17 @@ func NewFromConfig(c engine.Config) (s *Server) {
|
||||
return
|
||||
}
|
||||
|
||||
// SetHandler implements `engine.Engine#SetHandler` method.
|
||||
// SetHandler implements `engine.Server#SetHandler` method.
|
||||
func (s *Server) SetHandler(h engine.Handler) {
|
||||
s.handler = h
|
||||
}
|
||||
|
||||
// SetLogger implements `engine.Engine#SetLogger` method.
|
||||
// SetLogger implements `engine.Server#SetLogger` method.
|
||||
func (s *Server) SetLogger(l *log.Logger) {
|
||||
s.logger = l
|
||||
}
|
||||
|
||||
// Start implements `engine.Engine#Start` method.
|
||||
// Start implements `engine.Server#Start` method.
|
||||
func (s *Server) Start() {
|
||||
handler := func(c *fasthttp.RequestCtx) {
|
||||
// Request
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
// Server implements `engine.Engine`.
|
||||
// Server implements `engine.Server`.
|
||||
Server struct {
|
||||
*http.Server
|
||||
config engine.Config
|
||||
@ -80,17 +80,17 @@ func NewFromConfig(c engine.Config) (s *Server) {
|
||||
return
|
||||
}
|
||||
|
||||
// SetHandler implements `engine.Engine#SetHandler` method.
|
||||
// SetHandler implements `engine.Server#SetHandler` method.
|
||||
func (s *Server) SetHandler(h engine.Handler) {
|
||||
s.handler = h
|
||||
}
|
||||
|
||||
// SetLogger implements `engine.Engine#SetLogger` method.
|
||||
// SetLogger implements `engine.Server#SetLogger` method.
|
||||
func (s *Server) SetLogger(l *log.Logger) {
|
||||
s.logger = l
|
||||
}
|
||||
|
||||
// Start implements `engine.Engine#Start` method.
|
||||
// Start implements `engine.Server#Start` method.
|
||||
func (s *Server) Start() {
|
||||
certfile := s.config.TLSCertfile
|
||||
keyfile := s.config.TLSKeyfile
|
||||
|
@ -572,6 +572,26 @@ func TestRouterAPI(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRouterGitHubAPI(b *testing.B) {
|
||||
e := New()
|
||||
r := e.router
|
||||
|
||||
// Add routes
|
||||
for _, route := range api {
|
||||
r.Add(route.Method, route.Path, HandlerFunc(func(c Context) error {
|
||||
return nil
|
||||
}), e)
|
||||
}
|
||||
|
||||
// Find routes
|
||||
c := e.pool.Get().(*context)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, route := range api {
|
||||
r.Find(route.Method, route.Path, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *node) printTree(pfx string, tail bool) {
|
||||
p := prefix(tail, pfx, "└── ", "├── ")
|
||||
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.kind, n.parent, n.methodHandler)
|
||||
|
Loading…
Reference in New Issue
Block a user