add Pool and Handlers method helpers on the new ContextWrapper

This commit is contained in:
Gerasimos (Makis) Maropoulos 2023-11-02 15:24:45 +02:00
parent 4772177fef
commit ec69670edc
No known key found for this signature in database
GPG Key ID: 403EEB7885C79503
6 changed files with 32 additions and 9 deletions

View File

@ -53,9 +53,9 @@ func (f *Func) buildMeta() {
case Handler:
f.Meta = &FuncMeta{Handler: fn}
return
case func(*Context):
f.Meta = &FuncMeta{Handler: fn}
return
// case func(*Context):
// f.Meta = &FuncMeta{Handler: fn}
// return
case func(*Context) error:
f.Meta = &FuncMeta{HandlerWithErr: fn}
return

View File

@ -104,12 +104,12 @@ func (expr *NameExpr) MatchString(s string) bool {
//
// If Handler panics, the server (the caller of Handler) assumes that the effect of the panic was isolated to the active request.
// It recovers the panic, logs a stack trace to the server error log, and hangs up the connection.
type Handler func(*Context)
type Handler = func(*Context)
// Handlers is just a type of slice of []Handler.
//
// See `Handler` for more.
type Handlers []Handler
type Handlers = []Handler
func valueOf(v interface{}) reflect.Value {
if val, ok := v.(reflect.Value); ok {

View File

@ -1,5 +1,9 @@
package iris
import (
"github.com/kataras/iris/v12/context"
)
// ContextPool is a pool of T.
//
// See `NewContextWrapper` and `ContextPool` for more.
@ -76,6 +80,11 @@ func NewContextWrapper[T any](pool ContextPool[T]) *ContextWrapper[T] {
}
}
// Pool returns the pool, useful when manually Acquire and Release of custom context is required.
func (w *ContextWrapper[T]) Pool() ContextPool[T] {
return w.pool
}
// Handler wraps the handler with the pool's Acquire and Release methods.
// It returns a new handler which expects a T instead of iris.Context.
// The T is the type of the pool.
@ -83,9 +92,23 @@ func NewContextWrapper[T any](pool ContextPool[T]) *ContextWrapper[T] {
// The T is passed to the handler as an argument.
// The T is not shared between requests.
func (w *ContextWrapper[T]) Handler(handler func(T)) Handler {
if handler == nil {
return nil
}
return func(ctx Context) {
newT := w.pool.Acquire(ctx)
handler(newT)
w.pool.Release(newT)
}
}
// Handlers wraps the handlers with the pool's Acquire and Release methods.
func (w *ContextWrapper[T]) Handlers(handlers ...func(T)) context.Handlers {
newHandlers := make(context.Handlers, len(handlers))
for i, handler := range handlers {
newHandlers[i] = w.Handler(handler)
}
return newHandlers
}

View File

@ -18,8 +18,8 @@ func FromStd(handler interface{}) context.Handler {
switch h := handler.(type) {
case context.Handler:
return h
case func(*context.Context):
return h
// case func(*context.Context):
// return h
case http.Handler:
// handlerFunc.ServeHTTP(w,r)
return func(ctx *context.Context) {

View File

@ -1388,7 +1388,7 @@ func (api *APIBuilder) RemoveHandler(namesOrHandlers ...interface{}) Party {
switch h := nameOrHandler.(type) {
case string:
handlerName = h
case context.Handler, func(*context.Context):
case context.Handler: //, func(*context.Context):
handlerName = context.HandlerName(h)
case *int:
counter = h

View File

@ -160,7 +160,7 @@ func (r *Route) RemoveHandler(namesOrHandlers ...interface{}) (count int) {
switch h := nameOrHandler.(type) {
case string:
handlerName = h
case context.Handler, func(*context.Context):
case context.Handler: //, func(*context.Context):
handlerName = context.HandlerName(h)
default:
panic(fmt.Sprintf("remove handler: unexpected type of %T", h))