-
Notifications
You must be signed in to change notification settings - Fork 34
/
channel_provider.go
62 lines (51 loc) · 1.06 KB
/
channel_provider.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 gomavlib
import (
"errors"
"fmt"
)
type channelProvider struct {
n *Node
eca endpointChannelProvider
terminate chan struct{}
}
func newChannelProvider(n *Node, eca endpointChannelProvider) (*channelProvider, error) {
return &channelProvider{
n: n,
eca: eca,
terminate: make(chan struct{}),
}, nil
}
func (cp *channelProvider) close() {
close(cp.terminate)
cp.eca.close()
}
func (cp *channelProvider) start() {
cp.n.wg.Add(1)
go cp.run()
}
func (cp *channelProvider) run() {
defer cp.n.wg.Done()
for {
label, rwc, err := cp.eca.provide()
if err != nil {
if !errors.Is(err, errTerminated) {
panic("errTerminated is the only error allowed here")
}
break
}
ch, err := newChannel(cp.n, cp.eca, label, rwc)
if err != nil {
panic(fmt.Errorf("newChannel unexpected error: %w", err))
}
cp.n.newChannel(ch)
if cp.eca.oneChannelAtAtime() {
// wait the channel to emit EventChannelClose
// before creating another channel
select {
case <-ch.done:
case <-cp.terminate:
return
}
}
}
}