|
package message |
|
|
|
import ( |
|
"crypto/sha256" |
|
"time" |
|
|
|
"github.com/target/goalert/gadb" |
|
) |
|
|
|
|
|
type Throttle struct { |
|
cfg ThrottleConfig |
|
typeOnly bool |
|
now time.Time |
|
|
|
first map[ThrottleItem]time.Time |
|
count map[ThrottleItem]int |
|
cooldown map[gadb.DestHashV1]bool |
|
} |
|
|
|
|
|
type ThrottleItem struct { |
|
DestHash gadb.DestHashV1 |
|
BucketDur time.Duration |
|
} |
|
|
|
|
|
type ThrottleConfig interface { |
|
Rules(Message) []ThrottleRule |
|
MaxDuration() time.Duration |
|
} |
|
|
|
func maxThrottleDuration(cfgs ...ThrottleConfig) time.Duration { |
|
var max time.Duration |
|
for _, cfg := range cfgs { |
|
dur := cfg.MaxDuration() |
|
if dur > max { |
|
max = dur |
|
} |
|
} |
|
return max |
|
} |
|
|
|
|
|
func NewThrottle(cfg ThrottleConfig, now time.Time, byTypeOnly bool) *Throttle { |
|
return &Throttle{ |
|
cfg: cfg, |
|
now: now, |
|
typeOnly: byTypeOnly, |
|
|
|
first: make(map[ThrottleItem]time.Time), |
|
count: make(map[ThrottleItem]int), |
|
cooldown: make(map[gadb.DestHashV1]bool), |
|
} |
|
} |
|
|
|
func (tr *Throttle) destKey(d gadb.DestV1) gadb.DestHashV1 { |
|
if tr.typeOnly { |
|
return sha256.Sum256([]byte(d.Type)) |
|
} |
|
|
|
return d.DestHash() |
|
} |
|
|
|
|
|
func (tr *Throttle) Record(msg Message) { |
|
keyHash := tr.destKey(msg.Dest) |
|
|
|
since := tr.now.Sub(msg.SentAt) |
|
rules := tr.cfg.Rules(msg) |
|
for i, rule := range rules { |
|
if since >= rule.Per { |
|
continue |
|
} |
|
key := ThrottleItem{DestHash: keyHash, BucketDur: rule.Per} |
|
tr.count[key]++ |
|
count := tr.count[key] |
|
if tr.first[key].IsZero() || msg.SentAt.Before(tr.first[key]) { |
|
tr.first[key] = msg.SentAt |
|
} |
|
|
|
if count >= rule.Count { |
|
tr.cooldown[keyHash] = true |
|
continue |
|
} |
|
|
|
if !rule.Smooth { |
|
continue |
|
} |
|
|
|
|
|
var prevRule ThrottleRule |
|
if i > 0 { |
|
prevRule = rules[i-1] |
|
} |
|
|
|
if count < prevRule.Count || count == 0 { |
|
|
|
continue |
|
} |
|
|
|
|
|
count -= prevRule.Count |
|
elapsed := tr.now.Sub(tr.first[key]) - prevRule.Per |
|
per := rule.Per - prevRule.Per |
|
|
|
if count > int(elapsed*time.Duration(rule.Count-prevRule.Count)/per) { |
|
tr.cooldown[keyHash] = true |
|
} |
|
} |
|
} |
|
|
|
|
|
func (tr *Throttle) InCooldown(msg Message) bool { |
|
return tr.cooldown[tr.destKey(msg.Dest)] |
|
} |
|
|