forked from whamcloud/lemur
/
pacer-tokenBucketPacer.go
192 lines (160 loc) · 6.94 KB
/
pacer-tokenBucketPacer.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright © 2020 Microsoft <wastore@microsoft.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package util
import (
"context"
"sync/atomic"
"time"
)
// pacer is used by callers whose activity must be controlled to a certain pace
type Pacer interface {
// RequestTrafficAllocation blocks until the caller is allowed to process byteCount bytes.
RequestTrafficAllocation(ctx context.Context, byteCount int64) error
// UndoRequest reverses a previous request to process n bytes. Is used when
// the caller did not need all of the allocation they previously requested
// e.g. when they asked for enough for a big buffer, but never filled it, they would
// call this method to return the unused portion.
UndoRequest(byteCount int64)
// GetTotalTraffic returns the cumulative count of all traffic that has been processed
GetTotalTraffic() int64
Close() error
}
const (
// How long to sleep in the loop that puts tokens into the bucket
bucketFillSleepDuration = time.Duration(float32(time.Second) * 0.1)
// How long to sleep when reading from the bucket and finding there's not enough tokens
bucketDrainSleepDuration = time.Duration(float32(time.Second) * 0.333)
// Controls the max amount by which the contents of the token bucket can build up, unused.
maxSecondsToOverpopulateBucket = 2.5 // had 5, when doing coarse-grained pacing. TODO: find best all-round value, or parameterize
)
type atomicMorpherInt64 func(startVal int64) (val int64, morphResult interface{})
func atomicMorphInt64(target *int64, morpher atomicMorpherInt64) interface{} {
if target == nil || morpher == nil {
panic("target and morpher mut not be nil")
}
for {
currentVal := atomic.LoadInt64(target)
desiredVal, morphResult := morpher(currentVal)
if atomic.CompareAndSwapInt64(target, currentVal, desiredVal) {
return morphResult
}
}
}
// tokenBucketPacer allows us to control the pace of an activity, using a basic token bucket algorithm.
// The target rate is fixed, but can be modified at any time through SetTargetBytesPerSecond
type tokenBucketPacer struct {
atomicTokenBucket int64
atomicTargetBytesPerSecond int64
atomicGrandTotal int64
atomicWaitCount int64
expectedBytesPerRequest int64
done chan struct{}
}
/*
* NewTokenBucketPacer creates a new pacer with given config
*/
func NewTokenBucketPacer(bytesPerSecond int64, expectedBytesPerCoarseRequest int64) *tokenBucketPacer {
p := &tokenBucketPacer{atomicTokenBucket: bytesPerSecond / 4, // seed it immediately with part-of-a-second's worth, to avoid a sluggish start
atomicTargetBytesPerSecond: bytesPerSecond,
expectedBytesPerRequest: int64(expectedBytesPerCoarseRequest),
done: make(chan struct{}),
}
go p.pacerBody()
return p
}
// RequestTrafficAllocation function is called by goroutines to request right to send a certain amount of bytes.
// It controls their rate by blocking until they are allowed to proceed
func (p *tokenBucketPacer) RequestTrafficAllocation(ctx context.Context, byteCount int64) error {
if byteCount > p.targetBytesPerSecond() {
// knarasim - ToDO better logic
atomic.AddInt64(&p.atomicTokenBucket, -byteCount)
} else {
// block until tokens are available
for atomic.AddInt64(&p.atomicTokenBucket, -byteCount) < 0 {
// by taking our desired count we've moved below zero, which means our allocation is not available
// right now, so put back what we asked for, and then wait
atomic.AddInt64(&p.atomicTokenBucket, byteCount)
// vary the wait amount, to reduce risk of any kind of pulsing or synchronization effect, without the perf and
// and threadsafety issues of actual random numbers
totalWaitsSoFar := atomic.AddInt64(&p.atomicWaitCount, 1)
modifiedSleepDuration := time.Duration(float32(bucketDrainSleepDuration) * (float32(totalWaitsSoFar%10) + 5) / 10) // 50 to 150% of bucketDrainSleepDuration
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(modifiedSleepDuration):
// keep looping
}
}
}
// record what we issued
atomic.AddInt64(&p.atomicGrandTotal, byteCount)
return nil
}
// UndoRequest allows a caller to return unused tokens
func (p *tokenBucketPacer) UndoRequest(byteCount int64) {
if byteCount > 0 {
atomic.AddInt64(&p.atomicTokenBucket, byteCount) // put them back in the bucket
atomic.AddInt64(&p.atomicGrandTotal, -byteCount) // deduct them from all-time issued count
}
}
func (p *tokenBucketPacer) Close() error {
close(p.done)
return nil
}
func (p *tokenBucketPacer) pacerBody() {
lastTime := time.Now()
for {
select {
case <-p.done:
return
default:
}
currentTarget := atomic.LoadInt64(&p.atomicTargetBytesPerSecond)
time.Sleep(bucketFillSleepDuration)
elapsedSeconds := time.Since(lastTime).Seconds()
bytesToRelease := int64(float64(currentTarget) * elapsedSeconds)
newTokenCount := atomic.AddInt64(&p.atomicTokenBucket, bytesToRelease)
// If the backlog of unsent bytes is now too great, then trim it back down.
// Why don't we want a big backlog? Because it limits our ability to accurately control the speed.
maxAllowedUnsentBytes := int64(float32(currentTarget) * maxSecondsToOverpopulateBucket)
if maxAllowedUnsentBytes < p.expectedBytesPerRequest {
maxAllowedUnsentBytes = p.expectedBytesPerRequest // just in case we are very coarse grained at a very slow speed
}
if newTokenCount > maxAllowedUnsentBytes {
atomicMorphInt64(&p.atomicTokenBucket, func(currentVal int64) (newVal int64, _ interface{}) {
newVal = currentVal
if currentVal > maxAllowedUnsentBytes {
newVal = maxAllowedUnsentBytes
}
return
})
}
lastTime = time.Now()
}
}
func (p *tokenBucketPacer) targetBytesPerSecond() int64 {
return atomic.LoadInt64(&p.atomicTargetBytesPerSecond)
}
func (p *tokenBucketPacer) setTargetBytesPerSecond(value int64) {
atomic.StoreInt64(&p.atomicTargetBytesPerSecond, value)
}
func (p *tokenBucketPacer) GetTotalTraffic() int64 {
return atomic.LoadInt64(&p.atomicGrandTotal)
}