resolve deadlock problem with mutexes.

This commit is contained in:
Miroslav Šedivý 2020-12-03 20:05:44 +01:00
parent c61f5f2835
commit feb07b97ab

View File

@ -52,9 +52,6 @@ type SessionManagerCtx struct {
} }
func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) types.Session { func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) types.Session {
manager.membersMu.Lock()
defer manager.membersMu.Unlock()
session := &SessionCtx{ session := &SessionCtx{
id: id, id: id,
manager: manager, manager: manager,
@ -62,36 +59,40 @@ func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile)
profile: profile, profile: profile,
} }
manager.membersMu.Lock()
manager.members[id] = session manager.members[id] = session
manager.membersMu.Unlock()
manager.emmiter.Emit("created", session) manager.emmiter.Emit("created", session)
return session return session
} }
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) { func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
manager.membersMu.Lock() manager.membersMu.Lock()
defer manager.membersMu.Unlock()
session, ok := manager.members[id] session, ok := manager.members[id]
manager.membersMu.Unlock()
return session, ok return session, ok
} }
func (manager *SessionManagerCtx) Delete(id string) error { func (manager *SessionManagerCtx) Delete(id string) error {
manager.membersMu.Lock() manager.membersMu.Lock()
defer manager.membersMu.Unlock()
session, ok := manager.members[id] session, ok := manager.members[id]
if !ok { if !ok {
manager.membersMu.Unlock()
return fmt.Errorf("Member not found.") return fmt.Errorf("Member not found.")
} }
manager.emmiter.Emit("deleted", session)
delete(manager.members, id) delete(manager.members, id)
manager.membersMu.Unlock()
var err error
if session.IsConnected() { if session.IsConnected() {
return session.Disconnect("member deleted") err = session.Disconnect("member deleted")
} }
return nil manager.emmiter.Emit("deleted", session)
return err
} }
// --- // ---