-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapptelemetrywebsocketclient.go
403 lines (340 loc) · 10.4 KB
/
apptelemetrywebsocketclient.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package gocbcore
import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"github.com/gorilla/websocket"
"math/rand"
"net/http"
"sync"
"sync/atomic"
"time"
)
const (
telemetryWebsocketClientMinBackoff = 100 * time.Millisecond
telemetryWebsocketClientDefaultMaxBackoff = 1 * time.Hour
telemetryWebsocketClientDefaultPingInterval = 30 * time.Second
telemetryWebsocketClientDefaultPingTimeout = 5 * time.Second
)
type telemetryCommand uint8
const (
telemetryCommandGetTelemetry = telemetryCommand(0x00)
)
type telemetryStatus uint8
const (
telemetryStatusSuccess = telemetryStatus(0x00)
telemetryStatusUnknownCommand = telemetryStatus(0x01)
)
type telemetryResponse struct {
status telemetryStatus
data []byte
}
func (r *telemetryResponse) encode() []byte {
encoded := r.data
encoded = append(encoded, byte(0))
copy(encoded[1:], encoded)
encoded[0] = byte(r.status)
return encoded
}
type telemetryEndpoints struct {
epList []routeEndpoint
tlsConfig *dynTLSConfig
auth AuthProvider
}
func (e *telemetryEndpoints) selectEndpoint(excludeAddress string) (string, bool) {
if len(e.epList) == 0 {
return "", false
}
if len(e.epList) == 1 {
return e.epList[0].Address, true
}
var candidates []string
for _, ep := range e.epList {
if ep.Address != excludeAddress {
candidates = append(candidates, ep.Address)
}
}
return candidates[rand.Intn(len(candidates))], true // #nosec G404
}
func (e *telemetryEndpoints) createDialer(address string) (*websocket.Dialer, error) {
var tlsConfig *tls.Config
var err error
if e.tlsConfig != nil {
tlsConfig, err = e.tlsConfig.MakeForAddr(trimSchemePrefix(address))
if err != nil {
return nil, err
}
}
return &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
TLSClientConfig: tlsConfig,
}, nil
}
func (e *telemetryEndpoints) getAuthHeader(address string) (http.Header, error) {
creds, err := e.auth.Credentials(AuthCredsRequest{Service: MgmtService, Endpoint: address})
if err != nil {
return nil, err
}
if len(creds) != 1 {
return nil, errInvalidCredentials
}
return http.Header{
"Authorization": []string{
"Basic " + base64.StdEncoding.EncodeToString(
[]byte(fmt.Sprintf("%s:%s", creds[0].Username, creds[0].Password))),
},
}, nil
}
type telemetryWebsocketClient struct {
endpoints telemetryEndpoints
fixedEndpoints bool
maxBackoff time.Duration
pingInterval time.Duration
pingTimeout time.Duration
getMetricsFn func() string
conn *websocket.Conn
endpointLookupMutex sync.Mutex
lastEndpointAddress string
connInitiated atomic.Bool // This is set to true if there is an active connection attempt loop in progress.
shutdownSig chan struct{}
}
func newTelemetryWebsocketClient(maxBackoff, pingInterval, pingTimeout time.Duration, getMetricsFn func() string) *telemetryWebsocketClient {
if maxBackoff == time.Duration(0) {
maxBackoff = telemetryWebsocketClientDefaultMaxBackoff
}
if pingInterval == time.Duration(0) {
pingInterval = telemetryWebsocketClientDefaultPingInterval
}
if pingTimeout == time.Duration(0) {
pingTimeout = telemetryWebsocketClientDefaultPingTimeout
}
w := &telemetryWebsocketClient{
maxBackoff: maxBackoff,
pingInterval: pingInterval,
pingTimeout: pingTimeout,
getMetricsFn: getMetricsFn,
shutdownSig: make(chan struct{}),
}
return w
}
func (w *telemetryWebsocketClient) usesExternalEndpoint() bool {
return w.fixedEndpoints
}
func (w *telemetryWebsocketClient) connectionInitiated() bool {
return w.connInitiated.Load()
}
func (w *telemetryWebsocketClient) updateEndpoints(endpoints telemetryEndpoints) {
if w.fixedEndpoints {
// We shouldn't get here (telemetry components will not be listening to config updates in this case), but if
// we do then ignore the update.
return
}
w.endpointLookupMutex.Lock()
w.endpoints = endpoints
w.endpointLookupMutex.Unlock()
}
func (w *telemetryWebsocketClient) connectIfNotStarted() {
props, ok := w.createConnectionAttemptProps(true)
if !ok {
return
}
go w.connect(props)
}
type telemetryWebsocketClientConnectionProps struct {
address string
dialer *websocket.Dialer
header http.Header
}
func (w *telemetryWebsocketClient) createConnectionAttemptProps(withShouldConnectCheck bool) (telemetryWebsocketClientConnectionProps, bool) {
w.endpointLookupMutex.Lock()
defer w.endpointLookupMutex.Unlock()
// If this function is called by an active connection attempt loop, as part of a retry, we shouldn't do this check.
if withShouldConnectCheck {
shouldConnect := w.connInitiated.CompareAndSwap(false, true)
if !shouldConnect {
return telemetryWebsocketClientConnectionProps{}, false
}
}
address, ok := w.endpoints.selectEndpoint(w.lastEndpointAddress)
if !ok {
logDebugf("No app telemetry endpoints available")
w.connInitiated.Store(false)
return telemetryWebsocketClientConnectionProps{}, false
}
var header http.Header
var err error
if !w.fixedEndpoints {
header, err = w.endpoints.getAuthHeader(address)
if err != nil {
w.connInitiated.Store(false)
logWarnf("Failed to create auth header for telemetry reporting: %v", err)
return telemetryWebsocketClientConnectionProps{}, false
}
}
dialer, err := w.endpoints.createDialer(address)
if err != nil {
w.connInitiated.Store(false)
logWarnf("Failed to create websocket dialer for telemetry reporting: %v", err)
return telemetryWebsocketClientConnectionProps{}, false
}
w.lastEndpointAddress = address
return telemetryWebsocketClientConnectionProps{
address: address,
dialer: dialer,
header: header,
}, true
}
func (w *telemetryWebsocketClient) listenForTelemetryRequests() (retry bool) {
logDebugf("Starting app telemetry listener. Address=%s", w.conn.RemoteAddr().String())
var clientClosedConnection atomic.Bool
stopCh := make(chan struct{})
go func() {
select {
case <-w.shutdownSig:
logInfof("Closing App telemetry websocket connection")
clientClosedConnection.Store(true)
w.conn.Close()
case <-stopCh:
}
}()
err := w.readWritePump() // This will always return an error – even if err could be nil the behaviour is the same.
if clientClosedConnection.Load() {
w.conn = nil
logInfof("Closed App telemetry websocket connection")
return false
}
close(stopCh)
w.conn.Close()
w.conn = nil
logDebugf("Error from readWritePump: %s.", err)
return true
}
func (w *telemetryWebsocketClient) connect(props telemetryWebsocketClientConnectionProps) {
backoffCalculator := ExponentialBackoff(telemetryWebsocketClientMinBackoff, w.maxBackoff, 2)
var retryAttempts uint32 = 0
for {
conn, err := w.dialConn(props)
if err != nil {
if errors.Is(err, context.Canceled) {
// The client has been closed, don't attempt to reconnect.
w.connInitiated.Store(false)
return
}
logWarnf("Failed to establish websocket connection for telemetry reporter: %v", err)
} else {
w.conn = conn
retryAttempts = 0 // We created a connection successfully Reset exponential backoff
retry := w.listenForTelemetryRequests() // Start read/write loop
if !retry {
w.connInitiated.Store(false)
return
}
}
backoff := backoffCalculator(retryAttempts)
retryAttempts++
logInfof("App telemetry connection closed. Retrying in %s", backoff)
select {
case <-w.shutdownSig:
logInfof("App telemetry websocket client shutting down, canceling connection attempt")
return
case <-time.After(backoff):
}
var ok bool
props, ok = w.createConnectionAttemptProps(false)
if !ok {
w.connInitiated.Store(false)
return
}
}
}
func (w *telemetryWebsocketClient) dialConn(props telemetryWebsocketClientConnectionProps) (*websocket.Conn, error) {
logDebugf("Connecting to app telemetry endpoint: dialing %s", props.address)
ctx, cancel := context.WithCancel(context.Background())
stopCh := make(chan struct{})
go func() {
select {
case <-w.shutdownSig:
logInfof("App telemetry websocket client shutting down, cancelling connection attempt")
cancel()
case <-stopCh:
}
}()
defer close(stopCh)
conn, _, err := props.dialer.DialContext(ctx, props.address, props.header) // nolint: bodyclose
if err != nil {
return nil, err
}
return conn, nil
}
func (w *telemetryWebsocketClient) readWritePump() error {
pingStopCh := make(chan struct{})
defer close(pingStopCh)
err := w.startPingTicker(pingStopCh)
if err != nil {
return err
}
for {
_, message, err := w.conn.ReadMessage()
if err != nil {
return wrapError(err, "Error reading from app telemetry websocket")
}
cmd := telemetryCommand(message[0])
var resp telemetryResponse
switch cmd {
case telemetryCommandGetTelemetry:
logSchedf("Received GET_TELEMETRY command from server telemetry collector")
resp.status = telemetryStatusSuccess
metrics := w.getMetricsFn()
resp.data = []byte(metrics)
default:
logSchedf("Received unknown command from server telemetry collector")
resp.status = telemetryStatusUnknownCommand
}
logSchedf("Sending telemetry response to server telemetry collector. Size=%d bytes", len(resp.data))
err = w.conn.WriteMessage(websocket.BinaryMessage, resp.encode())
if err != nil {
return wrapError(err, "Error writing to app telemetry websocket")
}
}
}
func (w *telemetryWebsocketClient) startPingTicker(stopCh chan struct{}) error {
err := w.conn.SetReadDeadline(time.Now().Add(w.pingInterval + w.pingTimeout))
if err != nil {
return wrapError(err, "Could not update read deadline")
}
lastPingTimestamp := time.Now().UnixMilli()
w.conn.SetPongHandler(func(string) error {
err := w.conn.SetReadDeadline(time.UnixMilli(atomic.LoadInt64(&lastPingTimestamp)).Add(w.pingInterval + w.pingTimeout))
if err != nil {
return wrapError(err, "Could not update read deadline in pong handler")
}
return nil
})
go func() {
ticker := time.NewTicker(w.pingInterval)
defer ticker.Stop()
for {
select {
case <-stopCh:
return
case <-ticker.C:
atomic.StoreInt64(&lastPingTimestamp, time.Now().UnixMilli())
err := w.conn.WriteControl(websocket.PingMessage, nil, time.Now().Add(w.pingTimeout))
if err != nil {
logDebugf("Error writing PING message to telemetry reporter websocket: %v", err)
// No need to take any action on this error - reads will time out if we are unable to send pings, as
// the read deadline will not be increased.
return
}
}
}
}()
return nil
}
func (w *telemetryWebsocketClient) Close() {
logDebugf("Closing app telemetry websocket client")
close(w.shutdownSig)
}