-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.go
62 lines (46 loc) · 1011 Bytes
/
pipeline.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
package main
import (
"sync"
"time"
)
type ns struct{} //null struct
type FanOut struct {
sync.RWMutex
subscribers map[chan FrameData]ns
input chan FrameData
}
func CreateFanOut() *FanOut {
fanout := new(FanOut)
fanout.subscribers = make(map[chan FrameData]ns)
fanout.input = make(chan FrameData, 2)
go func() {
for frame := range fanout.input {
fanout.RLock()
for subs := range fanout.subscribers {
select {
case subs <- frame:
case <-time.After(time.Millisecond * 10):
}
}
fanout.RUnlock()
}
}()
return fanout
}
func (fanout *FanOut) Subscribe() chan FrameData {
fanout.Lock()
defer fanout.Unlock()
newChan := make(chan FrameData, 2)
fanout.subscribers[newChan] = ns{}
return newChan
}
func (fanout *FanOut) UnSubscribe(channel chan FrameData) {
fanout.Lock()
defer fanout.Unlock()
delete(fanout.subscribers, channel)
}
func (fanout *FanOut) Empty() bool {
fanout.RLock()
defer fanout.RUnlock()
return len(fanout.subscribers) == 0
}