fix missing initial sync.RWMutex

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-03-27 09:13:48 +08:00
parent bd5ee1aae2
commit ba621af762

View File

@ -224,6 +224,10 @@ func (c *Context) Error(err error) *Error {
// Set is used to store a new key/value pair exclusively for this context.
// It also lazy initializes c.Keys if it was not used previously.
func (c *Context) Set(key string, value interface{}) {
if c.KeysMutex == nil {
c.KeysMutex = &sync.RWMutex{}
}
c.KeysMutex.Lock()
if c.Keys == nil {
c.Keys = make(map[string]interface{})
@ -236,6 +240,10 @@ func (c *Context) Set(key string, value interface{}) {
// Get returns the value for the given key, ie: (value, true).
// If the value does not exists it returns (nil, false)
func (c *Context) Get(key string) (value interface{}, exists bool) {
if c.KeysMutex == nil {
c.KeysMutex = &sync.RWMutex{}
}
c.KeysMutex.RLock()
value, exists = c.Keys[key]
c.KeysMutex.RUnlock()