Handle connection being closed in slave

Listen for the close event and connect again if the connection was
closed from proper disconnect or initiate a shutdown if the connection
was unexpectedly closed.  Unexpected closure happens from authentication
failure and if a connection fails to reconnect before the timeout.
This commit is contained in:
Hornwitser committed 2020-05-09 16:24:02 +02:00
1 parent c35b6de70e
commit c87538ca58
1 file changed
+34
+34
View File
@@ -15,6 +15,7 @@ const path = require('path');
const yargs = require("yargs");
const events = require("events");
const pidusage = require("pidusage");
const setBlocking = require("set-blocking");
const version = require("./package").version;
// internal libraries
@@ -595,6 +596,37 @@ class Slave extends link.Link {
this.instanceConnections = new Map();
this.instanceInfos = new Map();
this._disconnecting = false;
this._shuttingDown = false;
this.connector.on("close", () => {
if (this._shuttingDown) {
return;
}
if (this._disconnecting) {
this._disconnecting = false;
this.connector.connect().catch((err) => {
console.error("ERROR: Unexpected error reconnecting to master");
console.error(err);
this.shutdown().catch((err) => {
setBlocking(true);
console.error("ERROR: Unexpected error during shutdown");
console.error(err);
process.exit(1)
});
});
} else {
console.error("ERROR: Master connection was unexpectedly closed");
this.shutdown().catch((err) => {
setBlocking(true);
console.error("ERROR: Unexpected error during shutdown");
console.error(err);
process.exit(1)
});
}
});
for (let event of ["connect", "drop", "close"]) {
this.connector.on(event, () => {
for (let instanceConnection of this.instanceConnections.values()) {
@@ -791,6 +823,7 @@ class Slave extends link.Link {
}
async prepareDisconnectRequestHandler(message, request) {
this._disconnecting = true;
for (let instanceConnection of this.instanceConnections.values()) {
await link.messages.prepareMasterDisconnect.send(instanceConnection);
}
@@ -802,6 +835,7 @@ class Slave extends link.Link {
* Stops all instances and closes the connection
*/
async shutdown() {
this._shuttingDown = true;
this.connector.setTimeout(30);
try {