-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwindow.go
49 lines (43 loc) · 881 Bytes
/
window.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
package main
import (
"sync"
"time"
log "github.com/sirupsen/logrus"
)
type windowManager struct {
rule Rule
sinkChan *chan interface{}
sync.RWMutex
interval int
lastCalled time.Time
closeChan *chan struct{}
}
func (w *windowManager) start() {
closeChan := make(chan struct{})
w.closeChan = &closeChan
go func(closeChan chan struct{}) {
for {
w.windowRunner()
select {
case <-closeChan:
break
case <-time.After(1 * time.Second):
}
}
}(closeChan)
}
func (w *windowManager) stop() {
close(*w.closeChan)
}
func (w *windowManager) windowRunner() {
if time.Now().Sub(w.lastCalled).Seconds() > float64(w.interval) {
outputs, err := w.rule.Window()
if err != nil {
log.Errorf("Error calling Window() on rule %v: %v", w.rule.String(), err)
}
for _, o := range outputs {
*w.sinkChan <- o
}
w.lastCalled = time.Now()
}
}