-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patheventfilter.go
83 lines (67 loc) · 1.78 KB
/
eventfilter.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
package workflow
import (
"hash/fnv"
)
// EventFilter can be passed to the event streaming implementation to allow specific consumers to have an
// earlier on filtering process. True is returned when the event should be skipped.
type EventFilter func(e *Event) bool
func FilterUsing(e *Event, filters ...EventFilter) bool {
for _, filter := range filters {
if mustFilterOut := filter(e); mustFilterOut {
return true
}
}
return false
}
func shardFilter(shard, totalShards int) EventFilter {
return func(e *Event) bool {
if totalShards > 1 {
return e.ID%int64(totalShards) != int64(shard)-1
}
return false
}
}
// ConnectorEventFilter can be passed to the event streaming implementation to allow specific consumers to have an
// earlier on filtering process. True is returned when the event should be skipped.
type ConnectorEventFilter func(e *ConnectorEvent) bool
func FilterConnectorEventUsing(e *ConnectorEvent, filters ...ConnectorEventFilter) bool {
for _, filter := range filters {
if mustFilterOut := filter(e); mustFilterOut {
return true
}
}
return false
}
func shardConnectorEventFilter(shard, totalShards int) ConnectorEventFilter {
hsh := fnv.New32()
return func(e *ConnectorEvent) bool {
if totalShards > 1 {
hsh.Reset()
_, err := hsh.Write([]byte(e.ID))
if err != nil {
return false
}
hash := hsh.Sum32()
return hash%uint32(totalShards) == uint32(shard)-1
}
return false
}
}
func filterByForeignID(foreignID string) EventFilter {
return func(e *Event) bool {
fid, ok := e.Headers[HeaderForeignID]
if !ok {
return false
}
return fid != foreignID
}
}
func filterByRunID(runID string) EventFilter {
return func(e *Event) bool {
rID, ok := e.Headers[HeaderRunID]
if !ok {
return false
}
return rID != runID
}
}