File size: 2,579 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package message

import (
	"crypto/sha256"
	"time"

	"github.com/target/goalert/gadb"
)

// Throttle represents the throttled messages for a queue.
type Throttle struct {
	cfg      ThrottleConfig
	typeOnly bool
	now      time.Time

	first    map[ThrottleItem]time.Time
	count    map[ThrottleItem]int
	cooldown map[gadb.DestHashV1]bool
}

// ThrottleItem represents the messages being throttled.
type ThrottleItem struct {
	DestHash  gadb.DestHashV1
	BucketDur time.Duration
}

// ThrottleConfig provides ThrottleRules for a given message.
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
}

// NewThrottle creates a new Throttle used to manage outgoing messages in a queue.
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()
}

// Record keeps track of the outgoing messages being throttled in a queue.
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
		}

		// flat rate
		var prevRule ThrottleRule
		if i > 0 {
			prevRule = rules[i-1]
		}

		if count < prevRule.Count || count == 0 {
			// allow prev rule in entirety
			continue
		}

		// spread remainder evenly
		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
		}
	}
}

// InCooldown returns true or false depending on the cooldown state of a throttled message.
func (tr *Throttle) InCooldown(msg Message) bool {
	return tr.cooldown[tr.destKey(msg.Dest)]
}