-
Notifications
You must be signed in to change notification settings - Fork 5
/
mux.go
108 lines (95 loc) · 2.37 KB
/
mux.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package supersense
import (
"sync"
"github.com/pkg/errors"
)
// Mux is a necessary struct to join different sources
type Mux struct {
pipelines []chan *Event // fan-out pipelines
filters map[chan *Event][]string // sources filter
sources []Source
running map[Source]bool
mu *sync.Mutex
}
// NewMux returns a new mux to use as a mani pipeline for all your event sources
func NewMux(sources ...Source) (*Mux, error) {
channels := make([]chan *Event, 0)
m := &Mux{
mu: &sync.Mutex{},
sources: sources,
pipelines: channels,
running: map[Source]bool{},
filters: map[chan *Event][]string{},
}
return m, nil
}
func (m *Mux) setRunningSource(s Source, isRunning bool) {
m.mu.Lock()
m.running[s] = isRunning
m.mu.Unlock()
}
func (m *Mux) sourceListener(s Source) {
m.setRunningSource(s, true)
for event := range s.Pipeline() {
m.mu.Lock()
for _, pipe := range m.pipelines {
filters, filtered := m.filters[pipe]
if filtered && len(filters) > 0 {
for _, filter := range filters {
if filter == event.SourceName {
pipe <- &event
}
}
} else {
pipe <- &event
}
}
m.mu.Unlock()
}
m.setRunningSource(s, false)
}
func (m *Mux) addNewSource(s Source) {
m.sources = append(m.sources, s)
go m.sourceListener(s)
}
// AddNewSource exports this function to public
func (m *Mux) AddNewSource(s Source) {
m.addNewSource(s)
}
// RunAllSources run all the sources at the same time
func (m *Mux) RunAllSources() error {
for _, s := range m.sources {
go m.sourceListener(s)
}
for _, s := range m.sources {
if err := s.Run(); err != nil {
return errors.WithStack(err)
}
}
return nil
}
func (m *Mux) addPipeline(pipeline chan *Event, filteredSources ...string) {
m.mu.Lock()
m.pipelines = append(m.pipelines, pipeline)
if len(filteredSources) > 0 {
m.filters[pipeline] = filteredSources
}
m.mu.Unlock()
}
func (m *Mux) deleteAndClosePipeline(pipeline chan *Event) {
for i, p := range m.pipelines {
if p == pipeline {
m.mu.Lock()
close(p)
m.pipelines = append(m.pipelines[:i], m.pipelines[i+1:]...)
delete(m.filters, pipeline)
m.mu.Unlock()
}
}
}
// Register attach a new channel to the pipes list.
func (m *Mux) Register(pipeline chan *Event, done <-chan struct{}, filteredSources ...string) {
m.addPipeline(pipeline, filteredSources...)
<-done
m.deleteAndClosePipeline(pipeline)
}