Skip to content

Commit ec69670

Browse files
committed
add Pool and Handlers method helpers on the new ContextWrapper
1 parent 4772177 commit ec69670

File tree

6 files changed

+32
-9
lines changed

6 files changed

+32
-9
lines changed

context/context_func.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ func (f *Func) buildMeta() {
5353
case Handler:
5454
f.Meta = &FuncMeta{Handler: fn}
5555
return
56-
case func(*Context):
57-
f.Meta = &FuncMeta{Handler: fn}
58-
return
56+
// case func(*Context):
57+
// f.Meta = &FuncMeta{Handler: fn}
58+
// return
5959
case func(*Context) error:
6060
f.Meta = &FuncMeta{HandlerWithErr: fn}
6161
return

context/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ func (expr *NameExpr) MatchString(s string) bool {
104104
//
105105
// If Handler panics, the server (the caller of Handler) assumes that the effect of the panic was isolated to the active request.
106106
// It recovers the panic, logs a stack trace to the server error log, and hangs up the connection.
107-
type Handler func(*Context)
107+
type Handler = func(*Context)
108108

109109
// Handlers is just a type of slice of []Handler.
110110
//
111111
// See `Handler` for more.
112-
type Handlers []Handler
112+
type Handlers = []Handler
113113

114114
func valueOf(v interface{}) reflect.Value {
115115
if val, ok := v.(reflect.Value); ok {

context_wrapper.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package iris
22

3+
import (
4+
"github.com/kataras/iris/v12/context"
5+
)
6+
37
// ContextPool is a pool of T.
48
//
59
// See `NewContextWrapper` and `ContextPool` for more.
@@ -76,16 +80,35 @@ func NewContextWrapper[T any](pool ContextPool[T]) *ContextWrapper[T] {
7680
}
7781
}
7882

83+
// Pool returns the pool, useful when manually Acquire and Release of custom context is required.
84+
func (w *ContextWrapper[T]) Pool() ContextPool[T] {
85+
return w.pool
86+
}
87+
7988
// Handler wraps the handler with the pool's Acquire and Release methods.
8089
// It returns a new handler which expects a T instead of iris.Context.
8190
// The T is the type of the pool.
8291
// The T is acquired from the pool and released back to the pool after the handler's execution.
8392
// The T is passed to the handler as an argument.
8493
// The T is not shared between requests.
8594
func (w *ContextWrapper[T]) Handler(handler func(T)) Handler {
95+
if handler == nil {
96+
return nil
97+
}
98+
8699
return func(ctx Context) {
87100
newT := w.pool.Acquire(ctx)
88101
handler(newT)
89102
w.pool.Release(newT)
90103
}
91104
}
105+
106+
// Handlers wraps the handlers with the pool's Acquire and Release methods.
107+
func (w *ContextWrapper[T]) Handlers(handlers ...func(T)) context.Handlers {
108+
newHandlers := make(context.Handlers, len(handlers))
109+
for i, handler := range handlers {
110+
newHandlers[i] = w.Handler(handler)
111+
}
112+
113+
return newHandlers
114+
}

core/handlerconv/from_std.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ func FromStd(handler interface{}) context.Handler {
1818
switch h := handler.(type) {
1919
case context.Handler:
2020
return h
21-
case func(*context.Context):
22-
return h
21+
// case func(*context.Context):
22+
// return h
2323
case http.Handler:
2424
// handlerFunc.ServeHTTP(w,r)
2525
return func(ctx *context.Context) {

core/router/api_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ func (api *APIBuilder) RemoveHandler(namesOrHandlers ...interface{}) Party {
13881388
switch h := nameOrHandler.(type) {
13891389
case string:
13901390
handlerName = h
1391-
case context.Handler, func(*context.Context):
1391+
case context.Handler: //, func(*context.Context):
13921392
handlerName = context.HandlerName(h)
13931393
case *int:
13941394
counter = h

core/router/route.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (r *Route) RemoveHandler(namesOrHandlers ...interface{}) (count int) {
160160
switch h := nameOrHandler.(type) {
161161
case string:
162162
handlerName = h
163-
case context.Handler, func(*context.Context):
163+
case context.Handler: //, func(*context.Context):
164164
handlerName = context.HandlerName(h)
165165
default:
166166
panic(fmt.Sprintf("remove handler: unexpected type of %T", h))

0 commit comments

Comments
 (0)