-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathchannel_provider.go
65 lines (54 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
63
64
65
package gomavlib
import (
"errors"
"fmt"
)
type channelProvider struct {
node *Node
endpoint Endpoint
terminate chan struct{}
}
func (cp *channelProvider) initialize() error {
cp.terminate = make(chan struct{})
return nil
}
func (cp *channelProvider) close() {
close(cp.terminate)
cp.endpoint.close()
}
func (cp *channelProvider) start() {
cp.node.wg.Add(1)
go cp.run()
}
func (cp *channelProvider) run() {
defer cp.node.wg.Done()
for {
label, rwc, err := cp.endpoint.provide()
if err != nil {
if !errors.Is(err, errTerminated) {
panic("errTerminated is the only error allowed here")
}
break
}
ch := &Channel{
node: cp.node,
endpoint: cp.endpoint,
label: label,
rwc: rwc,
}
err = ch.initialize()
if err != nil {
panic(fmt.Errorf("newChannel unexpected error: %w", err))
}
cp.node.newChannel(ch)
if cp.endpoint.oneChannelAtAtime() {
// wait the channel to emit EventChannelClose
// before creating another channel
select {
case <-ch.done:
case <-cp.terminate:
return
}
}
}
}