Make session Get and Set concurrency safe

This commit is contained in:
Ola
2022-09-30 18:19:05 +02:00
parent a3564f4d61
commit 429cbb6053
2 changed files with 56 additions and 8 deletions
+6
View File
@@ -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]
}