Skip to content

Commit

Permalink
Merge pull request #116 from aofei/AppV2
Browse files Browse the repository at this point in the history
AppV2: ensure isolation of handler state per request
  • Loading branch information
xushiwei committed Jun 2, 2024
2 parents 72ad5b0 + f5e250b commit f547c8e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions classfile_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ type iHandler interface {
func Gopt_AppV2_Main(app AppType, handlers ...iHandler) {
app.InitYap()
for _, h := range handlers {
reflect.ValueOf(h).Elem().Field(1).Set(reflect.ValueOf(app)) // (*handler).AppV2 = app
hVal := reflect.ValueOf(h).Elem()
hVal.FieldByName("AppV2").Set(reflect.ValueOf(app))
hType := hVal.Type()
handle := func(ctx *Context) {
// We must duplicate the handler instance for each request
// to ensure state isolation.
h2Val := reflect.New(hType).Elem()
h2Val.Set(hVal)
h2 := h2Val.Addr().Interface().(iHandler)
h2.Main(ctx)
}
switch method, path := parseClassfname(h.Classfname()); method {
case "handle":
app.Handle(path, h.Main)
app.Handle(path, handle)
default:
app.Route(strings.ToUpper(method), path, h.Main)
app.Route(strings.ToUpper(method), path, handle)
}
}
if me, ok := app.(interface{ MainEntry() }); ok {
Expand Down

0 comments on commit f547c8e

Please sign in to comment.