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: case Handler:
f.Meta = &FuncMeta{Handler: fn} f.Meta = &FuncMeta{Handler: fn}
return return
case func(*Context): // case func(*Context):
f.Meta = &FuncMeta{Handler: fn} // f.Meta = &FuncMeta{Handler: fn}
return // return
case func(*Context) error: case func(*Context) error:
f.Meta = &FuncMeta{HandlerWithErr: fn} f.Meta = &FuncMeta{HandlerWithErr: fn}
return 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. // 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. // 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. // Handlers is just a type of slice of []Handler.
// //
// See `Handler` for more. // See `Handler` for more.
type Handlers []Handler type Handlers = []Handler
func valueOf(v interface{}) reflect.Value { func valueOf(v interface{}) reflect.Value {
if val, ok := v.(reflect.Value); ok { if val, ok := v.(reflect.Value); ok {

View File

@ -1,5 +1,9 @@
package iris package iris
import (
"github.com/kataras/iris/v12/context"
)
// ContextPool is a pool of T. // ContextPool is a pool of T.
// //
// See `NewContextWrapper` and `ContextPool` for more. // 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. // 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. // It returns a new handler which expects a T instead of iris.Context.
// The T is the type of the pool. // 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 passed to the handler as an argument.
// The T is not shared between requests. // The T is not shared between requests.
func (w *ContextWrapper[T]) Handler(handler func(T)) Handler { func (w *ContextWrapper[T]) Handler(handler func(T)) Handler {
if handler == nil {
return nil
}
return func(ctx Context) { return func(ctx Context) {
newT := w.pool.Acquire(ctx) newT := w.pool.Acquire(ctx)
handler(newT) handler(newT)
w.pool.Release(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) { switch h := handler.(type) {
case context.Handler: case context.Handler:
return h return h
case func(*context.Context): // case func(*context.Context):
return h // return h
case http.Handler: case http.Handler:
// handlerFunc.ServeHTTP(w,r) // handlerFunc.ServeHTTP(w,r)
return func(ctx *context.Context) { return func(ctx *context.Context) {

View File

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

View File

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