Compare commits

..

2 Commits

2 changed files with 18 additions and 3 deletions

View File

@ -95,8 +95,9 @@ type Context struct {
// the browser to send this cookie along with cross-site requests. // the browser to send this cookie along with cross-site requests.
sameSite http.SameSite sameSite http.SameSite
internalContextMu sync.RWMutex internalContextMu sync.RWMutex
internalContext context.Context internalContext context.Context
internalContextCancelCause context.CancelCauseFunc
} }
/************************************/ /************************************/
@ -1424,7 +1425,7 @@ func (c *Context) WithInternalContext(ctx context.Context) {
c.internalContextMu.Lock() c.internalContextMu.Lock()
defer c.internalContextMu.Unlock() defer c.internalContextMu.Unlock()
c.internalContext = ctx c.internalContext, c.internalContextCancelCause = context.WithCancelCause(ctx)
} }
// InternalContext provides the currently stored internal context in a thread safe manner. // InternalContext provides the currently stored internal context in a thread safe manner.

14
gin.go
View File

@ -674,6 +674,20 @@ func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c.Request = req c.Request = req
c.reset() c.reset()
// If we're using internalContext then we need to pass on errors from the request context
if c.useInternalContext() {
reqCtx := req.Context()
go func() {
<-reqCtx.Done()
if err := reqCtx.Err(); err != nil {
c.internalContextMu.RLock()
defer c.internalContextMu.RUnlock()
c.internalContextCancelCause(err)
}
}()
}
engine.handleHTTPRequest(c) engine.handleHTTPRequest(c)
engine.pool.Put(c) engine.pool.Put(c)