Skip to content

Instantly share code, notes, and snippets.

@talkol
Last active October 22, 2016 11:14
Show Gist options
  • Select an option

  • Save talkol/1962bd6c78f92cb08966bd5335d39b2b to your computer and use it in GitHub Desktop.

Select an option

Save talkol/1962bd6c78f92cb08966bd5335d39b2b to your computer and use it in GitHub Desktop.
import request from 'request';
export function pingServers(servers, onComplete) {
let state = {
servers,
currentServer: 0,
currentPingNum: 0,
failedServers: {}
};
handleState(state, onComplete);
}
function handleState(state, onComplete) {
if (state.currentServer >= state.servers.length) {
onComplete(state.failedServers);
return;
}
if (state.currentPingNum >= 3) {
state.currentServer++;
state.currentPingNum = 0;
setImmediate(() => handleState(state, onComplete));
return;
}
const url = state.servers[state.currentServer];
request(url, (error, response) => {
if (error || response.statusCode !== 200) {
if (!state.failedServers[url]) state.failedServers[url] = 0;
state.failedServers[url]++;
}
state.currentPingNum++;
setTimeout(() => handleState(state, onComplete), 10000);
return;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment