From 4f1047940510648d3f5f5424ea69477259002e3f Mon Sep 17 00:00:00 2001 From: Ash Keel Date: Wed, 9 Feb 2022 15:16:47 +0100 Subject: [PATCH] Use RWMutex where possible --- modules/http/safebool.go | 8 ++++---- modules/twitch/modules.loyalty.go | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/http/safebool.go b/modules/http/safebool.go index 8ed2133..3084a98 100644 --- a/modules/http/safebool.go +++ b/modules/http/safebool.go @@ -4,11 +4,11 @@ import "sync" type SafeBool struct { val bool - mux sync.Mutex + mux sync.RWMutex } func newSafeBool(val bool) *SafeBool { - return &SafeBool{val: val, mux: sync.Mutex{}} + return &SafeBool{val: val, mux: sync.RWMutex{}} } func (s *SafeBool) Set(val bool) { @@ -18,8 +18,8 @@ func (s *SafeBool) Set(val bool) { } func (s *SafeBool) Get() bool { - s.mux.Lock() + s.mux.RLock() val := s.val - s.mux.Unlock() + s.mux.RUnlock() return val } diff --git a/modules/twitch/modules.loyalty.go b/modules/twitch/modules.loyalty.go index f048200..4ec95a6 100644 --- a/modules/twitch/modules.loyalty.go +++ b/modules/twitch/modules.loyalty.go @@ -52,7 +52,7 @@ func (b *Bot) SetupLoyalty(loyalty *loyalty.Manager) { // Setup handler for adding points over time b.Client.OnConnect(func() { b.logger.Info("status poll started") - var statusMux sync.Mutex + var statusMux sync.RWMutex streamOnline := true go func() { for { @@ -90,9 +90,9 @@ func (b *Bot) SetupLoyalty(loyalty *loyalty.Manager) { time.Sleep(time.Duration(config.Points.Interval) * time.Second) // If stream is confirmed offline, don't give points away! - statusMux.Lock() + statusMux.RLock() isOnline := streamOnline - statusMux.Unlock() + statusMux.RUnlock() if !isOnline { continue } @@ -154,15 +154,15 @@ func (b *Bot) IsBanned(user string) bool { func (b *Bot) IsActive(user string) bool { b.mu.Lock() - defer b.mu.Unlock() active, ok := b.activeUsers[user] + b.mu.Unlock() return ok && active } func (b *Bot) ResetActivity() { b.mu.Lock() - defer b.mu.Unlock() b.activeUsers = make(map[string]bool) + b.mu.Unlock() } func cmdBalance(bot *Bot, message irc.PrivateMessage) {