Merge pull request #571 from Hornwitser/link-bugfixes

Link bugfixes
This commit is contained in:
Cooldude2606 authored and GitHub committed 2024-01-22 21:57:50 +00:00
commit 2733f2e3c5
5 files changed
+72 -11

No files matched your search

+4 -1
View File
@@ -4,7 +4,8 @@
### Features ### Features
- Added support for `color-settings` Mod settings (#568)[https://github.com/clusterio/clusterio/issues/568]. - Added support for `color-settings` Mod settings [#568](https://github.com/clusterio/clusterio/issues/568).
- Added `clusterio_controller_pending_requests` and `clusterio_host_pending_requests` metrics.
### Changes ### Changes
@@ -12,6 +13,8 @@
- Fixed ctl not exporting types used for adding commands to ctl plugins. - Fixed ctl not exporting types used for adding commands to ctl plugins.
- Fixed controller only plugins throwing an error about missing a config field on startup out when added to hosts. - Fixed controller only plugins throwing an error about missing a config field on startup out when added to hosts.
- Fixed installer incorrectly claiming the options --factorio-dir and --no-download-headless conflicts with each other. - Fixed installer incorrectly claiming the options --factorio-dir and --no-download-headless conflicts with each other.
- Fixed a memory leak on every request sent over the WebSocket.
- Fixed assertion error on closing or invalidating a link with messages forwarded from a virtual link.
## Version 2.0.0-alpha.14 ## Version 2.0.0-alpha.14
+18
View File
@@ -37,6 +37,24 @@ import { version } from "./package.json";
// globals // globals
let controller: Controller; let controller: Controller;
void new lib.Gauge(
"clusterio_controller_pending_requests",
"Count of pending link requests currently waiting in memory on the controller.",
{
callback: (gauge) => {
let count = 0;
if (controller) {
for (const host of controller.wsServer.hostConnections.values()) {
count += host.pendingRequestCount;
}
for (const control of controller.wsServer.controlConnections.values()) {
count += control.pendingRequestCount;
}
}
gauge.set(count);
},
}
);
void new lib.Gauge( void new lib.Gauge(
"clusterio_controller_host_mapping", "clusterio_controller_host_mapping",
+26 -5
View File
@@ -26,6 +26,27 @@ import { ConsoleTransport, levels, logger } from "@clusterio/lib";
import Host from "./src/Host"; import Host from "./src/Host";
let host: Host | undefined;
void new lib.Gauge(
"clusterio_host_pending_requests",
"Count of pending link requests currently waiting in memory on the host.",
{
labels: ["host_id"],
callback: (gauge) => {
if (!host) {
return;
}
let count = host.pendingRequestCount;
for (const connection of host.instanceConnections.values()) {
count += connection.pendingRequestCount;
count += connection.instance.pendingRequestCount;
}
gauge.labels(String(host.config.get("host.id"))).set(count);
},
}
);
export class HostConnector extends lib.WebSocketClientConnector { export class HostConnector extends lib.WebSocketClientConnector {
constructor( constructor(
@@ -205,7 +226,7 @@ async function startHost() {
} }
let hostConnector = new HostConnector(hostConfig, tlsCa, pluginInfos); let hostConnector = new HostConnector(hostConfig, tlsCa, pluginInfos);
let host = new Host( host = new Host(
hostConnector, hostConnector,
args.config, args.config,
hostConfig, hostConfig,
@@ -227,7 +248,7 @@ async function startHost() {
secondSigint = true; secondSigint = true;
logger.info("Caught interrupt signal, shutting down"); logger.info("Caught interrupt signal, shutting down");
host.shutdown(); host!.shutdown();
}); });
let secondSigterm = false; let secondSigterm = false;
process.on("SIGTERM", () => { process.on("SIGTERM", () => {
@@ -240,11 +261,11 @@ async function startHost() {
secondSigterm = true; secondSigterm = true;
logger.info("Caught termination signal, shutting down"); logger.info("Caught termination signal, shutting down");
host.shutdown(); host!.shutdown();
}); });
process.on("SIGHUP", () => { process.on("SIGHUP", () => {
logger.info("Terminal closed, shutting down"); logger.info("Terminal closed, shutting down");
host.shutdown(); host!.shutdown();
}); });
try { try {
@@ -255,7 +276,7 @@ async function startHost() {
} }
hostConnector.once("connect", () => { hostConnector.once("connect", () => {
logger.add(new lib.LinkTransport({ link: host })); logger.add(new lib.LinkTransport({ link: host! }));
}); });
await hostConnector.connect(); await hostConnector.connect();
+20 -5
View File
@@ -141,17 +141,30 @@ export class Link {
}); });
} }
/**
* Count of requests currently waiting for a response on this link
*/
get pendingRequestCount() {
return this._pendingRequests.size + this._forwardedRequests.size;
}
_clearPendingRequests(err: Error & { code?: string }) { _clearPendingRequests(err: Error & { code?: string }) {
for (let pending of this._pendingRequests.values()) { for (let pending of this._pendingRequests.values()) {
pending.reject(err); pending.reject(err);
} }
for (let pending of this._forwardedRequests.values()) { for (let pending of this._forwardedRequests.values()) {
assert(pending.origin.connector instanceof WebSocketBaseConnector); // Drop response if the origin is a WebSocket and the session
if (pending.origin.connector.hasSession) { // has expired. The other end of the link will have sent an
pending.origin.connector.sendResponseError( // error response to the request in this case.
new libData.ResponseError(err.message, err.code), pending.src, pending.dst if (
); pending.origin.connector instanceof WebSocketBaseConnector
&& !pending.origin.connector.hasSession
) {
continue;
} }
pending.origin.connector.sendResponseError(
new libData.ResponseError(err.message, err.code), pending.src, pending.dst
);
} }
this._pendingRequests.clear(); this._pendingRequests.clear();
this._forwardedRequests.clear(); this._forwardedRequests.clear();
@@ -421,6 +434,7 @@ export class Link {
throw new libErrors.InvalidMessage(`Received reply from ${message.src} for message sent to ${pending.dst}`); throw new libErrors.InvalidMessage(`Received reply from ${message.src} for message sent to ${pending.dst}`);
} }
this._pendingRequests.delete(message.dst.requestId!);
try { try {
pending.resolve(pending.request.responseFromJSON(message.data)); pending.resolve(pending.request.responseFromJSON(message.data));
} catch (err: any) { } catch (err: any) {
@@ -437,6 +451,7 @@ export class Link {
); );
} }
this._pendingRequests.delete(message.dst.requestId!);
pending.reject(new libErrors.RequestError(message.data.message, message.data.code, message.data.stack)); pending.reject(new libErrors.RequestError(message.data.message, message.data.code, message.data.stack));
} }
+4
View File
@@ -16,6 +16,7 @@ describe("lib/link/link", function() {
describe("class Link", function() { describe("class Link", function() {
let testConnector; let testConnector;
/** @type {lib.Link} */
let testLink; let testLink;
let src = addr({ controlId: 1 }); let src = addr({ controlId: 1 });
let dst = addr("controller"); let dst = addr("controller");
@@ -281,6 +282,7 @@ describe("lib/link/link", function() {
let srcReq = new lib.Address(lib.Address.control, 1, 1); let srcReq = new lib.Address(lib.Address.control, 1, 1);
testConnector.emit("message", new lib.MessageResponse(1, dst, srcReq)); testConnector.emit("message", new lib.MessageResponse(1, dst, srcReq));
await request; await request;
assert.deepEqual(testLink._pendingRequests, new Map());
}); });
it("should send request and reject with error when error response is received", async function() { it("should send request and reject with error when error response is received", async function() {
let message = events.once(testConnector, "send"); let message = events.once(testConnector, "send");
@@ -297,6 +299,7 @@ describe("lib/link/link", function() {
request, request,
{ message: "Error" } { message: "Error" }
); );
assert.deepEqual(testLink._pendingRequests, new Map());
}); });
it("should send request and resolve with data when response is received", async function() { it("should send request and resolve with data when response is received", async function() {
let message = events.once(testConnector, "send"); let message = events.once(testConnector, "send");
@@ -305,6 +308,7 @@ describe("lib/link/link", function() {
let srcReq = new lib.Address(lib.Address.control, 1, 1); let srcReq = new lib.Address(lib.Address.control, 1, 1);
testConnector.emit("message", new lib.MessageResponse(1, dst, srcReq, 44)); testConnector.emit("message", new lib.MessageResponse(1, dst, srcReq, 44));
assert.deepEqual(await request, new NumberRequest.Response(44)); assert.deepEqual(await request, new NumberRequest.Response(44));
assert.deepEqual(testLink._pendingRequests, new Map());
}); });
it("should send event to the other side of the link", async function() { it("should send event to the other side of the link", async function() {
let message = events.once(testConnector, "send"); let message = events.once(testConnector, "send");