From db31c756621e59a01e910b700a9b937faf6f4e71 Mon Sep 17 00:00:00 2001 From: Ash Keel Date: Sun, 4 Dec 2022 00:04:15 +0100 Subject: [PATCH] refactor: use SyncSlice --- README.md | 2 +- go.mod | 2 +- go.sum | 4 ++-- loyalty/data.go | 6 ------ loyalty/manager.go | 24 ++++++++++++------------ twitch/bot.go | 3 ++- twitch/bot.timer.go | 14 +++++++++----- utils/pubsub.go | 10 +++++----- 8 files changed, 32 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 0529e64..1674867 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Strimertül is a single executable app that provides the following: At strimertül's core is [Kilovolt](https://github.com/strimertul/kilovolt), a pub/sub key-value store accessible via websocket. You can access every functionality of strimertul through the Kilovolt API. Check [this repository](https://github.com/strimertul/kilovolt-clients) for a list of officially supported kilovolt clients (or submit your own). You should be able to easily build a client yourself by just creating a websocket connection and using the [Kilovolt protocol](https://github.com/strimertul/kilovolt/blob/main/PROTOCOL.md). -Check out the [project's wiki](https://github.com/strimertul/strimertul/wiki) for more information on how to use the API to interact with strimertul, or the `docs` folder for more technical informations. +Check out the [project's wiki](https://github.com/strimertul/strimertul/wiki) for more information on how to use the API to interact with strimertul, or the `docs` folder for more technical information. ## Development diff --git a/go.mod b/go.mod index 423b624..da7cffa 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/strimertul/strimertul go 1.19 require ( - git.sr.ht/~hamcha/containers v0.2.1 + git.sr.ht/~hamcha/containers v0.2.3 github.com/Masterminds/sprig/v3 v3.2.2 github.com/apenwarr/fixconsole v0.0.0-20191012055117-5a9f6489cc29 github.com/cockroachdb/pebble v0.0.0-20221116223310-87eccabb90a3 diff --git a/go.sum b/go.sum index fea2d81..ab0e43f 100644 --- a/go.sum +++ b/go.sum @@ -31,8 +31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -git.sr.ht/~hamcha/containers v0.2.1 h1:mJ8b4fQhDKU73VRK1SjeIzJ5YnZYHeFHLJvHl6yKtNg= -git.sr.ht/~hamcha/containers v0.2.1/go.mod h1:RiZphUpy9t6EnL4Gf6uzByM9QrBoqRCEPo7kz2wzbhE= +git.sr.ht/~hamcha/containers v0.2.3 h1:h8BySrQg7bnOVC2a0WAs69Vkby6KYRPrsbTbdx/rrIE= +git.sr.ht/~hamcha/containers v0.2.3/go.mod h1:RiZphUpy9t6EnL4Gf6uzByM9QrBoqRCEPo7kz2wzbhE= github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= diff --git a/loyalty/data.go b/loyalty/data.go index 47a6c3a..7e39020 100644 --- a/loyalty/data.go +++ b/loyalty/data.go @@ -17,12 +17,8 @@ type Config struct { const RewardsKey = "loyalty/rewards" -type RewardStorage []Reward - const GoalsKey = "loyalty/goals" -type GoalStorage []Goal - type Reward struct { Enabled bool `json:"enabled"` ID string `json:"id"` @@ -53,8 +49,6 @@ type PointsEntry struct { const QueueKey = "loyalty/redeem-queue" -type RedeemQueueStorage []Redeem - type Redeem struct { Username string `json:"username"` DisplayName string `json:"display_name"` diff --git a/loyalty/manager.go b/loyalty/manager.go index 2fc3649..9547b60 100644 --- a/loyalty/manager.go +++ b/loyalty/manager.go @@ -30,9 +30,9 @@ var ( type Manager struct { points *containers.SyncMap[string, PointsEntry] Config *containers.RWSync[Config] - Rewards *containers.Sync[RewardStorage] - Goals *containers.Sync[GoalStorage] - Queue *containers.Sync[RedeemQueueStorage] + Rewards *containers.SyncSlice[Reward] + Goals *containers.SyncSlice[Goal] + Queue *containers.SyncSlice[Redeem] db *database.LocalDBClient logger *zap.Logger cooldowns map[string]time.Time @@ -48,9 +48,9 @@ func NewManager(db *database.LocalDBClient, twitchManager *twitch.Manager, logge ctx, cancelFn := context.WithCancel(context.Background()) loyalty := &Manager{ Config: containers.NewRWSync(Config{Enabled: false}), - Rewards: containers.NewSync(RewardStorage{}), - Goals: containers.NewSync(GoalStorage{}), - Queue: containers.NewSync(RedeemQueueStorage{}), + Rewards: containers.NewSyncSlice[Reward](), + Goals: containers.NewSyncSlice[Goal](), + Queue: containers.NewSyncSlice[Redeem](), logger: logger, db: db, @@ -73,7 +73,7 @@ func NewManager(db *database.LocalDBClient, twitchManager *twitch.Manager, logge } // Retrieve configs - var rewards RewardStorage + var rewards []Reward if err := db.GetJSON(RewardsKey, &rewards); err != nil { loyalty.Rewards.Set(rewards) } else { @@ -82,7 +82,7 @@ func NewManager(db *database.LocalDBClient, twitchManager *twitch.Manager, logge } } - var goals GoalStorage + var goals []Goal if err := db.GetJSON(GoalsKey, &goals); err != nil { loyalty.Goals.Set(goals) } else { @@ -91,7 +91,7 @@ func NewManager(db *database.LocalDBClient, twitchManager *twitch.Manager, logge } } - var queue RedeemQueueStorage + var queue []Redeem if err := db.GetJSON(QueueKey, &queue); err != nil { loyalty.Queue.Set(queue) } else { @@ -157,11 +157,11 @@ func (m *Manager) update(key, value string) { m.SetBanList(m.Config.Get().BanList) } case GoalsKey: - err = utils.LoadJSONToWrapped[GoalStorage](value, m.Goals) + err = utils.LoadJSONToWrapped[[]Goal](value, m.Goals) case RewardsKey: - err = utils.LoadJSONToWrapped[RewardStorage](value, m.Rewards) + err = utils.LoadJSONToWrapped[[]Reward](value, m.Rewards) case QueueKey: - err = utils.LoadJSONToWrapped[RedeemQueueStorage](value, m.Queue) + err = utils.LoadJSONToWrapped[[]Redeem](value, m.Queue) case CreateRedeemRPC: var redeem Redeem err = json.UnmarshalFromString(value, &redeem) diff --git a/twitch/bot.go b/twitch/bot.go index 3612b53..815ff25 100644 --- a/twitch/bot.go +++ b/twitch/bot.go @@ -24,7 +24,7 @@ type Bot struct { username string logger *zap.Logger lastMessage *containers.RWSync[time.Time] - chatHistory *containers.Sync[[]irc.PrivateMessage] + chatHistory *containers.SyncSlice[irc.PrivateMessage] commands *containers.SyncMap[string, BotCommand] customCommands *containers.SyncMap[string, BotCustomCommand] @@ -74,6 +74,7 @@ func newBot(api *Client, config BotConfig) *Bot { commands: containers.NewSyncMap[string, BotCommand](), customCommands: containers.NewSyncMap[string, BotCustomCommand](), customTemplates: containers.NewSyncMap[string, *template.Template](), + chatHistory: containers.NewSyncSlice[irc.PrivateMessage](), OnConnect: utils.NewPubSub[BotConnectHandler](), OnMessage: utils.NewPubSub[BotMessageHandler](), diff --git a/twitch/bot.timer.go b/twitch/bot.timer.go index 326c8a0..457dd31 100644 --- a/twitch/bot.timer.go +++ b/twitch/bot.timer.go @@ -34,7 +34,7 @@ type BotTimerModule struct { bot *Bot lastTrigger *containers.SyncMap[string, time.Time] - messages *containers.RWSync[[AverageMessageWindow]int] + messages *containers.SyncSlice[int] cancelTimerSub database.CancelFunc } @@ -43,7 +43,13 @@ func SetupTimers(bot *Bot) *BotTimerModule { mod := &BotTimerModule{ bot: bot, lastTrigger: containers.NewSyncMap[string, time.Time](), - messages: containers.NewRWSync([AverageMessageWindow]int{}), + messages: containers.NewSyncSlice[int](), + } + + // Fill messages with zero values + // (This can probably be done faster) + for i := 0; i < AverageMessageWindow; i += 1 { + mod.messages.Push(0) } // Load config from database @@ -160,7 +166,5 @@ func (m *BotTimerModule) currentChatActivity() int { func (m *BotTimerModule) OnMessage(message irc.PrivateMessage) { index := message.Time.Minute() % AverageMessageWindow - messages := m.messages.Get() - messages[index] += 1 - m.messages.Set(messages) + m.messages.SetIndex(index, 1) } diff --git a/utils/pubsub.go b/utils/pubsub.go index 60d4cb2..6a19427 100644 --- a/utils/pubsub.go +++ b/utils/pubsub.go @@ -3,21 +3,21 @@ package utils import "git.sr.ht/~hamcha/containers" type PubSub[T Comparable] struct { - subscribers *containers.RWSync[[]T] + subscribers *containers.SyncSlice[T] } func NewPubSub[T Comparable]() *PubSub[T] { return &PubSub[T]{ - subscribers: containers.NewRWSync([]T{}), + subscribers: containers.NewSyncSlice[T](), } } func (p *PubSub[T]) Subscribe(handler T) { - p.subscribers.Set(append(p.subscribers.Get(), handler)) + p.subscribers.Push(handler) } func (p *PubSub[T]) Unsubscribe(handler T) { - arr := p.subscribers.Get() + arr := p.subscribers.Copy() // Use slice trick to in-place remove entry if found for index := range arr { if arr[index].Equals(handler) { @@ -29,7 +29,7 @@ func (p *PubSub[T]) Unsubscribe(handler T) { } func (p *PubSub[T]) Subscribers() []T { - return p.subscribers.Get() + return p.subscribers.Copy() } func (p *PubSub[T]) Copy(other *PubSub[T]) {