Refactor connector logic

Refine the state flow used in the connector abstractions to better
handle close while connecting and resuming.  The new logic is to close
the connection immediately in these two states instead of continuing
trying to connect and then giving up some point later.

For the connectors the handshake state is split into connecting
and resuming, new is replaced by closed, and the closing state is turned
into a boolean flag as it's possible that closing is desired while
connecting, connected or resuming.  The connecting state is not present
in the server connector because there's nothing for the server to do in
that connector state.

The timeout logic is completely removed, once a connection attempt is
started for the client it will continue to attempt to connect, attempt
to resume dropped connections and attempt reconnects until either an
authentication error occurs or the client explicitly requests to close
the connection.

A new session timeout is added to control how long to attempt to resume
the connection until it's considered dead and invalidated.  This allows
stale connections to give up earlier, and swallow up less data while in
the dropped but not yet invalidated state.

For the client side sending messages while it's connecting has been
changed to throw an error instead of queuing up the message and
(possibly) sending it after the connection is established.  Code
shouldn't try to send messages without knowing if the connection it's
trying to send them over is active or not.
This commit is contained in:
Hornwitser committed 2021-08-30 20:20:49 +02:00
1 parent 3e617a81cf
commit de48d807f1
26 files changed
+2042 -821

No files matched your search

+7 -5
View File
@@ -46,7 +46,7 @@ describe("Integration of Clusterio", function() {
connectorA.token = controlToken;
let controlA = new TestControl(connectorA);
await connectorA.connect();
connectorA._state = "closing";
connectorA._closing = true;
connectorA.stopHeartbeat();
connectorA.on("error", () => {});
@@ -54,10 +54,12 @@ describe("Integration of Clusterio", function() {
connectorB.token = controlToken;
let controlB = new TestControl(connectorB);
connectorB._sessionToken = connectorA._sessionToken;
connectorB._state = "handshake";
await connectorB._doConnect();
connectorB._sessionTimeout = connectorA._sessionTimeout;
connectorB._startedResuming = Date.now();
connectorB._state = "resuming";
connectorB._doConnect();
await events.once(connectorB, "resume");
await connectorB.close();
await connectorB.close(1000, "");
});
});
});
@@ -84,7 +86,7 @@ describe("Integration of Clusterio", function() {
let slaveProcess;
try {
slaveProcess = await spawn(
"alt-slave:", `node ../../packages/slave run --config ${config}`, /SOCKET \| registering slave/
"alt-slave:", `node ../../packages/slave run --config ${config}`, /Started slave/
);
} finally {
if (slaveProcess) {
+2 -2
View File
@@ -169,7 +169,7 @@ before(async function() {
await exec("node ../../packages/master config set master.http_port 8880");
await exec("node ../../packages/master config set master.https_port 4443");
await exec("node ../../packages/master config set master.heartbeat_interval 0.25");
await exec("node ../../packages/master config set master.connector_shutdown_timeout 2");
await exec("node ../../packages/master config set master.session_timeout 2");
await exec("node ../../packages/master config set master.tls_certificate ../../test/file/tls/cert.pem");
await exec("node ../../packages/master config set master.tls_private_key ../../test/file/tls/key.pem");
@@ -189,7 +189,7 @@ before(async function() {
await exec(`node ../../packages/slave config set slave.factorio_directory ${path.join("..", "..", "factorio")}`);
await exec("node ../../packages/slave config set slave.tls_ca ../../test/file/tls/cert.pem");
slaveProcess = await spawn("slave:", "node ../../packages/slave run", /SOCKET \| registering slave/);
slaveProcess = await spawn("slave:", "node ../../packages/slave run", /Started slave/);
let tlsCa = await fs.readFile("test/file/tls/cert.pem");
let controlConnector = new TestControlConnector(url, 2, tlsCa);
+9 -5
View File
@@ -48,7 +48,7 @@ describe("lib/link/connectors", function() {
describe(".sendHandshake()", function() {
it("calls send on the socket", function() {
testConnector._state = "handshake";
testConnector._state = "connecting";
testConnector._socket.sentMessages = [];
testConnector.sendHandshake("test", { test: true });
assert.deepEqual(
@@ -93,7 +93,11 @@ describe("lib/link/connectors", function() {
it("should emit connect on ready", async function() {
let result = events.once(testConnector, "connect");
testConnector._processHandshake(
{ seq: 1, type: "ready", data: { session_token: "x", heartbeat_interval: 10 }}
{ seq: 1, type: "ready", data: {
session_token: "x",
session_timeout: 20,
heartbeat_interval: 10,
}}
);
await result;
clearInterval(testConnector._heartbeatId);
@@ -106,14 +110,14 @@ describe("lib/link/connectors", function() {
assert(testConnector._socket.events.size > 0, "No handlers were attached");
});
it("should throw on message received in invalid state", function() {
testConnector._state = "new";
testConnector._state = "closed";
assert.throws(
() => testConnector._socket.events.get("message")("{}"),
new Error("Received message in unexpected state new")
new Error("Received message in unexpected state closed")
);
});
it("should call _processHandshake on message in handshake state", function() {
testConnector._state = "handshake";
testConnector._state = "connecting";
let called = false;
testConnector._processHandshake = () => { called = true; };
testConnector._socket.events.get("message")("{}");