Make session Get and Set concurrency safe
This commit is contained in:
@@ -189,6 +189,9 @@ func (s *Session) CloseWithMsg(msg []byte) error {
|
||||
// Set is used to store a new key/value pair exclusivelly for this session.
|
||||
// It also lazy initializes s.Keys if it was not used previously.
|
||||
func (s *Session) Set(key string, value interface{}) {
|
||||
s.rwmutex.Lock()
|
||||
defer s.rwmutex.Unlock()
|
||||
|
||||
if s.Keys == nil {
|
||||
s.Keys = make(map[string]interface{})
|
||||
}
|
||||
@@ -199,6 +202,9 @@ func (s *Session) 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 (s *Session) Get(key string) (value interface{}, exists bool) {
|
||||
s.rwmutex.RLock()
|
||||
defer s.rwmutex.RUnlock()
|
||||
|
||||
if s.Keys != nil {
|
||||
value, exists = s.Keys[key]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user