Skip to content

Commit

Permalink
New API Echo#GetContext & Echo#PutContext.
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Mar 18, 2016
1 parent e159f4d commit 3946a18
Showing 5 changed files with 47 additions and 24 deletions.
31 changes: 17 additions & 14 deletions echo.go
Original file line number Diff line number Diff line change
@@ -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 {
4 changes: 2 additions & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
@@ -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)

8 changes: 4 additions & 4 deletions engine/fasthttp/server.go
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions engine/standard/server.go
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions router_test.go
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3946a18

Please sign in to comment.