fix: mothership reconnection logic

This commit is contained in:
Alexis Tyler
2020-12-16 06:38:09 +10:30
parent ed9b6274f9
commit dc77025aae
2 changed files with 9 additions and 5 deletions
+8 -4
View File
@@ -195,8 +195,8 @@ class MothershipService {
// Relay is closed
this.relay.on('close', async function (this: WebSocketWithHeartBeat, code, _message) {
try {
const message = JSON.parse(_message);
mothershipLogger.debug('Connection closed with code=%s reason="%s"', code, message.message);
const message = _message.trim() === '' ? { message: '' } : JSON.parse(_message);
mothershipLogger.debug('Connection closed with code=%s reason="%s"', code, code === 1006 ? 'Terminated' : message.message);
// Stop ws heartbeat
if (this.pingTimeout) {
@@ -262,14 +262,18 @@ class MothershipService {
if (code === 4500) {
await sleep(ONE_SECOND * 5);
}
} catch (error) {
mothershipLogger.debug('Connection closed with code=%s reason="%s"', code, error.message);
}
try {
// Wait a few seconds
await sleep(backoff(mothership.connectionAttempt, ONE_MINUTE, 5));
// Reconnect
await mothership.connect(wsServer, mothership.connectionAttempt + 1);
} catch (error) {
mothershipLogger.error('close error', error);
mothershipLogger.debug('Failed reconnecting to mothership reason="%s"', error.message);
}
});
+1 -1
View File
@@ -5,7 +5,7 @@ import { userCache, CachedServers } from '../cache';
import { log } from '../core';
const client = new SubscriptionClient(MOTHERSHIP_GRAPHQL_LINK, {
reconnect: false,
reconnect: true,
lazy: true, // only connect when there is a query
connectionCallback: (errors) => {
if (errors) {