-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcaster.go
147 lines (112 loc) · 2.47 KB
/
broadcaster.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package medialocker
import (
"errors"
"sync"
"time"
)
type Broadcaster struct {
lock sync.RWMutex
input chan interface{}
stopped chan interface{}
listeners []*listener
}
func NewBroadcaster(inBuffSize uint) *Broadcaster {
bc := Broadcaster{input: make(chan interface{}, inBuffSize), stopped: make(chan interface{}, 1)}
go bc.relay()
return &bc
}
func (bc *Broadcaster) IsDestroyed() bool {
bc.lock.RLock()
defer bc.lock.RUnlock()
return bc.listeners == nil
}
func (bc *Broadcaster) Destroy(timeout int) error {
bc.lock.Lock()
defer bc.lock.Unlock()
if bc.input == nil {
return errors.New("Calling BroadcastChan.Destroy multiple times on an instance.")
}
close(bc.input)
var err error
select {
case <-bc.stopped:
case <-time.After(time.Second * time.Duration(timeout)):
err = errors.New("BroadcastChan.Destroy timed out, forcing closing channels!")
}
for _, l := range bc.listeners {
l.Close()
}
bc.listeners = nil
return err
}
func (bc *Broadcaster) relay() {
for msg := range bc.input {
bc.broadcast(msg)
}
bc.input = nil
bc.stopped <- struct{}{}
close(bc.stopped)
}
func (bc *Broadcaster) sweep() {
var listeners []*listener
bc.lock.Lock()
defer bc.lock.Unlock()
for _, l := range bc.listeners {
if !l.IsClosed() {
listeners = append(listeners, l)
}
}
bc.listeners = listeners
}
func (bc *Broadcaster) Listen(buffSize uint) (<-chan interface{}, Closer) {
l := &listener{channel: make(chan interface{}, buffSize), closed: false}
bc.lock.Lock()
defer bc.lock.Unlock()
closer := func() error {
err := l.Close()
bc.sweep()
return err
}
bc.listeners = append(bc.listeners, l)
return l.channel, closer
}
func (bc *Broadcaster) Send(msg interface{}) {
bc.input <- msg
}
func (bc *Broadcaster) SendChannel() chan<- interface{} {
return bc.input
}
func (bc *Broadcaster) broadcast(msg interface{}) {
bc.lock.RLock()
defer bc.lock.RUnlock()
for _, ch := range bc.listeners {
ch.Write(msg)
}
}
type listener struct {
lock sync.RWMutex
closed bool
channel chan interface{}
}
func (l *listener) Write(msg interface{}) {
l.lock.RLock()
defer l.lock.RUnlock()
if !l.closed {
l.channel <- msg
}
}
func (l *listener) IsClosed() bool {
l.lock.RLock()
defer l.lock.RUnlock()
return l.closed
}
func (l *listener) Close() error {
l.lock.Lock()
defer l.lock.Unlock()
if l.closed {
return errors.New("Called close on an already closed Broadcast listener.")
}
close(l.channel)
l.closed = true
return nil
}