mirror of
https://git.sr.ht/~ashkeel/strimertul
synced 2024-09-20 02:00:49 +00:00
Fix mutex deadlock
This commit is contained in:
parent
751668fa14
commit
2513c2bc5a
3 changed files with 20 additions and 7 deletions
2
main.go
2
main.go
|
@ -23,6 +23,8 @@ import (
|
||||||
|
|
||||||
"github.com/mattn/go-colorable"
|
"github.com/mattn/go-colorable"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
_ "net/http/pprof"
|
||||||
)
|
)
|
||||||
|
|
||||||
const AppTitle = "strimertül"
|
const AppTitle = "strimertül"
|
||||||
|
|
|
@ -108,20 +108,20 @@ func (m *Manager) update(kvs []database.ModifiedKV) error {
|
||||||
switch key {
|
switch key {
|
||||||
case ConfigKey:
|
case ConfigKey:
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
|
||||||
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &m.config)
|
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &m.config)
|
||||||
|
m.mu.Unlock()
|
||||||
case GoalsKey:
|
case GoalsKey:
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
|
||||||
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &m.goals)
|
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &m.goals)
|
||||||
|
m.mu.Unlock()
|
||||||
case RewardsKey:
|
case RewardsKey:
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
|
||||||
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &m.rewards)
|
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &m.rewards)
|
||||||
|
m.mu.Unlock()
|
||||||
case QueueKey:
|
case QueueKey:
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
|
||||||
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &m.queue)
|
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &m.queue)
|
||||||
|
m.mu.Unlock()
|
||||||
case CreateRedeemRPC:
|
case CreateRedeemRPC:
|
||||||
var redeem Redeem
|
var redeem Redeem
|
||||||
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &redeem)
|
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &redeem)
|
||||||
|
@ -139,12 +139,12 @@ func (m *Manager) update(kvs []database.ModifiedKV) error {
|
||||||
switch {
|
switch {
|
||||||
// User point changed
|
// User point changed
|
||||||
case strings.HasPrefix(kv.Key, PointsPrefix):
|
case strings.HasPrefix(kv.Key, PointsPrefix):
|
||||||
m.mu.Lock()
|
|
||||||
defer m.mu.Unlock()
|
|
||||||
var entry PointsEntry
|
var entry PointsEntry
|
||||||
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &entry)
|
err = jsoniter.ConfigFastest.Unmarshal(kv.Data, &entry)
|
||||||
user := kv.Key[len(PointsPrefix):]
|
user := kv.Key[len(PointsPrefix):]
|
||||||
|
m.mu.Lock()
|
||||||
m.points[user] = entry
|
m.points[user] = entry
|
||||||
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package twitch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
irc "github.com/gempir/go-twitch-irc/v2"
|
irc "github.com/gempir/go-twitch-irc/v2"
|
||||||
|
@ -21,6 +22,8 @@ type Bot struct {
|
||||||
activeUsers map[string]bool
|
activeUsers map[string]bool
|
||||||
banlist map[string]bool
|
banlist map[string]bool
|
||||||
|
|
||||||
|
mu sync.Mutex
|
||||||
|
|
||||||
// Module specific vars
|
// Module specific vars
|
||||||
Loyalty *loyalty.Manager
|
Loyalty *loyalty.Manager
|
||||||
}
|
}
|
||||||
|
@ -38,6 +41,7 @@ func NewBot(api *Client, config BotConfig) *Bot {
|
||||||
lastMessage: time.Now(),
|
lastMessage: time.Now(),
|
||||||
activeUsers: make(map[string]bool),
|
activeUsers: make(map[string]bool),
|
||||||
banlist: make(map[string]bool),
|
banlist: make(map[string]bool),
|
||||||
|
mu: sync.Mutex{},
|
||||||
}
|
}
|
||||||
|
|
||||||
client.OnPrivateMessage(func(message irc.PrivateMessage) {
|
client.OnPrivateMessage(func(message irc.PrivateMessage) {
|
||||||
|
@ -47,14 +51,16 @@ func NewBot(api *Client, config BotConfig) *Bot {
|
||||||
bot.logger.Debug("message received too soon, ignoring")
|
bot.logger.Debug("message received too soon, ignoring")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
bot.mu.Lock()
|
||||||
bot.activeUsers[message.User.Name] = true
|
bot.activeUsers[message.User.Name] = true
|
||||||
|
bot.mu.Unlock()
|
||||||
|
|
||||||
// Check if it's a command
|
// Check if it's a command
|
||||||
if strings.HasPrefix(message.Message, "!") {
|
if strings.HasPrefix(message.Message, "!") {
|
||||||
// Run through supported commands
|
// Run through supported commands
|
||||||
for cmd, data := range commands {
|
for cmd, data := range commands {
|
||||||
if strings.HasPrefix(message.Message, cmd) {
|
if strings.HasPrefix(message.Message, cmd) {
|
||||||
data.Handler(bot, message)
|
go data.Handler(bot, message)
|
||||||
bot.lastMessage = time.Now()
|
bot.lastMessage = time.Now()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,6 +77,7 @@ func NewBot(api *Client, config BotConfig) *Bot {
|
||||||
}).Debug("user joined channel")
|
}).Debug("user joined channel")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
client.OnUserPartMessage(func(message irc.UserPartMessage) {
|
client.OnUserPartMessage(func(message irc.UserPartMessage) {
|
||||||
if strings.ToLower(message.User) == bot.username {
|
if strings.ToLower(message.User) == bot.username {
|
||||||
bot.logger.WithField("channel", message.Channel).Info("left channel")
|
bot.logger.WithField("channel", message.Channel).Info("left channel")
|
||||||
|
@ -168,11 +175,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()
|
||||||
|
defer b.mu.Unlock()
|
||||||
active, ok := b.activeUsers[user]
|
active, ok := b.activeUsers[user]
|
||||||
return ok && active
|
return ok && active
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) ResetActivity() {
|
func (b *Bot) ResetActivity() {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
b.activeUsers = make(map[string]bool)
|
b.activeUsers = make(map[string]bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue