twitch-bot/timers.go

84 lines
1.8 KiB
Go
Raw Permalink Normal View History

2020-12-21 00:32:39 +00:00
package main
import (
"crypto/sha256"
"fmt"
2020-12-21 00:32:39 +00:00
"strings"
"sync"
"time"
)
type timerType uint8
const (
timerTypePermit timerType = iota
timerTypeCooldown
)
2020-12-21 00:32:39 +00:00
var timerStore = newTimer()
type timerEntry struct {
kind timerType
time time.Time
}
2020-12-21 00:32:39 +00:00
type timer struct {
timers map[string]timerEntry
2020-12-21 00:32:39 +00:00
lock *sync.RWMutex
}
func newTimer() *timer {
return &timer{
timers: map[string]timerEntry{},
2020-12-21 00:32:39 +00:00
lock: new(sync.RWMutex),
}
}
// Cooldown timer
2020-12-21 00:32:39 +00:00
func (t *timer) AddCooldown(tt timerType, limiter, ruleID string) {
t.add(timerTypeCooldown, t.getCooldownTimerKey(tt, limiter, ruleID))
2020-12-21 00:32:39 +00:00
}
func (t *timer) InCooldown(tt timerType, limiter, ruleID string, cooldown time.Duration) bool {
return t.has(t.getCooldownTimerKey(tt, limiter, ruleID), cooldown)
}
func (t timer) getCooldownTimerKey(tt timerType, limiter, ruleID string) string {
h := sha256.New()
fmt.Fprintf(h, "%d:%s:%s", tt, limiter, ruleID)
return fmt.Sprintf("sha256:%x", h.Sum(nil))
2020-12-21 00:32:39 +00:00
}
// Permit timer
func (t *timer) AddPermit(channel, username string) {
t.add(timerTypePermit, t.getPermitTimerKey(channel, username))
2020-12-21 00:32:39 +00:00
}
func (t *timer) HasPermit(channel, username string) bool {
return t.has(t.getPermitTimerKey(channel, username), config.PermitTimeout)
2020-12-21 00:32:39 +00:00
}
func (t timer) getPermitTimerKey(channel, username string) string {
h := sha256.New()
fmt.Fprintf(h, "%d:%s:%s", timerTypePermit, channel, strings.ToLower(strings.TrimLeft(username, "@")))
return fmt.Sprintf("sha256:%x", h.Sum(nil))
}
// Generic
func (t *timer) add(kind timerType, id string) {
t.lock.Lock()
defer t.lock.Unlock()
t.timers[id] = timerEntry{kind: kind, time: time.Now()}
}
func (t *timer) has(id string, validity time.Duration) bool {
t.lock.RLock()
defer t.lock.RUnlock()
return time.Since(t.timers[id].time) < validity
}