Try another method for handling broken connections
This commit is contained in:
parent
0f5de673fe
commit
cb6e1b5f36
1 changed files with 30 additions and 4 deletions
|
@ -40,7 +40,10 @@ exports.getStatus = () => {
|
||||||
resolve(statuses);
|
resolve(statuses);
|
||||||
}, 5000);
|
}, 5000);
|
||||||
for (const connection of this.connections) {
|
for (const connection of this.connections) {
|
||||||
if (!connection.remoteAddress) continue;
|
if (!connection.remoteAddress) {
|
||||||
|
serversLeft--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
fetch(`http://${connection.remoteAddress}:8081/running`).then(statusRequest => statusRequest.json()).then((status) => {
|
fetch(`http://${connection.remoteAddress}:8081/running`).then(statusRequest => statusRequest.json()).then((status) => {
|
||||||
serversLeft--;
|
serversLeft--;
|
||||||
statuses.push(status);
|
statuses.push(status);
|
||||||
|
@ -49,7 +52,17 @@ exports.getStatus = () => {
|
||||||
resolve(statuses);
|
resolve(statuses);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}).catch(e => reject(e));
|
}).catch(e => {
|
||||||
|
if (e.code === "ECONNREFUSED") {
|
||||||
|
serversLeft--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
reject(e);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!serversLeft) {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
resolve(statuses);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -130,7 +143,7 @@ const getIdeal = () => {
|
||||||
}
|
}
|
||||||
}, 5000);
|
}, 5000);
|
||||||
for (const connection of this.connections) {
|
for (const connection of this.connections) {
|
||||||
if (!connection.remoteAddress || connection.destroyed) {
|
if (!connection.remoteAddress) {
|
||||||
serversLeft--;
|
serversLeft--;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -147,7 +160,13 @@ const getIdeal = () => {
|
||||||
const server = await chooseServer(idealServers);
|
const server = await chooseServer(idealServers);
|
||||||
resolve(this.connections.find(val => val.remoteAddress === server.addr));
|
resolve(this.connections.find(val => val.remoteAddress === server.addr));
|
||||||
}
|
}
|
||||||
}).catch(e => reject(e));
|
}).catch(e => {
|
||||||
|
if (e.code === "ECONNREFUSED") {
|
||||||
|
serversLeft--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
reject(e);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (!serversLeft) {
|
if (!serversLeft) {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
|
@ -162,6 +181,13 @@ const start = (object, num) => {
|
||||||
return getIdeal().then(async (currentServer) => {
|
return getIdeal().then(async (currentServer) => {
|
||||||
const data = Buffer.concat([Buffer.from([0x01 /* queue job */]), Buffer.from(num.length.toString()), Buffer.from(num), Buffer.from(JSON.stringify(object))]);
|
const data = Buffer.concat([Buffer.from([0x01 /* queue job */]), Buffer.from(num.length.toString()), Buffer.from(num), Buffer.from(JSON.stringify(object))]);
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
if (currentServer.destroyed) {
|
||||||
|
logger.log(`Lost connection to ${currentServer.remoteAddress}, attempting to reconnect...`);
|
||||||
|
currentServer.connect(8080, currentServer.remoteAddress, () => {
|
||||||
|
const res = start(object, num);
|
||||||
|
reject(res); // this is done to differentiate the result from a step
|
||||||
|
});
|
||||||
|
}
|
||||||
currentServer.write(data, (err) => {
|
currentServer.write(data, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.code === "EPIPE") {
|
if (err.code === "EPIPE") {
|
||||||
|
|
Loading…
Reference in a new issue