/
throttler.go
executable file
·38 lines (34 loc) · 1010 Bytes
/
throttler.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
package util
import (
"time"
)
// WriteThrottler struct
type WriteThrottler struct {
compactionBytePerSecond int64
lastSizeCounter int64
lastSizeCheckTime time.Time
}
// NewWriteThrottler return *WriteThrottler
func NewWriteThrottler(bytesPerSecond int64) *WriteThrottler {
return &WriteThrottler{
compactionBytePerSecond: bytesPerSecond,
lastSizeCheckTime: time.Now(),
}
}
// MaybeSlowdown -
func (wt *WriteThrottler) MaybeSlowdown(delta int64) {
if wt.compactionBytePerSecond > 0 {
wt.lastSizeCounter += delta
now := time.Now()
elapsedDuration := now.Sub(wt.lastSizeCheckTime)
if elapsedDuration > 100*time.Millisecond {
overLimitBytes := wt.lastSizeCounter - wt.compactionBytePerSecond/10
if overLimitBytes > 0 {
overRatio := float64(overLimitBytes) / float64(wt.compactionBytePerSecond)
sleepTime := time.Duration(overRatio*1000) * time.Millisecond
time.Sleep(sleepTime)
}
wt.lastSizeCounter, wt.lastSizeCheckTime = 0, time.Now()
}
}
}