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>
This commit is contained in:
parent
e071b53686
commit
eedc0c8324
2 changed files with 41 additions and 8 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue