-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy paththrottle.go
More file actions
50 lines (41 loc) · 832 Bytes
/
throttle.go
File metadata and controls
50 lines (41 loc) · 832 Bytes
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
package vfilter
import (
"time"
"www.velocidex.com/golang/vfilter/types"
)
type TimeThrottler struct {
ticker *time.Ticker
done chan bool
running bool
}
func (self *TimeThrottler) ChargeOp() {
select {
case <-self.ticker.C:
case <-self.done:
}
}
func (self *TimeThrottler) Close() {
if self.running {
self.ticker.Stop()
self.running = false
close(self.done)
}
}
func NewTimeThrottler(rate float64) types.Throttler {
// rate of 0 means no throttling.
if rate == 0 || rate > 100 {
rate = 100
}
result := &TimeThrottler{
ticker: time.NewTicker(time.Nanosecond *
time.Duration((float64(1000000000) / float64(rate)))),
done: make(chan bool, 1),
running: true,
}
// Just ignore rates which are too fast - do not throttle at
// all.
if rate >= 100 {
result.Close()
}
return result
}