|
|
@ -19,43 +19,45 @@ var ( |
|
|
// Set stores a value for a given key in a given request.
|
|
|
// Set stores a value for a given key in a given request.
|
|
|
func Set(r *http.Request, key, val interface{}) { |
|
|
func Set(r *http.Request, key, val interface{}) { |
|
|
mutex.Lock() |
|
|
mutex.Lock() |
|
|
defer mutex.Unlock() |
|
|
|
|
|
if data[r] == nil { |
|
|
if data[r] == nil { |
|
|
data[r] = make(map[interface{}]interface{}) |
|
|
data[r] = make(map[interface{}]interface{}) |
|
|
datat[r] = time.Now().Unix() |
|
|
datat[r] = time.Now().Unix() |
|
|
} |
|
|
} |
|
|
data[r][key] = val |
|
|
data[r][key] = val |
|
|
|
|
|
mutex.Unlock() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Get returns a value stored for a given key in a given request.
|
|
|
// Get returns a value stored for a given key in a given request.
|
|
|
func Get(r *http.Request, key interface{}) interface{} { |
|
|
func Get(r *http.Request, key interface{}) interface{} { |
|
|
mutex.RLock() |
|
|
mutex.RLock() |
|
|
defer mutex.RUnlock() |
|
|
|
|
|
if data[r] != nil { |
|
|
if data[r] != nil { |
|
|
|
|
|
mutex.RUnlock() |
|
|
return data[r][key] |
|
|
return data[r][key] |
|
|
} |
|
|
} |
|
|
|
|
|
mutex.RUnlock() |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// GetOk returns stored value and presence state like multi-value return of map access.
|
|
|
// GetOk returns stored value and presence state like multi-value return of map access.
|
|
|
func GetOk(r *http.Request, key interface{}) (interface{}, bool) { |
|
|
func GetOk(r *http.Request, key interface{}) (interface{}, bool) { |
|
|
mutex.RLock() |
|
|
mutex.RLock() |
|
|
defer mutex.RUnlock() |
|
|
|
|
|
if _, ok := data[r]; ok { |
|
|
if _, ok := data[r]; ok { |
|
|
value, ok := data[r][key] |
|
|
value, ok := data[r][key] |
|
|
|
|
|
mutex.RUnlock() |
|
|
return value, ok |
|
|
return value, ok |
|
|
} |
|
|
} |
|
|
|
|
|
mutex.RUnlock() |
|
|
return nil, false |
|
|
return nil, false |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// GetAll returns all stored values for the request as a map. Nil is returned for invalid requests.
|
|
|
// GetAll returns all stored values for the request as a map. Nil is returned for invalid requests.
|
|
|
func GetAll(r *http.Request) map[interface{}]interface{} { |
|
|
func GetAll(r *http.Request) map[interface{}]interface{} { |
|
|
mutex.RLock() |
|
|
mutex.RLock() |
|
|
defer mutex.RUnlock() |
|
|
|
|
|
|
|
|
|
|
|
if context, ok := data[r]; ok { |
|
|
if context, ok := data[r]; ok { |
|
|
|
|
|
mutex.RUnlock() |
|
|
return context |
|
|
return context |
|
|
} |
|
|
} |
|
|
|
|
|
mutex.RUnlock() |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -63,19 +65,18 @@ func GetAll(r *http.Request) map[interface{}]interface{} { |
|
|
// ok if the request was never registered.
|
|
|
// ok if the request was never registered.
|
|
|
func GetAllOk(r *http.Request) (map[interface{}]interface{}, bool) { |
|
|
func GetAllOk(r *http.Request) (map[interface{}]interface{}, bool) { |
|
|
mutex.RLock() |
|
|
mutex.RLock() |
|
|
defer mutex.RUnlock() |
|
|
|
|
|
|
|
|
|
|
|
context, ok := data[r] |
|
|
context, ok := data[r] |
|
|
|
|
|
mutex.RUnlock() |
|
|
return context, ok |
|
|
return context, ok |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Delete removes a value stored for a given key in a given request.
|
|
|
// Delete removes a value stored for a given key in a given request.
|
|
|
func Delete(r *http.Request, key interface{}) { |
|
|
func Delete(r *http.Request, key interface{}) { |
|
|
mutex.Lock() |
|
|
mutex.Lock() |
|
|
defer mutex.Unlock() |
|
|
|
|
|
if data[r] != nil { |
|
|
if data[r] != nil { |
|
|
delete(data[r], key) |
|
|
delete(data[r], key) |
|
|
} |
|
|
} |
|
|
|
|
|
mutex.Unlock() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Clear removes all values stored for a given request.
|
|
|
// Clear removes all values stored for a given request.
|
|
|
@ -84,8 +85,8 @@ func Delete(r *http.Request, key interface{}) { |
|
|
// variables at the end of a request lifetime. See ClearHandler().
|
|
|
// variables at the end of a request lifetime. See ClearHandler().
|
|
|
func Clear(r *http.Request) { |
|
|
func Clear(r *http.Request) { |
|
|
mutex.Lock() |
|
|
mutex.Lock() |
|
|
defer mutex.Unlock() |
|
|
|
|
|
clear(r) |
|
|
clear(r) |
|
|
|
|
|
mutex.Unlock() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// clear is Clear without the lock.
|
|
|
// clear is Clear without the lock.
|
|
|
@ -105,7 +106,6 @@ func clear(r *http.Request) { |
|
|
// periodically until the problem is fixed.
|
|
|
// periodically until the problem is fixed.
|
|
|
func Purge(maxAge int) int { |
|
|
func Purge(maxAge int) int { |
|
|
mutex.Lock() |
|
|
mutex.Lock() |
|
|
defer mutex.Unlock() |
|
|
|
|
|
count := 0 |
|
|
count := 0 |
|
|
if maxAge <= 0 { |
|
|
if maxAge <= 0 { |
|
|
count = len(data) |
|
|
count = len(data) |
|
|
@ -120,6 +120,7 @@ func Purge(maxAge int) int { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
mutex.Unlock() |
|
|
return count |
|
|
return count |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|