diff options
author | zeripath <art27@cantab.net> | 2021-05-06 00:30:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-06 01:30:25 +0200 |
commit | eedc0c8324820817f4c06603ec3195a1d3513542 (patch) | |
tree | ad14f7d5baf7ffe8fe73d8704dd84193d8f84e4b /modules/web | |
parent | e071b53686572c6a31c17655fbfa0035945cfb59 (diff) | |
download | gitea-eedc0c8324820817f4c06603ec3195a1d3513542.tar.gz gitea-eedc0c8324820817f4c06603ec3195a1d3513542.zip |
Defer closing the gitrepo until the end of the wrapped context functions (#15653)
There was a mistake in #15372 where deferral of gitrepo close occurs before it should.
This PR fixes this.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/web')
-rw-r--r-- | modules/web/route.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/modules/web/route.go b/modules/web/route.go index 6f9e76bdf3..319d08f598 100644 --- a/modules/web/route.go +++ b/modules/web/route.go @@ -5,6 +5,7 @@ package web import ( + goctx "context" "fmt" "net/http" "reflect" @@ -27,6 +28,7 @@ func Wrap(handlers ...interface{}) http.HandlerFunc { switch t := handler.(type) { case http.HandlerFunc, func(http.ResponseWriter, *http.Request), func(ctx *context.Context), + func(ctx *context.Context) goctx.CancelFunc, func(*context.APIContext), func(*context.PrivateContext), func(http.Handler) http.Handler: @@ -48,6 +50,15 @@ func Wrap(handlers ...interface{}) http.HandlerFunc { if r, ok := resp.(context.ResponseWriter); ok && r.Status() > 0 { return } + case func(ctx *context.Context) goctx.CancelFunc: + ctx := context.GetContext(req) + cancel := t(ctx) + if cancel != nil { + defer cancel() + } + if ctx.Written() { + return + } case func(ctx *context.Context): ctx := context.GetContext(req) t(ctx) @@ -94,6 +105,23 @@ func Middle(f func(ctx *context.Context)) func(netx http.Handler) http.Handler { } } +// MiddleCancel wrap a context function as a chi middleware +func MiddleCancel(f func(ctx *context.Context) goctx.CancelFunc) func(netx http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { + ctx := context.GetContext(req) + cancel := f(ctx) + if cancel != nil { + defer cancel() + } + if ctx.Written() { + return + } + next.ServeHTTP(ctx.Resp, ctx.Req) + }) + } +} + // MiddleAPI wrap a context function as a chi middleware func MiddleAPI(f func(ctx *context.APIContext)) func(netx http.Handler) http.Handler { return func(next http.Handler) http.Handler { @@ -163,6 +191,8 @@ func (r *Route) Use(middlewares ...interface{}) { r.R.Use(t) case func(*context.Context): r.R.Use(Middle(t)) + case func(*context.Context) goctx.CancelFunc: + r.R.Use(MiddleCancel(t)) case func(*context.APIContext): r.R.Use(MiddleAPI(t)) default: |