Skip to content

Commit

Permalink
Propagate request context behaviour to gin context methods implementi…
Browse files Browse the repository at this point in the history
…ng stdlib context.Context interface.

It makes possible to correctly cancel handlers when request cancelled (connection closed, etc.)
Try to extract value from request context when it was not found in gin context

Signed-off-by: Vladimir Stolyarov <[email protected]>
  • Loading branch information
xakep666 authored and Vladimir Stolyarov committed Mar 8, 2019
1 parent 70a0aba commit edc4df5
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,22 +985,22 @@ func (c *Context) SetAccepted(formats ...string) {
c.Accepted = formats
}

/************************************/
/***** GOLANG.ORG/X/NET/CONTEXT *****/
/************************************/
/*****************************************/
/***** STDLIB CONTEXT IMPLEMENTATION *****/
/*****************************************/

// Deadline returns the time when work done on behalf of this context
// should be canceled. Deadline returns ok==false when no deadline is
// set. Successive calls to Deadline return the same results.
func (c *Context) Deadline() (deadline time.Time, ok bool) {
return
return c.Request.Context().Deadline()
}

// Done returns a channel that's closed when work done on behalf of this
// context should be canceled. Done may return nil if this context can
// never be canceled. Successive calls to Done return the same value.
func (c *Context) Done() <-chan struct{} {
return nil
return c.Request.Context().Done()
}

// Err returns a non-nil error value after Done is closed,
Expand All @@ -1010,7 +1010,7 @@ func (c *Context) Done() <-chan struct{} {
// Canceled if the context was canceled
// or DeadlineExceeded if the context's deadline passed.
func (c *Context) Err() error {
return nil
return c.Request.Context().Err()
}

// Value returns the value associated with this context for key, or nil
Expand All @@ -1021,8 +1021,10 @@ func (c *Context) Value(key interface{}) interface{} {
return c.Request
}
if keyAsString, ok := key.(string); ok {
val, _ := c.Get(keyAsString)
return val
val, exists := c.Get(keyAsString)
if exists {
return val
}
}
return nil
return c.Request.Context().Value(key)
}

0 comments on commit edc4df5

Please sign in to comment.