add memberMu.

This commit is contained in:
Miroslav Šedivý 2020-11-14 23:05:09 +01:00
parent f11c1c5e54
commit 0d419787bc
2 changed files with 35 additions and 20 deletions

View File

@ -20,6 +20,7 @@ func New(capture types.CaptureManager, config *config.Session) *SessionManagerCt
capture: capture, capture: capture,
config: config, config: config,
members: make(map[string]*SessionCtx), members: make(map[string]*SessionCtx),
membersMu: sync.Mutex{},
emmiter: events.New(), emmiter: events.New(),
} }
} }
@ -31,10 +32,14 @@ type SessionManagerCtx struct {
capture types.CaptureManager capture types.CaptureManager
config *config.Session config *config.Session
members map[string]*SessionCtx members map[string]*SessionCtx
membersMu sync.Mutex
emmiter events.EventEmmiter emmiter events.EventEmmiter
} }
func (manager *SessionManagerCtx) New(id string, admin bool) types.Session { func (manager *SessionManagerCtx) New(id string, admin bool) types.Session {
manager.membersMu.Lock()
defer manager.membersMu.Unlock()
session := &SessionCtx{ session := &SessionCtx{
id: id, id: id,
admin: admin, admin: admin,
@ -48,16 +53,17 @@ func (manager *SessionManagerCtx) New(id string, admin bool) types.Session {
} }
func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) { func (manager *SessionManagerCtx) Get(id string) (types.Session, bool) {
manager.membersMu.Lock()
defer manager.membersMu.Unlock()
session, ok := manager.members[id] session, ok := manager.members[id]
return session, ok return session, ok
} }
func (manager *SessionManagerCtx) Has(id string) bool {
_, ok := manager.members[id]
return ok
}
func (manager *SessionManagerCtx) Destroy(id string) error { func (manager *SessionManagerCtx) Destroy(id string) error {
manager.membersMu.Lock()
defer manager.membersMu.Unlock()
session, ok := manager.members[id] session, ok := manager.members[id]
if ok { if ok {
delete(manager.members, id) delete(manager.members, id)
@ -105,6 +111,9 @@ func (manager *SessionManagerCtx) ClearHost() {
// members list // members list
// --- // ---
func (manager *SessionManagerCtx) Admins() []types.Session { func (manager *SessionManagerCtx) Admins() []types.Session {
manager.membersMu.Lock()
defer manager.membersMu.Unlock()
var sessions []types.Session var sessions []types.Session
for _, session := range manager.members { for _, session := range manager.members {
if !session.connected || !session.admin { if !session.connected || !session.admin {
@ -118,6 +127,9 @@ func (manager *SessionManagerCtx) Admins() []types.Session {
} }
func (manager *SessionManagerCtx) Members() []types.Session { func (manager *SessionManagerCtx) Members() []types.Session {
manager.membersMu.Lock()
defer manager.membersMu.Unlock()
var sessions []types.Session var sessions []types.Session
for _, session := range manager.members { for _, session := range manager.members {
if !session.connected { if !session.connected {
@ -131,6 +143,9 @@ func (manager *SessionManagerCtx) Members() []types.Session {
} }
func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) error { func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{}) error {
manager.membersMu.Lock()
defer manager.membersMu.Unlock()
for id, session := range manager.members { for id, session := range manager.members {
if !session.connected { if !session.connected {
continue continue
@ -146,6 +161,7 @@ func (manager *SessionManagerCtx) Broadcast(v interface{}, exclude interface{})
return err return err
} }
} }
return nil return nil
} }

View File

@ -23,7 +23,6 @@ type Session interface {
type SessionManager interface { type SessionManager interface {
New(id string, admin bool) Session New(id string, admin bool) Session
Get(id string) (Session, bool) Get(id string) (Session, bool)
Has(id string) bool
Destroy(id string) error Destroy(id string) error
HasHost() bool HasHost() bool