add hostMu.

This commit is contained in:
Miroslav Šedivý 2020-11-14 22:54:35 +01:00
parent 1fd53cfb74
commit f11c1c5e54

View File

@ -1,6 +1,8 @@
package session package session
import ( import (
"sync"
"github.com/kataras/go-events" "github.com/kataras/go-events"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -14,6 +16,7 @@ func New(capture types.CaptureManager, config *config.Session) *SessionManagerCt
return &SessionManagerCtx{ return &SessionManagerCtx{
logger: log.With().Str("module", "session").Logger(), logger: log.With().Str("module", "session").Logger(),
host: nil, host: nil,
hostMu: sync.Mutex{},
capture: capture, capture: capture,
config: config, config: config,
members: make(map[string]*SessionCtx), members: make(map[string]*SessionCtx),
@ -24,6 +27,7 @@ func New(capture types.CaptureManager, config *config.Session) *SessionManagerCt
type SessionManagerCtx struct { type SessionManagerCtx struct {
logger zerolog.Logger logger zerolog.Logger
host types.Session host types.Session
hostMu sync.Mutex
capture types.CaptureManager capture types.CaptureManager
config *config.Session config *config.Session
members map[string]*SessionCtx members map[string]*SessionCtx
@ -67,19 +71,31 @@ func (manager *SessionManagerCtx) Destroy(id string) error {
// host // host
// --- // ---
func (manager *SessionManagerCtx) HasHost() bool { func (manager *SessionManagerCtx) HasHost() bool {
manager.hostMu.Lock()
defer manager.hostMu.Unlock()
return manager.host != nil return manager.host != nil
} }
func (manager *SessionManagerCtx) SetHost(host types.Session) { func (manager *SessionManagerCtx) SetHost(host types.Session) {
manager.hostMu.Lock()
defer manager.hostMu.Unlock()
manager.host = host manager.host = host
manager.emmiter.Emit("host", host) manager.emmiter.Emit("host", host)
} }
func (manager *SessionManagerCtx) GetHost() types.Session { func (manager *SessionManagerCtx) GetHost() types.Session {
manager.hostMu.Lock()
defer manager.hostMu.Unlock()
return manager.host return manager.host
} }
func (manager *SessionManagerCtx) ClearHost() { func (manager *SessionManagerCtx) ClearHost() {
manager.hostMu.Lock()
defer manager.hostMu.Unlock()
host := manager.host host := manager.host
manager.host = nil manager.host = nil
manager.emmiter.Emit("host_cleared", host) manager.emmiter.Emit("host_cleared", host)