Fix for : fatal error: concurrent map read and map write

This commit is contained in:
Rohan Shah 2016-09-11 12:14:26 +05:30
parent 63f05bfdc3
commit 470747bd8c

View File

@ -12,6 +12,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/gin-gonic/gin/binding"
@ -46,6 +47,7 @@ type Context struct {
engine *Engine
Keys map[string]interface{}
KeysLocker sync.RWMutex
Errors errorMsgs
Accepted []string
}
@ -160,14 +162,18 @@ func (c *Context) Set(key string, value interface{}) {
if c.Keys == nil {
c.Keys = make(map[string]interface{})
}
c.KeysLocker.Lock()
c.Keys[key] = value
c.KeysLocker.Unlock()
}
// 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.Keys != nil {
c.KeysLocker.RLock()
value, exists = c.Keys[key]
c.KeysLocker.RUnlock()
}
return
}