1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-20 02:00:49 +00:00

fix: remove last mutexes

This commit is contained in:
Ash Keel 2022-12-03 17:29:19 +01:00
parent 26db325171
commit 330c4a3ec2
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E
2 changed files with 16 additions and 22 deletions

View file

@ -91,7 +91,6 @@ type BotAlertsModule struct {
Config BotAlertsConfig Config BotAlertsConfig
bot *Bot bot *Bot
mu sync.Mutex
templates templateCache templates templateCache
cancelAlertSub database.CancelFunc cancelAlertSub database.CancelFunc
@ -101,7 +100,6 @@ type BotAlertsModule struct {
func SetupAlerts(bot *Bot) *BotAlertsModule { func SetupAlerts(bot *Bot) *BotAlertsModule {
mod := &BotAlertsModule{ mod := &BotAlertsModule{
bot: bot, bot: bot,
mu: sync.Mutex{},
templates: templateCache{}, templates: templateCache{},
} }

View file

@ -2,9 +2,10 @@ package twitch
import ( import (
"math/rand" "math/rand"
"sync"
"time" "time"
"git.sr.ht/~hamcha/containers"
"github.com/strimertul/strimertul/database" "github.com/strimertul/strimertul/database"
"go.uber.org/zap" "go.uber.org/zap"
@ -31,11 +32,9 @@ const AverageMessageWindow = 5
type BotTimerModule struct { type BotTimerModule struct {
Config BotTimersConfig Config BotTimersConfig
lastTrigger map[string]time.Time
bot *Bot bot *Bot
messages [AverageMessageWindow]int lastTrigger *containers.SyncMap[string, time.Time]
mu sync.Mutex messages *containers.RWSync[[AverageMessageWindow]int]
startTime time.Time
cancelTimerSub database.CancelFunc cancelTimerSub database.CancelFunc
} }
@ -43,9 +42,8 @@ type BotTimerModule struct {
func SetupTimers(bot *Bot) *BotTimerModule { func SetupTimers(bot *Bot) *BotTimerModule {
mod := &BotTimerModule{ mod := &BotTimerModule{
bot: bot, bot: bot,
startTime: time.Now().Round(time.Minute), lastTrigger: containers.NewSyncMap[string, time.Time](),
lastTrigger: make(map[string]time.Time), messages: containers.NewRWSync([AverageMessageWindow]int{}),
mu: sync.Mutex{},
} }
// Load config from database // Load config from database
@ -101,27 +99,25 @@ func (m *BotTimerModule) runTimers() {
// Reset timer // Reset timer
func() { func() {
index := time.Now().Minute() % AverageMessageWindow index := time.Now().Minute() % AverageMessageWindow
m.mu.Lock() messages := m.messages.Get()
defer m.mu.Unlock() messages[index] = 0
m.messages[index] = 0 m.messages.Set(messages)
}() }()
// Run timers // Run timers
func() { func() {
now := time.Now() now := time.Now()
m.mu.Lock()
defer m.mu.Unlock()
for name, timer := range m.Config.Timers { for name, timer := range m.Config.Timers {
// Must be enabled // Must be enabled
if !timer.Enabled { if !timer.Enabled {
continue continue
} }
// Check if enough time has passed // Check if enough time has passed
lastTriggeredTime, ok := m.lastTrigger[name] lastTriggeredTime, ok := m.lastTrigger.GetKey(name)
if !ok { if !ok {
// If it's the first time we're checking it, start the cooldown // If it's the first time we're checking it, start the cooldown
lastTriggeredTime = time.Now() lastTriggeredTime = time.Now()
m.lastTrigger[name] = lastTriggeredTime m.lastTrigger.SetKey(name, lastTriggeredTime)
} }
minDelay := timer.MinimumDelay minDelay := timer.MinimumDelay
if minDelay < 60 { if minDelay < 60 {
@ -142,7 +138,7 @@ func (m *BotTimerModule) runTimers() {
m.bot.WriteMessage(message) m.bot.WriteMessage(message)
// Update last trigger // Update last trigger
m.lastTrigger[name] = now m.lastTrigger.SetKey(name, now)
} }
}() }()
} }
@ -156,7 +152,7 @@ func (m *BotTimerModule) Close() {
func (m *BotTimerModule) currentChatActivity() int { func (m *BotTimerModule) currentChatActivity() int {
total := 0 total := 0
for _, v := range m.messages { for _, v := range m.messages.Get() {
total += v total += v
} }
return total return total
@ -164,7 +160,7 @@ func (m *BotTimerModule) currentChatActivity() int {
func (m *BotTimerModule) OnMessage(message irc.PrivateMessage) { func (m *BotTimerModule) OnMessage(message irc.PrivateMessage) {
index := message.Time.Minute() % AverageMessageWindow index := message.Time.Minute() % AverageMessageWindow
m.mu.Lock() messages := m.messages.Get()
defer m.mu.Unlock() messages[index] += 1
m.messages[index] += 1 m.messages.Set(messages)
} }