fix: do a full reconnect to internal and relay

This commit is contained in:
Alexis
2021-09-15 15:20:27 +09:30
parent efbfd74696
commit 250d65187a
2 changed files with 50 additions and 18 deletions
+21 -16
View File
@@ -108,32 +108,37 @@ am(async () => {
return;
}
apiManagerLogger.debug('Replacing my_servers key. Last known key was %s. New key is %s', lastKnownApiKey, newApiKey);
// Disconnect from relay
sockets.relay?.close();
coreLogger.debug('Disconnected from relay ws.');
// Just disconnect everything as the key is undefined
// Disconnect from internal ws
sockets.internal?.close();
coreLogger.debug('Disconnected from internal ws.');
// Disconnect from mothership's subscription endpoint
mothership.close();
coreLogger.debug('Disconnected from mothership\'s subscription endpoint.');
// Record last known key
lastKnownApiKey = newApiKey;
// Since we no longer have a key and
// everything is disconnected we can bail
if (newApiKey === undefined) {
// Clear out old API key
lastKnownApiKey = undefined;
// Disconnect from relay
sockets.relay?.close();
coreLogger.debug('Disconnected from relay ws.');
// Disconnect from internal ws
sockets.internal?.close();
coreLogger.debug('Disconnected from internal ws.');
// Disconnect from mothership's subscription endpoint
mothership.close();
coreLogger.debug('Disconnected from mothership\'s subscription endpoint.');
apiManagerLogger.debug('Cleared my_servers key.');
return;
}
apiManagerLogger.debug('Replacing my_servers key. Last known key was %s. New key is %s', lastKnownApiKey, newApiKey);
// We've never had a key before so let's start the internal API connection
// That'll then start the relay connection to mothership
if (!hasFirstKey && lastKnownApiKey === undefined) {
coreLogger.debug('First time with a valid API key, waiting 5s to connect.');
// Wait 5s for the API to come up before connecting
setTimeout(() => {
coreLogger.debug('Connecting to internal for the first time.');
// Start the internal relay connection
// This will connect to relay once it's up
startInternal(newApiKey);
+29 -2
View File
@@ -16,8 +16,17 @@ export const sockets = {
};
let internalOpen = false;
let relayOpen = false;
let isLocalConnecting = false;
let isRelayConnecting = false;
export const startInternal = (apiKey: string) => {
// Another process has already kicked this off
if (isLocalConnecting) {
return;
}
// Start the connection
isLocalConnecting = true;
log.debug('⌨️ INTERNAL:CONNECTING');
sockets.internal = new GracefulWebSocket(INTERNAL_WS_LINK, ['graphql-ws'], {
headers: {
@@ -27,6 +36,7 @@ export const startInternal = (apiKey: string) => {
sockets.internal.on('connected', () => {
log.debug('⌨️ INTERNAL:CONNECTED');
isLocalConnecting = false;
internalOpen = true;
sockets.internal?.send(JSON.stringify({
type: 'connection_init',
@@ -40,12 +50,19 @@ export const startInternal = (apiKey: string) => {
subscribeToServers(apiKey);
});
sockets.internal?.on('disconnected', () => {
sockets.internal.on('disconnected', () => {
log.debug('⌨️ INTERNAL:DISCONNECTED');
isLocalConnecting = false;
internalOpen = false;
});
sockets.internal?.on('message', e => {
sockets.internal.on('killed', () => {
isLocalConnecting = false;
internalOpen = false;
log.debug('☁️ INTERNAL:KILLED');
});
sockets.internal.on('message', e => {
// Skip auth acknowledgement
if (e.data === '{"type":"connection_ack"}') {
return;
@@ -88,6 +105,13 @@ const serializer = new IniSerializer({
});
export const startRelay = () => {
// Another process has already kicked this off
if (isRelayConnecting) {
return;
}
// Start the connection
isRelayConnecting = true;
log.debug('☁️ RELAY:CONNECTING');
sockets.relay = new GracefulWebSocket(MOTHERSHIP_RELAY_WS_LINK, ['graphql-ws'], {
headers: getRelayHeaders()
@@ -95,18 +119,21 @@ export const startRelay = () => {
// Connection-state related events
sockets.relay.on('connected', () => {
isRelayConnecting = false;
relayOpen = true;
log.debug('☁️ RELAY:CONNECTED');
});
sockets.relay.on('disconnected', () => {
log.debug('☁️ RELAY:DISCONNECTED');
isRelayConnecting = false;
relayOpen = false;
sockets.internal?.close();
sockets.internal?.start();
});
sockets.relay.on('killed', () => {
isRelayConnecting = false;
log.debug('☁️ RELAY:KILLED');
});