forked from webex/webex-js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercury.js
568 lines (482 loc) · 17.3 KB
/
mercury.js
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/* eslint-disable require-jsdoc */
/*!
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
*/
import url from 'url';
import {WebexPlugin} from '@webex/webex-core';
import {deprecated, oneFlight} from '@webex/common';
import {camelCase, get, set} from 'lodash';
import backoff from 'backoff';
import Socket from './socket';
import {
BadRequest,
Forbidden,
NotAuthorized,
UnknownResponse,
ConnectionError,
// NotFound
} from './errors';
const normalReconnectReasons = ['idle', 'done (forced)', 'pong not received', 'pong mismatch'];
const Mercury = WebexPlugin.extend({
namespace: 'Mercury',
lastError: undefined,
session: {
connected: {
default: false,
type: 'boolean',
},
connecting: {
default: false,
type: 'boolean',
},
hasEverConnected: {
default: false,
type: 'boolean',
},
socket: 'object',
localClusterServiceUrls: 'object',
mercuryTimeOffset: {
default: undefined,
type: 'number',
},
},
derived: {
listening: {
deps: ['connected'],
fn() {
return this.connected;
},
},
},
initialize() {
/*
When one of these legacy feature gets updated, this event would be triggered
* group-message-notifications
* mention-notifications
* thread-notifications
*/
this.on('event:featureToggle_update', (envelope) => {
if (envelope && envelope.data) {
this.webex.internal.feature.updateFeature(envelope.data.featureToggle);
}
});
},
/**
* Get the last error.
* @returns {any} The last error.
*/
getLastError() {
return this.lastError;
},
@oneFlight
connect(webSocketUrl) {
if (this.connected) {
this.logger.info(`${this.namespace}: already connected, will not connect again`);
return Promise.resolve();
}
this.connecting = true;
this.logger.info(`${this.namespace}: starting connection attempt`);
this.logger.info(
`${this.namespace}: debug_mercury_logging stack: `,
new Error('debug_mercury_logging').stack
);
return Promise.resolve(
this.webex.internal.device.registered || this.webex.internal.device.register()
).then(() => {
this.logger.info(`${this.namespace}: connecting`);
return this._connectWithBackoff(webSocketUrl);
});
},
logout() {
this.logger.info(`${this.namespace}: logout() called`);
this.logger.info(
`${this.namespace}: debug_mercury_logging stack: `,
new Error('debug_mercury_logging').stack
);
return this.disconnect(
this.config.beforeLogoutOptionsCloseReason &&
!normalReconnectReasons.includes(this.config.beforeLogoutOptionsCloseReason)
? {code: 3050, reason: this.config.beforeLogoutOptionsCloseReason}
: undefined
);
},
@oneFlight
disconnect(options) {
return new Promise((resolve) => {
if (this.backoffCall) {
this.logger.info(`${this.namespace}: aborting connection`);
this.backoffCall.abort();
}
if (this.socket) {
this.socket.removeAllListeners('message');
this.once('offline', resolve);
resolve(this.socket.close(options || undefined));
}
resolve();
});
},
@deprecated('Mercury#listen(): Use Mercury#connect() instead')
listen() {
/* eslint no-invalid-this: [0] */
return this.connect();
},
@deprecated('Mercury#stopListening(): Use Mercury#disconnect() instead')
stopListening() {
/* eslint no-invalid-this: [0] */
return this.disconnect();
},
processRegistrationStatusEvent(message) {
this.localClusterServiceUrls = message.localClusterServiceUrls;
},
_applyOverrides(event) {
if (!event || !event.headers) {
return;
}
const headerKeys = Object.keys(event.headers);
headerKeys.forEach((keyPath) => {
set(event, keyPath, event.headers[keyPath]);
});
},
_prepareUrl(webSocketUrl) {
if (!webSocketUrl) {
webSocketUrl = this.webex.internal.device.webSocketUrl;
}
return this.webex.internal.feature
.getFeature('developer', 'web-high-availability')
.then((haMessagingEnabled) => {
if (haMessagingEnabled) {
return this.webex.internal.services.convertUrlToPriorityHostUrl(webSocketUrl);
}
return webSocketUrl;
})
.then((wsUrl) => {
webSocketUrl = wsUrl;
})
.then(() => this.webex.internal.feature.getFeature('developer', 'web-shared-mercury'))
.then((webSharedMercury) => {
webSocketUrl = url.parse(webSocketUrl, true);
Object.assign(webSocketUrl.query, {
outboundWireFormat: 'text',
bufferStates: true,
aliasHttpStatus: true,
});
if (webSharedMercury) {
Object.assign(webSocketUrl.query, {
mercuryRegistrationStatus: true,
isRegistrationRefreshEnabled: true,
});
Reflect.deleteProperty(webSocketUrl.query, 'bufferStates');
}
if (get(this, 'webex.config.device.ephemeral', false)) {
webSocketUrl.query.multipleConnections = true;
}
webSocketUrl.query.clientTimestamp = Date.now();
return url.format(webSocketUrl);
});
},
_attemptConnection(socketUrl, callback) {
const socket = new Socket();
let attemptWSUrl;
socket.on('close', (...args) => this._onclose(...args));
socket.on('message', (...args) => this._onmessage(...args));
socket.on('pong', (...args) => this._setTimeOffset(...args));
socket.on('sequence-mismatch', (...args) => this._emit('sequence-mismatch', ...args));
socket.on('ping-pong-latency', (...args) => this._emit('ping-pong-latency', ...args));
Promise.all([this._prepareUrl(socketUrl), this.webex.credentials.getUserToken()])
.then(([webSocketUrl, token]) => {
if (!this.backoffCall) {
const msg = `${this.namespace}: prevent socket open when backoffCall no longer defined`;
this.logger.info(msg);
return Promise.reject(new Error(msg));
}
attemptWSUrl = webSocketUrl;
let options = {
forceCloseDelay: this.config.forceCloseDelay,
pingInterval: this.config.pingInterval,
pongTimeout: this.config.pongTimeout,
token: token.toString(),
trackingId: `${this.webex.sessionId}_${Date.now()}`,
logger: this.logger,
};
// if the consumer has supplied request options use them
if (this.webex.config.defaultMercuryOptions) {
this.logger.info(`${this.namespace}: setting custom options`);
options = {...options, ...this.webex.config.defaultMercuryOptions};
}
// Set the socket before opening it. This allows a disconnect() to close
// the socket if it is in the process of being opened.
this.socket = socket;
this.logger.info(`${this.namespace} connection url: ${webSocketUrl}`);
return socket.open(webSocketUrl, options);
})
.then(() => {
this.logger.info(
`${this.namespace}: connected to mercury, success, action: connected, url: ${attemptWSUrl}`
);
callback();
return this.webex.internal.feature
.getFeature('developer', 'web-high-availability')
.then((haMessagingEnabled) => {
if (haMessagingEnabled) {
return this.webex.internal.device.refresh();
}
return Promise.resolve();
});
})
.catch((reason) => {
this.lastError = reason; // remember the last error
// Suppress connection errors that appear to be network related. This
// may end up suppressing metrics during outages, but we might not care
// (especially since many of our outages happen in a way that client
// metrics can't be trusted).
if (reason.code !== 1006 && this.backoffCall && this.backoffCall?.getNumRetries() > 0) {
this._emit('connection_failed', reason, {retries: this.backoffCall?.getNumRetries()});
}
this.logger.info(
`${this.namespace}: connection attempt failed`,
reason,
this.backoffCall?.getNumRetries() === 0 ? reason.stack : ''
);
// UnknownResponse is produced by IE for any 4XXX; treated it like a bad
// web socket url and let WDM handle the token checking
if (reason instanceof UnknownResponse) {
this.logger.info(
`${this.namespace}: received unknown response code, refreshing device registration`
);
return this.webex.internal.device.refresh().then(() => callback(reason));
}
// NotAuthorized implies expired token
if (reason instanceof NotAuthorized) {
this.logger.info(`${this.namespace}: received authorization error, reauthorizing`);
return this.webex.credentials.refresh({force: true}).then(() => callback(reason));
}
// // NotFound implies expired web socket url
// else if (reason instanceof NotFound) {
// this.logger.info(`mercury: received not found error, refreshing device registration`);
// return this.webex.internal.device.refresh()
// .then(() => callback(reason));
// }
// BadRequest implies current credentials are for a Service Account
// Forbidden implies current user is not entitle for Webex
if (reason instanceof BadRequest || reason instanceof Forbidden) {
this.logger.warn(`${this.namespace}: received unrecoverable response from mercury`);
this.backoffCall.abort();
return callback(reason);
}
if (reason instanceof ConnectionError) {
return this.webex.internal.feature
.getFeature('developer', 'web-high-availability')
.then((haMessagingEnabled) => {
if (haMessagingEnabled) {
this.logger.info(
`${this.namespace}: received a generic connection error, will try to connect to another datacenter. failed, action: 'failed', url: ${attemptWSUrl} error: ${reason.message}`
);
return this.webex.internal.services.markFailedUrl(attemptWSUrl);
}
return null;
})
.then(() => callback(reason));
}
return callback(reason);
})
.catch((reason) => {
this.logger.error(`${this.namespace}: failed to handle connection failure`, reason);
callback(reason);
});
},
_connectWithBackoff(webSocketUrl) {
return new Promise((resolve, reject) => {
// eslint gets confused about whether or not call is actually used
// eslint-disable-next-line prefer-const
let call;
const onComplete = (err) => {
this.connecting = false;
this.backoffCall = undefined;
if (err) {
this.logger.info(
`${
this.namespace
}: failed to connect after ${call.getNumRetries()} retries; log statement about next retry was inaccurate; ${err}`
);
return reject(err);
}
this.connected = true;
this.hasEverConnected = true;
this._emit('online');
return resolve();
};
// eslint-disable-next-line prefer-reflect
call = backoff.call((callback) => {
this.logger.info(`${this.namespace}: executing connection attempt ${call.getNumRetries()}`);
this._attemptConnection(webSocketUrl, callback);
}, onComplete);
call.setStrategy(
new backoff.ExponentialStrategy({
initialDelay: this.config.backoffTimeReset,
maxDelay: this.config.backoffTimeMax,
})
);
if (this.config.initialConnectionMaxRetries && !this.hasEverConnected) {
call.failAfter(this.config.initialConnectionMaxRetries);
} else if (this.config.maxRetries) {
call.failAfter(this.config.maxRetries);
}
call.on('abort', () => {
this.logger.info(`${this.namespace}: connection aborted`);
reject(new Error('Mercury Connection Aborted'));
});
call.on('callback', (err) => {
if (err) {
const number = call.getNumRetries();
const delay = Math.min(call.strategy_.nextBackoffDelay_, this.config.backoffTimeMax);
this.logger.info(
`${this.namespace}: failed to connect; attempting retry ${number + 1} in ${delay} ms`
);
/* istanbul ignore if */
if (process.env.NODE_ENV === 'development') {
this.logger.debug(`${this.namespace}: `, err, err.stack);
}
return;
}
this.logger.info(`${this.namespace}: connected`);
});
call.start();
this.backoffCall = call;
});
},
_emit(...args) {
try {
this.trigger(...args);
} catch (error) {
this.logger.error(
`${this.namespace}: error occurred in event handler:`,
error,
' with args: ',
args
);
}
},
_getEventHandlers(eventType) {
const [namespace, name] = eventType.split('.');
const handlers = [];
if (!this.webex[namespace] && !this.webex.internal[namespace]) {
return handlers;
}
const handlerName = camelCase(`process_${name}_event`);
if ((this.webex[namespace] || this.webex.internal[namespace])[handlerName]) {
handlers.push({
name: handlerName,
namespace,
});
}
return handlers;
},
_onclose(event) {
// I don't see any way to avoid the complexity or statement count in here.
/* eslint complexity: [0] */
try {
const reason = event.reason && event.reason.toLowerCase();
const socketUrl = this.socket.url;
this.socket.removeAllListeners();
this.unset('socket');
this.connected = false;
this._emit('offline', event);
switch (event.code) {
case 1003:
// metric: disconnect
this.logger.info(
`${this.namespace}: Mercury service rejected last message; will not reconnect: ${event.reason}`
);
this._emit('offline.permanent', event);
break;
case 4000:
// metric: disconnect
this.logger.info(`${this.namespace}: socket replaced; will not reconnect`);
this._emit('offline.replaced', event);
break;
case 1001:
case 1005:
case 1006:
case 1011:
this.logger.info(`${this.namespace}: socket disconnected; reconnecting`);
this._emit('offline.transient', event);
this._reconnect(socketUrl);
// metric: disconnect
// if (code == 1011 && reason !== ping error) metric: unexpected disconnect
break;
case 1000:
case 3050: // 3050 indicates logout form of closure, default to old behavior, use config reason defined by consumer to proceed with the permanent block
if (normalReconnectReasons.includes(reason)) {
this.logger.info(`${this.namespace}: socket disconnected; reconnecting`);
this._emit('offline.transient', event);
this._reconnect(socketUrl);
// metric: disconnect
// if (reason === done forced) metric: force closure
} else {
this.logger.info(
`${this.namespace}: socket disconnected; will not reconnect: ${event.reason}`
);
this._emit('offline.permanent', event);
}
break;
default:
this.logger.info(
`${this.namespace}: socket disconnected unexpectedly; will not reconnect`
);
// unexpected disconnect
this._emit('offline.permanent', event);
}
} catch (error) {
this.logger.error(`${this.namespace}: error occurred in close handler`, error);
}
},
_onmessage(event) {
this._setTimeOffset(event);
const envelope = event.data;
if (process.env.ENABLE_MERCURY_LOGGING) {
this.logger.debug(`${this.namespace}: message envelope: `, envelope);
}
const {data} = envelope;
this._applyOverrides(data);
return this._getEventHandlers(data.eventType)
.reduce(
(promise, handler) =>
promise.then(() => {
const {namespace, name} = handler;
return new Promise((resolve) =>
resolve((this.webex[namespace] || this.webex.internal[namespace])[name](data))
).catch((reason) =>
this.logger.error(
`${this.namespace}: error occurred in autowired event handler for ${data.eventType}`,
reason
)
);
}),
Promise.resolve()
)
.then(() => {
this._emit('event', event.data);
const [namespace] = data.eventType.split('.');
if (namespace === data.eventType) {
this._emit(`event:${namespace}`, envelope);
} else {
this._emit(`event:${namespace}`, envelope);
this._emit(`event:${data.eventType}`, envelope);
}
})
.catch((reason) => {
this.logger.error(`${this.namespace}: error occurred processing socket message`, reason);
});
},
_setTimeOffset(event) {
const {wsWriteTimestamp} = event.data;
if (typeof wsWriteTimestamp === 'number' && wsWriteTimestamp > 0) {
this.mercuryTimeOffset = Date.now() - wsWriteTimestamp;
}
},
_reconnect(webSocketUrl) {
this.logger.info(`${this.namespace}: reconnecting`);
return this.connect(webSocketUrl);
},
});
export default Mercury;