Skip to content

Commit 0bcb544

Browse files
joeybloggsjoeybloggs
authored andcommitted
update for future go 1.7 Context being part of the *http.Request
1 parent 96fb4ad commit 0bcb544

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

context.go

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type Context interface {
5656

5757
// Ctx encapsulates the http request, response context
5858
type Ctx struct {
59-
context.Context
59+
netContext context.Context
6060
request *http.Request
6161
response *Response
6262
websocket *websocket.Conn
@@ -115,8 +115,7 @@ func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
115115
c.request = r
116116
c.response.reset(w)
117117
c.params = c.params[0:0]
118-
c.Context = context.Background()
119-
// c.store = nil
118+
c.netContext = context.Background() // in go 1.7 will call r.Context(), netContext will go away and be replaced with the Request objects Context
120119
c.index = -1
121120
c.handlers = nil
122121
c.formParsed = false
@@ -187,14 +186,14 @@ func (c *Ctx) ParseMultipartForm(maxMemory int64) error {
187186
// golang.org/x/net/context contained on this Context.
188187
// It is a shortcut for context.WithValue(..., ...)
189188
func (c *Ctx) Set(key interface{}, value interface{}) {
190-
c.Context = context.WithValue(c.Context, key, value)
189+
c.netContext = context.WithValue(c.netContext, key, value)
191190
}
192191

193192
// Get returns the value for the given key and is a shortcut
194193
// for the golang.org/x/net/context context.Value(...) ... but it
195194
// also returns if the value was found or not.
196195
func (c *Ctx) Get(key interface{}) (value interface{}, exists bool) {
197-
value = c.Context.Value(key)
196+
value = c.netContext.Value(key)
198197
exists = value != nil
199198
return
200199
}
@@ -407,36 +406,66 @@ func (c *Ctx) Inline(r io.Reader, filename string) (err error) {
407406
return
408407
}
409408

410-
// golang.org/x/net/context Overrides to keep context update on lars.Context object
409+
// golang.org/x/net/context functions to comply with context.Context interface and keep context update on lars.Context object
410+
411+
// Context returns the request's context. To change the context, use
412+
// WithContext.
413+
//
414+
// The returned context is always non-nil.
415+
func (c *Ctx) Context() context.Context {
416+
return c.netContext // TODO: in go 1.7 return c.request.Context()
417+
}
418+
419+
// WithContext updates the underlying request's context with to ctx
420+
// The provided ctx must be non-nil.
421+
func (c *Ctx) WithContext(ctx context.Context) {
422+
c.netContext = ctx // TODO: in go 1.7 must update Request object after calling c.request.WithContext(...)
423+
}
424+
425+
// Deadline calls the underlying golang.org/x/net/context Deadline()
426+
func (c *Ctx) Deadline() (deadline time.Time, ok bool) {
427+
return c.netContext.Deadline()
428+
}
429+
430+
// Done calls the underlying golang.org/x/net/context Done()
431+
func (c *Ctx) Done() <-chan struct{} {
432+
return c.netContext.Done()
433+
}
434+
435+
// Err calls the underlying golang.org/x/net/context Err()
436+
func (c *Ctx) Err() error {
437+
return c.netContext.Err()
438+
}
439+
440+
// Value calls the underlying golang.org/x/net/context Value()
441+
func (c *Ctx) Value(key interface{}) interface{} {
442+
return c.netContext.Value(key)
443+
}
411444

412-
// WithCancel calls embedded golang.org/x/net/context WithCancel and automatically
445+
// WithCancel calls golang.org/x/net/context WithCancel and automatically
413446
// updates context on the containing las.Context object.
414-
func (c *Ctx) WithCancel(parent context.Context) (ctx context.Context, cf context.CancelFunc) {
415-
c.Context, cf = context.WithCancel(parent)
416-
ctx = c
447+
func (c *Ctx) WithCancel() (cf context.CancelFunc) {
448+
c.netContext, cf = context.WithCancel(c.netContext)
417449
return
418450
}
419451

420-
// WithDeadline calls embedded golang.org/x/net/context WithDeadline and automatically
452+
// WithDeadline calls golang.org/x/net/context WithDeadline and automatically
421453
// updates context on the containing las.Context object.
422-
func (c *Ctx) WithDeadline(parent context.Context, deadline time.Time) (ctx context.Context, cf context.CancelFunc) {
423-
c.Context, cf = context.WithDeadline(parent, deadline)
424-
ctx = c
454+
func (c *Ctx) WithDeadline(deadline time.Time) (cf context.CancelFunc) {
455+
c.netContext, cf = context.WithDeadline(c.netContext, deadline)
425456
return
426457
}
427458

428-
// WithTimeout calls embedded golang.org/x/net/context WithTimeout and automatically
459+
// WithTimeout calls golang.org/x/net/context WithTimeout and automatically
429460
// updates context on the containing las.Context object.
430-
func (c *Ctx) WithTimeout(parent context.Context, timeout time.Duration) (ctx context.Context, cf context.CancelFunc) {
431-
c.Context, cf = context.WithTimeout(parent, timeout)
432-
ctx = c
461+
func (c *Ctx) WithTimeout(timeout time.Duration) (cf context.CancelFunc) {
462+
c.netContext, cf = context.WithTimeout(c.netContext, timeout)
433463
return
434464
}
435465

436-
// WithValue calls embedded golang.org/x/net/context WithValue and automatically
466+
// WithValue calls golang.org/x/net/context WithValue and automatically
437467
// updates context on the containing las.Context object.
438468
// Can also use Set() function on Context object (Recommended)
439-
func (c *Ctx) WithValue(parent context.Context, key interface{}, val interface{}) context.Context {
440-
c.Context = context.WithValue(parent, key, val)
441-
return c.Context
469+
func (c *Ctx) WithValue(key interface{}, val interface{}) {
470+
c.netContext = context.WithValue(c.netContext, key, val)
442471
}

context_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func TestContext(t *testing.T) {
110110
// }
111111

112112
c.params = varParams
113-
c.Context = context.Background()
113+
c.netContext = context.Background()
114114
// c.m = new(sync.RWMutex)
115115
// c.store = storeMap
116116
c.request = r
@@ -165,7 +165,7 @@ func TestContext(t *testing.T) {
165165
NotEqual(t, c.response, nil)
166166

167167
//Set
168-
Equal(t, c.Context.Value("test"), nil)
168+
Equal(t, c.netContext.Value("test"), nil)
169169

170170
// Index
171171
Equal(t, c.index, -1)

util.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var NativeChainHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Re
1616
b := c.BaseContext()
1717

1818
if b.index+1 < len(b.handlers) {
19+
// TODO: in go 1.7 make sure r is the same as c.request... request's context could have been updated in a native handler
1920
c.Next()
2021
}
2122
})
@@ -55,6 +56,7 @@ func (l *LARS) wrapHandler(h Handler) HandlerFunc {
5556
}
5657

5758
if ctx.index+1 < len(ctx.handlers) {
59+
// TODO: in go 1.7 make sure r is the same as c.request... request's context could have been updated in a native handler
5860
c.Next()
5961
}
6062
}
@@ -69,6 +71,7 @@ func (l *LARS) wrapHandler(h Handler) HandlerFunc {
6971
}
7072

7173
if ctx.index+1 < len(ctx.handlers) {
74+
// TODO: in go 1.7 make sure r is the same as c.request... request's context could have been updated in a native handler
7275
c.Next()
7376
}
7477
}

0 commit comments

Comments
 (0)