context: inherits context cancelation and deadline from http.Request context for Go>=1.7 (#1690)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
*gin.Context implements standard context.Context methods, but always returns data as context is still valid. Since Go 1.7, http.Request now contains a context.Context object, which can be controlled by the http.Server to indicates that the context is now closed, and persue of request should be canceled. This implements the propagation of http.Request context methods inside gin.Context to have HTTP context cancelation information at gin.Context level.
authored by Romain Beuque · committed by thinkerou · parent 59695e7 · Browse files · 4 files·+118 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: Romain Beuque <romain.beuque@corp.ovh.com>
Diff stats
4 files+118 added−28 removed
- other:+118 added−28 removed
Provenance
- Commit
f67d7a9- Tree
ffcd301- Author
Romain Beuque <rbeuque74@gmail.com>- Committer
thinkerou <thinkerou@gmail.com>- Parent
59695e7
4 files changed+118 added−28 removed
modified:context_17_test.go+49 added
5 unmodified lines6 7 package gin8 9 import (10+ "bytes"11+ "context"12 "net/http"13 "net/http/httptest"14 "testing"15+ "time"16 17 "github.com/stretchr/testify/assert"18 )19 7 unmodified lines27 assert.Equal(t, http.StatusCreated, w.Code)28 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())29 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))30 }31+32+func TestContextHTTPContext(t *testing.T) {33+ c, _ := CreateTestContext(httptest.NewRecorder())34+ req, _ := http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))35+ ctx, cancelFunc := context.WithCancel(context.Background())36+ defer cancelFunc()37+ c.Request = req.WithContext(ctx)38+39+ assert.NoError(t, c.Err())40+ assert.NotNil(t, c.Done())41+ select {42+ case <-c.Done():43+ assert.Fail(t, "context should not be canceled")44+ default:45+ }46+47+ ti, ok := c.Deadline()48+ assert.Equal(t, ti, time.Time{})49+ assert.False(t, ok)50+ assert.Equal(t, c.Value(0), c.Request)51+52+ cancelFunc()53+ assert.NotNil(t, c.Done())54+ select {55+ case <-c.Done():56+ default:57+ assert.Fail(t, "context should be canceled")58+ }59+}60+61+func TestContextHTTPContextWithDeadline(t *testing.T) {62+ c, _ := CreateTestContext(httptest.NewRecorder())63+ req, _ := http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))64+ location, _ := time.LoadLocation("Europe/Paris")65+ assert.NotNil(t, location)66+ date := time.Date(2031, 12, 27, 16, 00, 00, 00, location)67+ ctx, cancelFunc := context.WithDeadline(context.Background(), date)68+ defer cancelFunc()69+ c.Request = req.WithContext(ctx)70+71+ assert.NoError(t, c.Err())72+73+ ti, ok := c.Deadline()74+ assert.Equal(t, ti, date)75+ assert.True(t, ok)76+}modified:context_17.go+30 added
5 unmodified lines6 7 package gin8 9 import (10+ "time"11+12 "github.com/gin-gonic/gin/render"13 )14 15 // PureJSON serializes the given struct as JSON into the response body.16 // PureJSON, unlike JSON, does not replace special html characters with their unicode entities.17 func (c *Context) PureJSON(code int, obj interface{}) {18 c.Render(code, render.PureJSON{Data: obj})19 }20+21+/************************************/22+/***** GOLANG.ORG/X/NET/CONTEXT *****/23+/************************************/24+25+// Deadline returns the time when work done on behalf of this context26+// should be canceled. Deadline returns ok==false when no deadline is27+// set. Successive calls to Deadline return the same results.28+func (c *Context) Deadline() (time.Time, bool) {29+ return c.Request.Context().Deadline()30+}31+32+// Done returns a channel that's closed when work done on behalf of this33+// context should be canceled. Done may return nil if this context can34+// never be canceled. Successive calls to Done return the same value.35+func (c *Context) Done() <-chan struct{} {36+ return c.Request.Context().Done()37+}38+39+// Err returns a non-nil error value after Done is closed,40+// successive calls to Err return the same error.41+// If Done is not yet closed, Err returns nil.42+// If Done is closed, Err returns a non-nil error explaining why:43+// Canceled if the context was canceled44+// or DeadlineExceeded if the context's deadline passed.45+func (c *Context) Err() error {46+ return c.Request.Context().Err()47+}added:context_pre17.go+39 added
1+// Copyright 2018 Gin Core Team. All rights reserved.2+// Use of this source code is governed by a MIT style3+// license that can be found in the LICENSE file.4+5+// +build !go1.76+7+package gin8+9+import (10+ "time"11+)12+13+/************************************/14+/***** GOLANG.ORG/X/NET/CONTEXT *****/15+/************************************/16+17+// Deadline returns the time when work done on behalf of this context18+// should be canceled. Deadline returns ok==false when no deadline is19+// set. Successive calls to Deadline return the same results.20+func (c *Context) Deadline() (deadline time.Time, ok bool) {21+ return22+}23+24+// Done returns a channel that's closed when work done on behalf of this25+// context should be canceled. Done may return nil if this context can26+// never be canceled. Successive calls to Done return the same value.27+func (c *Context) Done() <-chan struct{} {28+ return nil29+}30+31+// Err returns a non-nil error value after Done is closed,32+// successive calls to Err return the same error.33+// If Done is not yet closed, Err returns nil.34+// If Done is closed, Err returns a non-nil error explaining why:35+// Canceled if the context was canceled36+// or DeadlineExceeded if the context's deadline passed.37+func (c *Context) Err() error {38+ return nil39+}modified:context.go-28 removed
940 unmodified lines941 func (c *Context) SetAccepted(formats ...string) {942 c.Accepted = formats943 }944 945-/************************************/946-/***** GOLANG.ORG/X/NET/CONTEXT *****/947-/************************************/948-949-// Deadline returns the time when work done on behalf of this context950-// should be canceled. Deadline returns ok==false when no deadline is951-// set. Successive calls to Deadline return the same results.952-func (c *Context) Deadline() (deadline time.Time, ok bool) {953- return954-}955-956-// Done returns a channel that's closed when work done on behalf of this957-// context should be canceled. Done may return nil if this context can958-// never be canceled. Successive calls to Done return the same value.959-func (c *Context) Done() <-chan struct{} {960- return nil961-}962-963-// Err returns a non-nil error value after Done is closed,964-// successive calls to Err return the same error.965-// If Done is not yet closed, Err returns nil.966-// If Done is closed, Err returns a non-nil error explaining why:967-// Canceled if the context was canceled968-// or DeadlineExceeded if the context's deadline passed.969-func (c *Context) Err() error {970- return nil971-}972-945 // Value returns the value associated with this context for key, or nil946 // if no value is associated with key. Successive calls to Value with947 // the same key returns the same result.948 func (c *Context) Value(key interface{}) interface{} {9 unmodified lines