77 "net"
88 "net/http"
99 "strings"
10+ "time"
1011
1112 "golang.org/x/net/context"
1213 "golang.org/x/net/websocket"
@@ -23,8 +24,6 @@ type Param struct {
2324// It is therefore safe to read values by the index.
2425type Params []Param
2526
26- // type store map[string]interface{}
27-
2827// Context is the context interface type
2928type Context interface {
3029 context.Context
@@ -58,18 +57,16 @@ type Context interface {
5857// Ctx encapsulates the http request, response context
5958type Ctx struct {
6059 context.Context
61- request * http.Request
62- response * Response
63- websocket * websocket.Conn
64- params Params
65- handlers HandlersChain
66- handlerName string
67- // store store
60+ request * http.Request
61+ response * Response
62+ websocket * websocket.Conn
63+ params Params
64+ handlers HandlersChain
65+ handlerName string
6866 index int
6967 formParsed bool
7068 multipartFormParsed bool
7169 parent Context
72- // m *sync.RWMutex
7370}
7471
7572var _ context.Context = & Ctx {}
@@ -186,53 +183,20 @@ func (c *Ctx) ParseMultipartForm(maxMemory int64) error {
186183 return nil
187184}
188185
189- // Set is used to store a new key/value pair exclusivelly for thisContext.
190- // It also lazy initializes c.Keys if it was not used previously.
186+ // Set is used to store a new key/value pair using the
187+ // golang.org/x/net/context contained on this Context.
188+ // It is a shortcut for context.WithValue(..., ...)
191189func (c * Ctx ) Set (key interface {}, value interface {}) {
192-
193- // must double check this is thread safe
194- // if c.Context == nil {
195- // c.Context = context.Background()
196- // }
197-
198190 c .Context = context .WithValue (c .Context , key , value )
199- // if c.store == nil {
200-
201- // if c.m == nil {
202- // c.m = new(sync.RWMutex)
203- // }
204-
205- // c.m.Lock()
206- // c.store = make(store)
207- // } else {
208- // c.m.Lock()
209- // }
210-
211- // c.store[key] = value
212- // c.m.Unlock()
213191}
214192
215- // Get returns the value for the given key, ie: (value, true).
216- // If the value does not exists it returns (nil, false)
193+ // Get returns the value for the given key and is a shortcut
194+ // for the golang.org/x/net/context context.Value(...) ... but it
195+ // also returns if the value was found or not.
217196func (c * Ctx ) Get (key interface {}) (value interface {}, exists bool ) {
218-
219197 value = c .Context .Value (key )
220-
221- if value != nil {
222- exists = true
223- }
224-
198+ exists = value != nil
225199 return
226- // must double check this is thread safe
227- // if c.Context == nil {
228- // c.Context = context.Background()
229- // }
230- // if c.store != nil {
231- // c.m.RLock()
232- // value, exists = c.store[key]
233- // c.m.RUnlock()
234- // }
235- // return
236200}
237201
238202// Next should be used only inside middleware.
@@ -442,3 +406,37 @@ func (c *Ctx) Inline(r io.Reader, filename string) (err error) {
442406
443407 return
444408}
409+
410+ // golang.org/x/net/context Overrides to keep context update on lars.Context object
411+
412+ // WithCancel calls embedded golang.org/x/net/context WithCancel and automatically
413+ // 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
417+ return
418+ }
419+
420+ // WithDeadline calls embedded golang.org/x/net/context WithDeadline and automatically
421+ // 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
425+ return
426+ }
427+
428+ // WithTimeout calls embedded golang.org/x/net/context WithTimeout and automatically
429+ // 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
433+ return
434+ }
435+
436+ // WithValue calls embedded golang.org/x/net/context WithValue and automatically
437+ // updates context on the containing las.Context object.
438+ // 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
442+ }
0 commit comments