-
Notifications
You must be signed in to change notification settings - Fork 3
/
sleep.go
64 lines (58 loc) · 1.54 KB
/
sleep.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
package steps
import (
"fmt"
"time"
"github.com/bitflow-stream/go-bitflow/bitflow"
"github.com/bitflow-stream/go-bitflow/script/reg"
)
func RegisterSleep(b reg.ProcessorRegistry) {
b.RegisterStep("sleep", _create_sleep_processor,
"Between every two samples, sleep the time difference between their timestamps").
Optional("time", reg.Duration(), time.Duration(0)).
Optional("onChangedTag", reg.String(), "")
}
func _create_sleep_processor(p *bitflow.SamplePipeline, params map[string]interface{}) error {
timeout := params["time"].(time.Duration)
changedTag := params["onChangedTag"].(string)
desc := "sleep between samples"
if timeout > 0 {
desc += fmt.Sprintf(" (%v)", timeout)
} else {
desc += " (timestamp difference)"
}
if changedTag != "" {
desc += " when tag " + changedTag + " changes"
}
previousTag := ""
var lastTimestamp time.Time
processor := &bitflow.SimpleProcessor{
Description: desc,
}
processor.Process = func(sample *bitflow.Sample, header *bitflow.Header) (*bitflow.Sample, *bitflow.Header, error) {
doSleep := true
if changedTag != "" {
newTag := sample.Tag(changedTag)
if newTag == previousTag {
doSleep = false
}
previousTag = newTag
}
if doSleep {
if timeout > 0 {
processor.StopChan.WaitTimeout(timeout)
} else {
last := lastTimestamp
if !last.IsZero() {
diff := sample.Time.Sub(last)
if diff > 0 {
processor.StopChan.WaitTimeout(diff)
}
}
lastTimestamp = sample.Time
}
}
return sample, header, nil
}
p.Add(processor)
return nil
}