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 {
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
}

View file

@ -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) {