Skip to content

Commit

Permalink
Modify c.Request.Context() when using ContextWithFallback
Browse files Browse the repository at this point in the history
  • Loading branch information
bvisness committed Oct 18, 2022
1 parent 33ab0fc commit 4a3008f
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 121 deletions.
40 changes: 29 additions & 11 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gin

import (
"context"
"errors"
"io"
"io/ioutil"
Expand Down Expand Up @@ -50,9 +51,10 @@ const abortIndex int8 = math.MaxInt8 >> 1
// Context is the most important part of gin. It allows us to pass variables between middleware,
// manage the flow, validate the JSON of a request and render a JSON response for example.
type Context struct {
writermem responseWriter
Request *http.Request
Writer ResponseWriter
writermem responseWriter
Request *http.Request
Writer ResponseWriter
requestContext context.Context

Params Params
handlers HandlersChain
Expand Down Expand Up @@ -108,6 +110,22 @@ func (c *Context) reset() {
*c.skippedNodes = (*c.skippedNodes)[:0]
}

// Initializes c.Request and c.requestContext according to the ContextWithFallback feature flag
func (c *Context) setRequest(req *http.Request) {
if req == nil {
c.Request = nil
c.requestContext = nil
return
}

c.requestContext = req.Context()
if c.engine.ContextWithFallback {
c.Request = req.WithContext(c)
} else {
c.Request = req
}
}

// Copy returns a copy of the current context that can be safely used outside the request's scope.
// This has to be used when the context has to be passed to a goroutine.
func (c *Context) Copy() *Context {
Expand Down Expand Up @@ -1173,26 +1191,26 @@ func (c *Context) SetAccepted(formats ...string) {

// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
func (c *Context) Deadline() (deadline time.Time, ok bool) {
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
if !c.engine.ContextWithFallback || c.Request == nil || c.requestContext == nil {
return
}
return c.Request.Context().Deadline()
return c.requestContext.Deadline()
}

// Done returns nil (chan which will wait forever) when c.Request has no Context.
func (c *Context) Done() <-chan struct{} {
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
if !c.engine.ContextWithFallback || c.Request == nil || c.requestContext == nil {
return nil
}
return c.Request.Context().Done()
return c.requestContext.Done()
}

// Err returns nil when c.Request has no Context.
func (c *Context) Err() error {
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
if !c.engine.ContextWithFallback || c.Request == nil || c.requestContext == nil {
return nil
}
return c.Request.Context().Err()
return c.requestContext.Err()
}

// Value returns the value associated with this context for key, or nil
Expand All @@ -1210,8 +1228,8 @@ func (c *Context) Value(key any) any {
return val
}
}
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
if !c.engine.ContextWithFallback || c.Request == nil || c.requestContext == nil {
return nil
}
return c.Request.Context().Value(key)
return c.requestContext.Value(key)
}
Loading

0 comments on commit 4a3008f

Please sign in to comment.