mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
improvements of the new ContextWrapper
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
@@ -8,22 +10,12 @@ func main() {
|
||||
// 1. Create the iris app instance.
|
||||
app := iris.New()
|
||||
|
||||
/*
|
||||
w := iris.NewContextWrapper(&iris.DefaultContextPool[*myCustomContext]{
|
||||
AcquireFunc: func(ctx iris.Context) *myCustomContext {
|
||||
return &myCustomContext{
|
||||
Context: ctx,
|
||||
// custom fields here...
|
||||
}
|
||||
},
|
||||
ReleaseFunc: func(t *myCustomContext) {
|
||||
// do nothing
|
||||
},
|
||||
})
|
||||
OR: */
|
||||
// 2. Create the Context Wrapper which will be used to wrap the handlers
|
||||
// that expect a *myCustomContext instead of iris.Context.
|
||||
w := iris.NewContextWrapper(&myCustomContextPool{})
|
||||
// OR:
|
||||
// w := iris.NewContextWrapper(iris.NewContextPool[myCustomContext, *myCustomContext]())
|
||||
// The example custom context pool operates exactly the same as the result of iris.NewContextPool.
|
||||
|
||||
// 3. Register the handler(s) which expects a *myCustomContext instead of iris.Context.
|
||||
// The `w.Handler` will wrap the handler and will call the `Acquire` and `Release`
|
||||
@@ -38,6 +30,38 @@ func index(ctx *myCustomContext) {
|
||||
ctx.HTML("<h1>Hello, World!</h1>")
|
||||
}
|
||||
|
||||
/*
|
||||
Custom Context Pool
|
||||
*/
|
||||
// Create the context sync pool for our custom context,
|
||||
// the pool must implement Acquire() T and Release(T) methods to satisfy the iris.ContextPool interface.
|
||||
type myCustomContextPool struct {
|
||||
pool sync.Pool
|
||||
}
|
||||
|
||||
// Acquire returns a new custom context from the pool.
|
||||
func (p *myCustomContextPool) Acquire(ctx iris.Context) *myCustomContext {
|
||||
v := p.pool.Get()
|
||||
if v == nil {
|
||||
v = &myCustomContext{
|
||||
Context: ctx,
|
||||
// custom fields here...
|
||||
}
|
||||
}
|
||||
|
||||
return v.(*myCustomContext)
|
||||
}
|
||||
|
||||
// Release puts a custom context back to the pool.
|
||||
func (p *myCustomContextPool) Release(t *myCustomContext) {
|
||||
// You can take advantage of this method to clear the context
|
||||
// and re-use it on the Acquire method, use the sync.Pool.
|
||||
p.pool.Put(t)
|
||||
}
|
||||
|
||||
/*
|
||||
Custom Context
|
||||
*/
|
||||
// Create a custom context.
|
||||
type myCustomContext struct {
|
||||
// It's just an embedded field which is set on AcquireFunc,
|
||||
@@ -47,28 +71,16 @@ type myCustomContext struct {
|
||||
iris.Context
|
||||
}
|
||||
|
||||
// SetContext sets the original iris.Context,
|
||||
// should be implemented by custom context type(s) when
|
||||
// the ContextWrapper uses a context Pool through the iris.NewContextPool function.
|
||||
// Comment line 15, uncomment line 17 and the method below.
|
||||
func (c *myCustomContext) SetContext(ctx iris.Context) {
|
||||
c.Context = ctx
|
||||
}
|
||||
|
||||
func (c *myCustomContext) HTML(format string, args ...interface{}) (int, error) {
|
||||
c.Application().Logger().Info("HTML was called from custom Context")
|
||||
|
||||
return c.Context.HTML(format, args...)
|
||||
}
|
||||
|
||||
// Create the context memory pool for your custom context,
|
||||
// the pool must contain Acquire() T and Release(T) methods.
|
||||
type myCustomContextPool struct{}
|
||||
|
||||
// Acquire returns a new custom context from the pool.
|
||||
func (p *myCustomContextPool) Acquire(ctx iris.Context) *myCustomContext {
|
||||
return &myCustomContext{
|
||||
Context: ctx,
|
||||
// custom fields here...
|
||||
}
|
||||
}
|
||||
|
||||
// Release puts a custom context back to the pool.
|
||||
func (p *myCustomContextPool) Release(t *myCustomContext) {
|
||||
// You can take advantage of this method to clear the context
|
||||
// and re-use it on the Acquire method, use the sync.Pool.
|
||||
//
|
||||
// We do nothing for the shake of the exampel.
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user