refactor: extract host runtime controller for connection management

This commit is contained in:
Mohamed Boudra
2026-02-19 21:19:51 +07:00
parent bff1a5accc
commit f8e3204572
9 changed files with 1077 additions and 215 deletions
@@ -25,7 +25,7 @@ function probes(
}
describe("selectBestConnection", () => {
it("picks the available connection with lowest latency regardless of transport type", () => {
it("picks the available connection with lowest latency regardless of transport", () => {
const candidates: ConnectionCandidate[] = [
{ connectionId: "direct:a", connection: makeDirect("direct:a", "a:6767") },
{
@@ -36,7 +36,6 @@ describe("selectBestConnection", () => {
const selected = selectBestConnection({
candidates,
preferredConnectionId: "direct:a",
probeByConnectionId: probes({
"direct:a": { status: "available", latencyMs: 84 },
"relay:b": { status: "available", latencyMs: 34 },
@@ -46,6 +45,28 @@ describe("selectBestConnection", () => {
expect(selected).toBe("relay:b");
});
it("picks the lowest-latency connection among mixed transport candidates", () => {
const candidates: ConnectionCandidate[] = [
{ connectionId: "direct:a", connection: makeDirect("direct:a", "a:6767") },
{ connectionId: "direct:c", connection: makeDirect("direct:c", "c:6767") },
{
connectionId: "relay:b",
connection: makeRelay("relay:b", "relay.example:443"),
},
];
const selected = selectBestConnection({
candidates,
probeByConnectionId: probes({
"direct:a": { status: "available", latencyMs: 84 },
"direct:c": { status: "available", latencyMs: 41 },
"relay:b": { status: "available", latencyMs: 12 },
}),
});
expect(selected).toBe("relay:b");
});
it("ignores unavailable and pending probes", () => {
const candidates: ConnectionCandidate[] = [
{ connectionId: "direct:a", connection: makeDirect("direct:a", "a:6767") },
@@ -58,7 +79,6 @@ describe("selectBestConnection", () => {
const selected = selectBestConnection({
candidates,
preferredConnectionId: "direct:a",
probeByConnectionId: probes({
"direct:a": { status: "pending", latencyMs: null },
"relay:b": { status: "unavailable", latencyMs: null },
@@ -69,7 +89,7 @@ describe("selectBestConnection", () => {
expect(selected).toBe("direct:c");
});
it("falls back to preferred connection when no candidates are available", () => {
it("returns null when no candidates are available", () => {
const candidates: ConnectionCandidate[] = [
{ connectionId: "direct:a", connection: makeDirect("direct:a", "a:6767") },
{
@@ -80,17 +100,16 @@ describe("selectBestConnection", () => {
const selected = selectBestConnection({
candidates,
preferredConnectionId: "relay:b",
probeByConnectionId: probes({
"direct:a": { status: "pending", latencyMs: null },
"relay:b": { status: "unavailable", latencyMs: null },
}),
});
expect(selected).toBe("relay:b");
expect(selected).toBeNull();
});
it("falls back to first candidate when preferred is missing and none are available", () => {
it("returns null when no probes are available even if candidates exist", () => {
const candidates: ConnectionCandidate[] = [
{ connectionId: "direct:a", connection: makeDirect("direct:a", "a:6767") },
{
@@ -101,13 +120,12 @@ describe("selectBestConnection", () => {
const selected = selectBestConnection({
candidates,
preferredConnectionId: "missing",
probeByConnectionId: probes({
"direct:a": { status: "unavailable", latencyMs: null },
"relay:b": { status: "pending", latencyMs: null },
}),
});
expect(selected).toBe("direct:a");
expect(selected).toBeNull();
});
});
+9 -15
View File
@@ -12,14 +12,13 @@ export type ConnectionProbeState =
export type SelectBestConnectionInput = {
candidates: ConnectionCandidate[];
preferredConnectionId: string | null;
probeByConnectionId: Map<string, ConnectionProbeState>;
};
export function selectBestConnection(
input: SelectBestConnectionInput
): string | null {
const { candidates, preferredConnectionId, probeByConnectionId } = input;
const { candidates, probeByConnectionId } = input;
if (candidates.length === 0) {
return null;
}
@@ -29,8 +28,9 @@ export function selectBestConnection(
.filter(({ candidate }) => {
const probe = probeByConnectionId.get(candidate.connectionId);
return probe?.status === "available";
})
.sort((left, right) => {
});
const byLatency = (left: { candidate: ConnectionCandidate; index: number }, right: { candidate: ConnectionCandidate; index: number }) => {
const leftProbe = probeByConnectionId.get(left.candidate.connectionId);
const rightProbe = probeByConnectionId.get(right.candidate.connectionId);
@@ -48,18 +48,12 @@ export function selectBestConnection(
}
return leftLatency - rightLatency;
});
};
if (available.length > 0) {
return available[0]!.candidate.connectionId;
if (available.length === 0) {
return null;
}
const preferred = candidates.find(
(candidate) => candidate.connectionId === preferredConnectionId
);
if (preferred) {
return preferred.connectionId;
}
return candidates[0]!.connectionId;
const sorted = available.sort(byLatency);
return sorted[0]!.candidate.connectionId;
}