Skip to content

Commit

Permalink
Deprecated Echo#GetContext() => Echo#AcquireContext(), Echo#PutContex…
Browse files Browse the repository at this point in the history
…t() => Echo#ReleaseContext()

Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed May 4, 2016
1 parent d819560 commit 682a558
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
3 changes: 2 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ type (
ServeContent(io.ReadSeeker, string, time.Time) error

// Reset resets the context after request completes. It must be called along
// with `Echo#GetContext()` and `Echo#PutContext()`. See `Echo#ServeHTTP()`
// with `Echo#AcquireContext()` and `Echo#ReleaseContext()`.
// See `Echo#ServeHTTP()`
Reset(engine.Request, engine.Response)
}

Expand Down
20 changes: 15 additions & 5 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,18 +515,28 @@ func (e *Echo) Routes() []Route {
return e.router.routes
}

// GetContext returns `Context` from the sync.Pool. You must return the context by
// calling `PutContext()`.
// AcquireContext returns an empty `Context` instance from the pool.
// You must be return the context by calling `ReleaseContext()`.
func (e *Echo) AcquireContext() Context {
return e.pool.Get().(Context)
}

// GetContext is deprecated, use `AcquireContext()` instead.
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) {
// ReleaseContext returns the `Context` instance back to the pool.
// You must call it after `AcquireContext()`.
func (e *Echo) ReleaseContext(c Context) {
e.pool.Put(c)
}

// PutContext is deprecated, use `ReleaseContext()` instead.
func (e *Echo) PutContext(c Context) {
e.ReleaseContext(c)
}

func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
c := e.pool.Get().(*context)
c.Reset(req, res)
Expand Down
4 changes: 2 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ func (n *node) checkMethodNotAllowed() HandlerFunc {
//
// For performance:
//
// - Get context from `Echo#GetContext()`
// - Get context from `Echo#AcquireContext()`
// - Reset it `Context#Reset()`
// - Return it `Echo#PutContext()`.
// - Return it `Echo#ReleaseContext()`.
func (r *Router) Find(method, path string, context Context) {
cn := r.tree // Current node as root

Expand Down
4 changes: 2 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,10 @@ func BenchmarkRouterGitHubAPI(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, route := range api {
// c := e.pool.Get().(*context)
c := e.GetContext()
c := e.AcquireContext()
r.Find(route.Method, route.Path, c)
// router.Find(r.Method, r.Path, c)
e.PutContext(c)
e.ReleaseContext(c)
// e.pool.Put(c)
}
}
Expand Down

0 comments on commit 682a558

Please sign in to comment.