-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathendpoint_client.go
More file actions
89 lines (71 loc) · 1.5 KB
/
endpoint_client.go
File metadata and controls
89 lines (71 loc) · 1.5 KB
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
package gomavlib
import (
"context"
"io"
"time"
"github.com/bluenviron/gomavlib/v3/pkg/timednetconn"
)
var reconnectPeriod = 2 * time.Second
type endpointClient struct {
node *Node
conf EndpointCustomClient
ctx context.Context
ctxCancel func()
first bool
}
func (e *endpointClient) initialize() error {
e.ctx, e.ctxCancel = context.WithCancel(context.Background())
return nil
}
func (e *endpointClient) isEndpoint() {}
func (e *endpointClient) Conf() EndpointConf {
return e.conf
}
func (e *endpointClient) close() {
e.ctxCancel()
}
func (e *endpointClient) oneChannelAtAtime() bool {
return true
}
func (e *endpointClient) connect() (io.ReadWriteCloser, error) {
timedContext, timedContextClose := context.WithTimeout(e.ctx, e.node.ReadTimeout)
nconn, err := e.conf.Connect(timedContext)
timedContextClose()
if err != nil {
return nil, err
}
return timednetconn.New(
e.node.IdleTimeout,
e.node.WriteTimeout,
nconn,
), nil
}
func (e *endpointClient) provide() (string, io.ReadWriteCloser, error) {
if !e.first {
e.first = true
} else {
select {
case <-time.After(reconnectPeriod):
case <-e.ctx.Done():
return "", nil, errTerminated
}
}
for {
conn, err := e.connect()
if err != nil {
select {
case <-time.After(reconnectPeriod):
continue
case <-e.ctx.Done():
return "", nil, errTerminated
}
}
return e.label(), conn, nil
}
}
func (e *endpointClient) label() string {
if e.conf.Label != "" {
return e.conf.Label
}
return "custom"
}