diff --git a/src/notifiers/session_notifier.go b/src/notifiers/session_notifier.go index 81a8fca..b86faf3 100644 --- a/src/notifiers/session_notifier.go +++ b/src/notifiers/session_notifier.go @@ -1,6 +1,8 @@ package notifiers import ( + "sync" + "pixeltactics.com/match/src/types" ) @@ -34,12 +36,13 @@ func (notifier *SessionNotifier) NotifyAction(playerId string, actionName string } var sessionNotifier *SessionNotifier = nil +var once sync.Once func GetSessionNotifier() *SessionNotifier { - if sessionNotifier == nil { + once.Do(func() { sessionNotifier = &SessionNotifier{ SendChannel: make(chan *NotifierData, 256), } - } + }) return sessionNotifier } diff --git a/src/repositories/session_repository.go b/src/repositories/session_repository.go index dd1ff74..3310892 100644 --- a/src/repositories/session_repository.go +++ b/src/repositories/session_repository.go @@ -3,6 +3,7 @@ package repositories import ( "errors" "log" + "sync" "github.com/google/uuid" "pixeltactics.com/match/src/data_structures" @@ -140,13 +141,14 @@ func (repo *SessionRepository) checkOpponentSession(playerId string, opponentId } var sessionRepository *SessionRepository = nil +var onceSession sync.Once func GetSessionRepository() *SessionRepository { - if sessionRepository == nil { + onceSession.Do(func() { sessionRepository = &SessionRepository{ sessions: data_structures.NewSyncMap[string, *matches.Session](), playerSession: data_structures.NewSyncMap[string, *matches.Session](), } - } + }) return sessionRepository } diff --git a/src/repositories/template_repository.go b/src/repositories/template_repository.go index 45e95d9..fe3d663 100644 --- a/src/repositories/template_repository.go +++ b/src/repositories/template_repository.go @@ -2,6 +2,7 @@ package repositories import ( "errors" + "sync" matches_heroes_templates "pixeltactics.com/match/src/matches/heroes/templates" matches_interfaces "pixeltactics.com/match/src/matches/interfaces" @@ -22,13 +23,14 @@ func (repo TemplateRepository) GetTemplateFromName(name string) (matches_interfa } var templateRepository *TemplateRepository = nil +var onceTemplate sync.Once func GetTemplateRepository() *TemplateRepository { - if templateRepository == nil { + onceTemplate.Do(func() { templateRepository = &TemplateRepository{ Knight: matches_heroes_templates.Knight{}, Mage: matches_heroes_templates.Mage{}, } - } + }) return templateRepository }