diff --git a/context/context_func.go b/context/context_func.go index 177ce1ec..4d04d700 100644 --- a/context/context_func.go +++ b/context/context_func.go @@ -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 diff --git a/context/handler.go b/context/handler.go index bc5343f5..42d4caa3 100644 --- a/context/handler.go +++ b/context/handler.go @@ -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 { diff --git a/context_wrapper.go b/context_wrapper.go index 4a8236ce..67b718aa 100644 --- a/context_wrapper.go +++ b/context_wrapper.go @@ -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 +} diff --git a/core/handlerconv/from_std.go b/core/handlerconv/from_std.go index 8a4ec957..18f21911 100644 --- a/core/handlerconv/from_std.go +++ b/core/handlerconv/from_std.go @@ -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) { diff --git a/core/router/api_builder.go b/core/router/api_builder.go index fa2b6dad..6987d79c 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -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 diff --git a/core/router/route.go b/core/router/route.go index 34d8684a..9caa6f15 100644 --- a/core/router/route.go +++ b/core/router/route.go @@ -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))