1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

Use RWMutex where possible

This commit is contained in:
Ash Keel 2022-02-09 15:16:47 +01:00
parent 69d1f69113
commit 4f10479405
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E
2 changed files with 9 additions and 9 deletions

View file

@ -4,11 +4,11 @@ import "sync"
type SafeBool struct { type SafeBool struct {
val bool val bool
mux sync.Mutex mux sync.RWMutex
} }
func newSafeBool(val bool) *SafeBool { 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) { func (s *SafeBool) Set(val bool) {
@ -18,8 +18,8 @@ func (s *SafeBool) Set(val bool) {
} }
func (s *SafeBool) Get() bool { func (s *SafeBool) Get() bool {
s.mux.Lock() s.mux.RLock()
val := s.val val := s.val
s.mux.Unlock() s.mux.RUnlock()
return val return val
} }

View file

@ -52,7 +52,7 @@ func (b *Bot) SetupLoyalty(loyalty *loyalty.Manager) {
// Setup handler for adding points over time // Setup handler for adding points over time
b.Client.OnConnect(func() { b.Client.OnConnect(func() {
b.logger.Info("status poll started") b.logger.Info("status poll started")
var statusMux sync.Mutex var statusMux sync.RWMutex
streamOnline := true streamOnline := true
go func() { go func() {
for { for {
@ -90,9 +90,9 @@ func (b *Bot) SetupLoyalty(loyalty *loyalty.Manager) {
time.Sleep(time.Duration(config.Points.Interval) * time.Second) time.Sleep(time.Duration(config.Points.Interval) * time.Second)
// If stream is confirmed offline, don't give points away! // If stream is confirmed offline, don't give points away!
statusMux.Lock() statusMux.RLock()
isOnline := streamOnline isOnline := streamOnline
statusMux.Unlock() statusMux.RUnlock()
if !isOnline { if !isOnline {
continue continue
} }
@ -154,15 +154,15 @@ func (b *Bot) IsBanned(user string) bool {
func (b *Bot) IsActive(user string) bool { func (b *Bot) IsActive(user string) bool {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock()
active, ok := b.activeUsers[user] active, ok := b.activeUsers[user]
b.mu.Unlock()
return ok && active return ok && active
} }
func (b *Bot) ResetActivity() { func (b *Bot) ResetActivity() {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock()
b.activeUsers = make(map[string]bool) b.activeUsers = make(map[string]bool)
b.mu.Unlock()
} }
func cmdBalance(bot *Bot, message irc.PrivateMessage) { func cmdBalance(bot *Bot, message irc.PrivateMessage) {