Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsbradleyjr committed Dec 12, 2018
1 parent 25520c7 commit 8dfef2a
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions src/lib/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,33 @@ function getJson(url, cb) {
httpGetJson(url, cb);
}

function pingEndpoint(host, port, type, protocol, origin, callback, count = 0) {
// remove trailing api key from infura, ie rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
function pingEndpoint(host, port, type, protocol, origin, callback, count = 0, start = Date.now()) {
// remove trailing api key from infura, e.g. rinkeby.infura.io/nmY8WtT4QfEwz2S7wTbl
const _host = host.indexOf('/') > -1 ? host.split('/')[0] : host;
const max_retries = 600;
const timeout = 1000;
const maxWait = 600000;
const timeout = 100;

const adjustTimeout = () => {
const t = Math.floor(
(1 + (0.1 * (count && (Math.pow(2, count - 1) / 1)))) * timeout
);
return t > 3000 ? 3000 : t;
};

let alreadyClosed = false;
let retrying = false;

const doCallback = (v) => {
if (typeof v !== 'undefined') {
callback._doCallback = !!v;
const shouldCallback = (...args) => {
if (args.length) {
callback._shouldCallback = !!args[0];
}
let _doCallback = callback._doCallback;
if (typeof _doCallback === 'undefined') {
_doCallback = true;
let shouldCallback;
if (callback.hasOwnProperty('_shouldCallback')) {
shouldCallback = !!callback._shouldCallback;
} else {
_doCallback = !!_doCallback;
shouldCallback = true;
}
return _doCallback;
return shouldCallback;
};

const cleanup = (req, closeMethod) => {
Expand All @@ -118,38 +125,37 @@ function pingEndpoint(host, port, type, protocol, origin, callback, count = 0) {
}
};

const maybeRetry = (req, closeMethod, cond, ...args) => {
cleanup(req, closeMethod);
if (doCallback()) {
if (cond) {
const handleEvent = (req, closeMethod, retryCond, ...args) => {
if (shouldCallback()) {
if (!retrying && retryCond) {
retrying = true;
setImmediate(() => {
pingEndpoint(host, port, type, protocol, origin, callback, ++count);
pingEndpoint(
host, port, type, protocol, origin, callback, ++count, start
);
});
} else {
doCallback(false);
} else if (!alreadyClosed) {
shouldCallback(false);
callback(...args);
}
}
// following cleanup any later events on req will effectively be ignored
cleanup(req, closeMethod);
};

const handleError = (req, closeMethod) => {
req.on('error', (err) => {
if (!retrying) {
maybeRetry(
req,
closeMethod,
(/timed out/).test(err.message) && count < max_retries,
err
);
} else {
cleanup(req, closeMethod);
}
handleEvent(
req,
closeMethod,
(/timed out/).test(err.message) && Date.now() - start < maxWait,
err
);
});
};

const handleSuccess = (req, closeMethod, event) => {
req.once(event, () => { maybeRetry(req, closeMethod, false); });
req.once(event, () => { handleEvent(req, closeMethod, false); });
};

const handleRequest = (req, closeMethod, event) => {
Expand All @@ -160,20 +166,16 @@ function pingEndpoint(host, port, type, protocol, origin, callback, count = 0) {
if (type === 'ws') {
const req = new (require('ws'))(
`${protocol === 'https' ? 'wss' : 'ws'}://${_host}:${port}/`,
{handshakeTimeout: timeout, origin}
{handshakeTimeout: adjustTimeout(), origin}
);
handleRequest(req, 'close', 'open');
} else {
const req = (protocol === 'https' ? require('https') : require('http')).get(
{host: _host, origin, port, timeout}
{host: _host, origin, port}
);
handleRequest(req, 'abort', 'response');
req.once('socket', (sock) => {
sock.once('connect', () => {
req.once('timeout', () => {
req.emit('error', new Error('timed out'));
});
});
req.setTimeout(adjustTimeout(), () => {
req.emit('error', new Error('timed out'));
});
}
}
Expand Down

0 comments on commit 8dfef2a

Please sign in to comment.