Skip to content

Commit 7395c54

Browse files
fix(relay): count legacy tunnels as soon as they pass an age bucket
Flooring the age made a tunnel down for seven days and an hour count as seven days old, so it missed the over-seven bucket; the same held at 30 and 90. The census also rejected a whole page when Cloudflare omitted an optional field. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 3e660f4 commit 7395c54

3 files changed

Lines changed: 24 additions & 18 deletions

File tree

‎infra/relay/scripts/tunnel-census.ts‎

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,32 @@ import * as Console from "effect/Console";
1313
import * as Effect from "effect/Effect";
1414
import * as Redacted from "effect/Redacted";
1515
import * as Schema from "effect/Schema";
16-
import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient";
17-
import * as HttpClient from "effect/unstable/http/HttpClient";
18-
import * as HttpClientRequest from "effect/unstable/http/HttpClientRequest";
19-
import * as HttpClientResponse from "effect/unstable/http/HttpClientResponse";
16+
import * as FetchHttpClient from "effect/http/FetchHttpClient";
17+
import * as HttpClient from "effect/http/HttpClient";
18+
import * as HttpClientRequest from "effect/http/HttpClientRequest";
19+
import * as HttpClientResponse from "effect/http/HttpClientResponse";
2020

2121
const PREFIX = "t3coderelay-managedendpoint-";
2222
const PAGE_SIZE = 1_000;
2323
const DAY_MS = 86_400_000;
2424

25+
// Cloudflare documents every field here as optional, so a tunnel missing one
26+
// must not fail the whole page.
2527
const Tunnel = Schema.Struct({
26-
name: Schema.String,
27-
status: Schema.String,
28-
created_at: Schema.String,
29-
conns_inactive_at: Schema.NullOr(Schema.String),
28+
name: Schema.optional(Schema.NullOr(Schema.String)),
29+
status: Schema.optional(Schema.NullOr(Schema.String)),
30+
created_at: Schema.optional(Schema.NullOr(Schema.String)),
31+
conns_inactive_at: Schema.optional(Schema.NullOr(Schema.String)),
3032
});
3133
type Tunnel = typeof Tunnel.Type;
3234
const TunnelPage = Schema.Struct({ result: Schema.Array(Tunnel) });
3335

3436
// "t3coderelay-managedendpoint-<stage>-<16 hex>"; stages may contain hyphens.
35-
const stageOf = (name: string) => name.slice(PREFIX.length).replace(/-[a-f0-9]{16}$/u, "");
37+
const stageOf = (name: string | null | undefined) =>
38+
name ? name.slice(PREFIX.length).replace(/-[a-f0-9]{16}$/u, "") : "unknown";
3639

3740
const ageBucket = (tunnel: Tunnel, now: number) => {
38-
const since = Date.parse(tunnel.conns_inactive_at ?? tunnel.created_at);
41+
const since = Date.parse(tunnel.conns_inactive_at ?? tunnel.created_at ?? "");
3942
if (Number.isNaN(since)) return "unknown";
4043
const days = (now - since) / DAY_MS;
4144
if (days > 90) return ">90d";
@@ -74,7 +77,7 @@ const main = Effect.gen(function* () {
7477
total += 1;
7578
const idle =
7679
tunnel.status === "down" || tunnel.status === "inactive" ? ageBucket(tunnel, now) : "-";
77-
const key = `${stageOf(tunnel.name)}\t${tunnel.status}\t${idle}`;
80+
const key = `${stageOf(tunnel.name)}\t${tunnel.status ?? "unknown"}\t${idle}`;
7881
counts.set(key, (counts.get(key) ?? 0) + 1);
7982
}
8083
if (tunnels.length < PAGE_SIZE) break;

‎infra/relay/src/environments/ManagedEndpointReaper.test.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,10 @@ describe("ManagedEndpointReaper", () => {
440440
const down = (id: string, suffix: string, timestamp: string) =>
441441
tunnel({ id, suffix, status: "down", timestamp });
442442
const tunnels = [
443-
// Legacy owners, down for 1, 10, 40, and 100 days.
443+
// Legacy owners, down for 1, 10, 40, and 100 days, plus one just past
444+
// seven days, which must already count as over seven.
444445
down("legacy-1d", "1111111111111111", "2026-08-24T11:00:00.000Z"),
446+
down("legacy-7d1h", "7777777777777777", "2026-08-18T11:00:00.000Z"),
445447
down("legacy-10d", "2222222222222222", "2026-08-15T11:00:00.000Z"),
446448
down("legacy-40d", "3333333333333333", "2026-07-16T11:00:00.000Z"),
447449
down("legacy-100d", "4444444444444444", "2026-05-17T11:00:00.000Z"),
@@ -454,6 +456,7 @@ describe("ManagedEndpointReaper", () => {
454456
tunnels,
455457
allocations: [
456458
allocation({ tunnelId: "legacy-1d", recoveryEnabled: false }),
459+
allocation({ tunnelId: "legacy-7d1h", recoveryEnabled: false }),
457460
allocation({ tunnelId: "legacy-10d", recoveryEnabled: false }),
458461
allocation({ tunnelId: "legacy-40d", recoveryEnabled: false }),
459462
allocation({ tunnelId: "legacy-100d", recoveryEnabled: false }),
@@ -473,14 +476,14 @@ describe("ManagedEndpointReaper", () => {
473476
yield* TestClock.setTime(NOW_MILLIS);
474477
const reaper = yield* ManagedEndpointReaper.ManagedEndpointReaper;
475478
expect(yield* reaper.sweep).toMatchObject({
476-
scanned: 6,
477-
skippedLegacy: 4,
478-
legacyOver7Days: 3,
479+
scanned: 7,
480+
skippedLegacy: 5,
481+
legacyOver7Days: 4,
479482
legacyOver30Days: 2,
480483
legacyOver90Days: 1,
481484
skippedReplaced: 1,
482485
skippedUnrecorded: 1,
483-
totalDown: 6,
486+
totalDown: 7,
484487
totalInactive: 0,
485488
wouldDelete: 0,
486489
});

‎infra/relay/src/environments/ManagedEndpointReaper.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function isExpiredManagedTunnel(input: {
8484
return Option.isSome(timestamp) && timestamp.value.epochMilliseconds <= cutoff.epochMilliseconds;
8585
}
8686

87-
/** Whole days since the tunnel went down, or was created if it never connected. */
87+
/** Days (fractional) since the tunnel went down, or was created if it never connected. */
8888
function inactiveDaysAt(
8989
tunnel: ManagedEndpointProvider.ManagedEndpointTunnel,
9090
status: "down" | "inactive",
@@ -94,7 +94,7 @@ function inactiveDaysAt(
9494
if (typeof since !== "string") return null;
9595
const timestamp = DateTime.make(since);
9696
if (Option.isNone(timestamp)) return null;
97-
return Math.floor((now.epochMilliseconds - timestamp.value.epochMilliseconds) / 86_400_000);
97+
return (now.epochMilliseconds - timestamp.value.epochMilliseconds) / 86_400_000;
9898
}
9999

100100
function isRateLimited(cause: unknown): boolean {

0 commit comments

Comments
 (0)