Report whether the peer answered from every provider that can tell

An egress spell masked a service on the surviving network going down: with
`peerResponded` set only by the DNS and gRPC providers, every other failure fell
back to the HTTP-status heuristic, which answers "nothing replied" for anything
that is not speaking HTTP. A refused connection on the local network was read as
the instance's own loss of egress, so the check was recorded as degraded and the
monitor stayed up.

`peerAnsweredFromError` holds the errno codes that require the target's own stack
to have replied — a refusal, a reset, a TLS handshake that got as far as a
certificate. Http, Port, WebSocket and Docker use it; Http also treats any status
code as an answer, and Docker any status from the daemon. Hardware and PageSpeed
go through Http and inherit it.

`true` is the claim that costs something, since it blames the target, so it is
only set on evidence. Two cases stay conservative and are commented as such: a
ping that draws no echo reply, and a gamedig query that fails, neither of which
can be told apart from the instance losing its egress. Nor can a proxied HTTP
request that fails with no response, where the refusal may be the proxy's.

🔧
This commit is contained in:
Craig Lawson committed 2026-09-19 14:43:11 +01:00
1 parent 13799af721
commit b55560e2f1
13 files changed
+318 -7

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "@jest/globals";
import { timeRequest, isStatusUp } from "../../../../src/service/network/utils.ts";
import { timeRequest, isStatusUp, peerAnsweredFromError } from "../../../../src/service/network/utils.ts";
import { NETWORK_ERROR } from "../../../../src/types/network.ts";
describe("network utils", () => {
@@ -39,6 +39,40 @@ describe("network utils", () => {
});
});
// The egress check blames the instance only when nothing answered, so this helper's `true` must never be a
// guess: every code in it requires the peer's own stack to have replied.
describe("peerAnsweredFromError", () => {
it.each([
["ECONNREFUSED", "the host refused the connection"],
["ECONNRESET", "the host reset an established connection"],
["EPIPE", "the host closed a connection we were writing to"],
["EPROTO", "a protocol error on a connection that was made"],
["CERT_HAS_EXPIRED", "the host presented a certificate"],
["DEPTH_ZERO_SELF_SIGNED_CERT", "the host presented a certificate"],
["SELF_SIGNED_CERT_IN_CHAIN", "the host presented a certificate"],
["UNABLE_TO_GET_ISSUER_CERT_LOCALLY", "the host presented a certificate"],
["UNABLE_TO_VERIFY_LEAF_SIGNATURE", "the host presented a certificate"],
["ERR_TLS_CERT_ALTNAME_INVALID", "the host presented a certificate"],
["CERT_NOT_YET_VALID", "the host presented a certificate"],
["ERR_SSL_WRONG_VERSION_NUMBER", "the host spoke back, badly"],
])("reports the peer as having answered for %s", (code) => {
expect(peerAnsweredFromError(Object.assign(new Error("failed"), { code }))).toBe(true);
});
it.each([["ETIMEDOUT"], ["ENOTFOUND"], ["EAI_AGAIN"], ["EHOSTUNREACH"], ["ENETUNREACH"], ["ECONNABORTED"], ["ENETDOWN"]])(
"reports the peer as silent for %s",
(code) => {
expect(peerAnsweredFromError(Object.assign(new Error("failed"), { code }))).toBe(false);
}
);
it("reports the peer as silent when the error carries no code", () => {
expect(peerAnsweredFromError(new Error("Connection timeout"))).toBe(false);
expect(peerAnsweredFromError(undefined)).toBe(false);
expect(peerAnsweredFromError("not an error")).toBe(false);
});
});
describe("isStatusUp", () => {
it("returns true for 2xx status codes", () => {
expect(isStatusUp(200)).toBe(true);