-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathloop-ticker.go
86 lines (74 loc) · 1.84 KB
/
loop-ticker.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
package blaster
import (
"context"
"time"
)
func (b *Blaster) startTickerLoop(ctx context.Context) {
b.mainWait.Add(1)
var ticker *time.Ticker
updateTicker := func() {
if b.Rate == 0 {
ticker = &time.Ticker{} // empty *time.Ticker will have nil C, so block forever.
return
}
ticksPerSecond := b.Rate/float64(len(b.PayloadVariants))
ticksPerMs := ticksPerSecond / 1000.0
ticksPerUs := ticksPerMs / 1000.0
ticksPerNs := ticksPerUs / 1000.0
nsPerTick := 1.0 / ticksPerNs
ticker = time.NewTicker(time.Nanosecond * time.Duration(nsPerTick))
}
changeRate := func(rate float64) {
b.Rate = rate
if ticker != nil {
ticker.Stop()
}
b.metrics.addSegment(b.Rate)
updateTicker()
b.printStatus(false)
}
updateTicker()
go func() {
defer b.mainWait.Done()
defer b.println("Exiting ticker loop")
defer func() {
if ticker != nil {
ticker.Stop()
}
}()
for {
// First wait for a tick... but we should also wait for an exit signal, data finished
// signal or rate change command (we could be waiting forever on rate = 0).
select {
case <-ticker.C:
// continue
case <-ctx.Done():
return
case <-b.dataFinishedChannel:
return
case rate := <-b.changeRateChannel:
// Restart the for loop after a rate change. If rate == 0, we may not want to send
// any more.
changeRate(rate)
continue
}
segment := b.metrics.currentSegment()
// Next send on the main channel. The channel won't have a listener if there is no idle
// worker. In this case we should continue and log a miss.
select {
case b.mainChannel <- segment:
// if main loop is waiting, send it a message
case <-ctx.Done():
// notest
return
case <-b.dataFinishedChannel:
// notest
return
default:
// notest
// if main loop is busy, skip this tick
continue
}
}
}()
}