diff --git a/echo.go b/echo.go index 49ad6bf7..7c41aaed 100644 --- a/echo.go +++ b/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 { diff --git a/engine/engine.go b/engine/engine.go index 37e1c542..a5d50075 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -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) diff --git a/engine/fasthttp/server.go b/engine/fasthttp/server.go index a03fbe40..551c805e 100644 --- a/engine/fasthttp/server.go +++ b/engine/fasthttp/server.go @@ -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 diff --git a/engine/standard/server.go b/engine/standard/server.go index 4b8d98fc..27dcfbc8 100644 --- a/engine/standard/server.go +++ b/engine/standard/server.go @@ -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 diff --git a/router_test.go b/router_test.go index f9c573c7..d6f6a4b9 100644 --- a/router_test.go +++ b/router_test.go @@ -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)