-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoller.go
68 lines (51 loc) · 1.1 KB
/
poller.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
package eventlog
import (
"github.com/xelaj/go-dry"
"io"
"time"
)
type Poller interface {
Poll(r EventReader, dest chan Event, stop chan struct{})
}
type LongPoller struct {
Limit int
Timeout time.Duration
// AllowedSeverity contains the event types
// Possible values:
// SeverityInfo => "Информация"
// SeverityError => "Ошибка"
// SeverityWarn => "Предупреждение"
// SeverityNote => "Примечание"
//
AllowedSeverity []SeverityType
LastErr error
}
// Poll does long polling.
func (p *LongPoller) Poll(r EventReader, dest chan Event, stop chan struct{}) {
p.LastErr = nil
for {
select {
case <-stop:
return
default:
}
events, err := r.Read(p.Limit, p.Timeout)
p.LastErr = err
if err != nil && err != io.EOF {
return
}
p.pushEvents(events, dest)
if p.LastErr == io.EOF {
return
}
}
}
func (p *LongPoller) pushEvents(events []Event, dest chan Event) {
for _, event := range events {
if len(p.AllowedSeverity) > 0 &&
!dry.SliceContains(p.AllowedSeverity, event.Severity) {
continue
}
dest <- event
}
}