Skip to content
This repository has been archived by the owner on Dec 9, 2023. It is now read-only.

Commit

Permalink
update connection routines
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed May 5, 2018
1 parent 43b5a63 commit 1d70206
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 222 deletions.
142 changes: 64 additions & 78 deletions apiclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,6 @@ function getFetchPromise(request) {
return fetchWithTimeout(request.url, fetchRequest, request.timeout);
}

function getServerAddress(server, mode) {

switch (mode) {
case 0:
case 'local':
return server.LocalAddress;
case 2:
case 'manual':
return server.ManualAddress;
case 1:
case 'remote':
return server.RemoteAddress;
default:
return server.ManualAddress || server.LocalAddress || server.RemoteAddress;
}
}

/**
* Creates a new api client instance
* @param {String} serverAddress
Expand Down Expand Up @@ -321,7 +304,7 @@ class ApiClient {
}

// http://api.jquery.com/jQuery.ajax/
if (!error && enableReconnection) {
if ((!error || !error.status) && enableReconnection) {
console.log("Attempting reconnection");

const previousServerAddress = instance.serverAddress();
Expand Down Expand Up @@ -699,22 +682,14 @@ class ApiClient {
}, includeAuthorization);
}

updateServerInfo(server, connectionMode) {
updateServerInfo(server, serverUrl) {

if (server == null) {
throw new Error('server cannot be null');
}

if (connectionMode == null) {
throw new Error('connectionMode cannot be null');
}

console.log(`Begin updateServerInfo. connectionMode: ${connectionMode}`);

this.serverInfo(server);

const serverUrl = getServerAddress(server, connectionMode);

if (!serverUrl) {
throw new Error(`serverUrl cannot be null. serverInfo: ${JSON.stringify(server)}`);
}
Expand Down Expand Up @@ -3623,83 +3598,94 @@ function setSavedEndpointInfo(instance, info) {
instance._endPointInfo = info;
}

function switchConnectionMode(instance, connectionMode) {
function getTryConnectPromise(instance, url, state, resolve, reject) {

const currentServerInfo = instance.serverInfo();
let newConnectionMode = connectionMode;
console.log('getTryConnectPromise ' + url);

newConnectionMode--;
if (newConnectionMode < 0) {
newConnectionMode = 'manual';
}
return fetchWithTimeout(url + "/system/info/public", {

if (getServerAddress(currentServerInfo, newConnectionMode)) {
return newConnectionMode;
}
method: 'GET',
accept: 'application/json'

newConnectionMode--;
if (newConnectionMode < 0) {
newConnectionMode = 'manual';
}
// Commenting this out since the fetch api doesn't have a timeout option yet
//timeout: timeout

if (getServerAddress(currentServerInfo, newConnectionMode)) {
return newConnectionMode;
}
}, 15000).then(() => {

return connectionMode;
}
if (!state.resolved) {
state.resolved = true;

function tryReconnectInternal(instance, resolve, reject, connectionMode, currentRetryCount) {
console.log("Reconnect succeeded to " + url);
instance.serverAddress(url);
resolve();
}

connectionMode = switchConnectionMode(instance, connectionMode);
const url = getServerAddress(instance.serverInfo(), connectionMode);
}, () => {

console.log(`Attempting reconnection to ${url}`);
console.log("Reconnect failed to " + url);

const timeout = connectionMode === 'local' ? 7000 : 15000;
state.rejects++;
if (state.rejects >= state.numAddresses) {
reject();
}
});
}

fetchWithTimeout(`${url}/system/info/public`, {
function tryReconnectInternal(instance) {

method: 'GET',
accept: 'application/json'
const addresses = [];
const addressesStrings = [];

// Commenting this out since the fetch api doesn't have a timeout option yet
//timeout: timeout
const serverInfo = instance.serverInfo();
if (serverInfo.LocalAddress && addressesStrings.indexOf(serverInfo.LocalAddress) === -1) {
addresses.push({ url: serverInfo.LocalAddress, timeout: 0 });
addressesStrings.push(addresses[addresses.length - 1].url);
}
if (serverInfo.ManualAddress && addressesStrings.indexOf(serverInfo.ManualAddress) === -1) {
addresses.push({ url: serverInfo.ManualAddress, timeout: 100 });
addressesStrings.push(addresses[addresses.length - 1].url);
}
if (serverInfo.RemoteAddress && addressesStrings.indexOf(serverInfo.RemoteAddress) === -1) {
addresses.push({ url: serverInfo.RemoteAddress, timeout: 200 });
addressesStrings.push(addresses[addresses.length - 1].url);
}

}, timeout).then(() => {
console.log('tryReconnect: ' + addressesStrings.join('|'));

console.log(`Reconnect succeeded to ${url}`);
return new Promise((resolve, reject) => {

instance.serverInfo().LastConnectionMode = connectionMode;
instance.serverAddress(url);
const state = {};
state.numAddresses = addresses.length;
state.rejects = 0;

resolve();
addresses.map((url) => {

}, () => {
setTimeout(() => {
getTryConnectPromise(instance, url.url, state, resolve, reject);

console.log(`Reconnect attempt failed to ${url}`);
}, url.timeout);
});
});
}

if (currentRetryCount < 4) {
function tryReconnect(instance, retryCount) {

const newConnectionMode = switchConnectionMode(instance, connectionMode);
retryCount = retryCount || 0;

setTimeout(() => {
tryReconnectInternal(instance, resolve, reject, newConnectionMode, currentRetryCount + 1);
}, 300);
if (retryCount >= 20) {
return Promise.reject();
}

} else {
reject();
}
});
}
return tryReconnectInternal(instance).catch((err) => {

function tryReconnect(instance) {
console.log('error in tryReconnectInternal: ' + (err || ''));

return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {

setTimeout(() => {
tryReconnectInternal(instance, resolve, reject, instance.serverInfo().LastConnectionMode, 0);
}, 300);
setTimeout(() => {
tryReconnect(instance, retryCount + 1).then(resolve, reject);
}, 500);
});
});
}

Expand Down
Loading

0 comments on commit 1d70206

Please sign in to comment.