Skip to content

Commit 2bc2abb

Browse files
fix(relay): re-reserved released and revoked allocations count against the limit
Released rows and rows with a revoked link stayed excluded from the count until provisioning finished outside the lock, so several of them re-provisioned at once could all pass. A row now counts while it was touched within the pending window (reserve bumps it under the lock) or while it holds a live tunnel for an active link. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 147a27c commit 2bc2abb

2 files changed

Lines changed: 50 additions & 42 deletions

File tree

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,34 @@ describe("ManagedTunnelLimits", () => {
9696
);
9797
});
9898

99-
it.effect("stops counting tunnel-less reservations after the pending window", () => {
100-
const countConditions: Array<unknown> = [];
101-
const now = DateTime.makeUnsafe("2026-10-27T12:00:00.000Z");
99+
it.effect(
100+
"counts reservations only within the pending window unless they hold a live tunnel",
101+
() => {
102+
const countConditions: Array<unknown> = [];
103+
const now = DateTime.makeUnsafe("2026-10-27T12:00:00.000Z");
102104

103-
return Effect.gen(function* () {
104-
yield* TestClock.setTime(now.epochMilliseconds);
105-
const limits = yield* ManagedTunnelLimits.ManagedTunnelLimits;
106-
yield* limits.withCapacity({ userId: "user-1", environmentId: "environment-1" }, Effect.void);
105+
return Effect.gen(function* () {
106+
yield* TestClock.setTime(now.epochMilliseconds);
107+
const limits = yield* ManagedTunnelLimits.ManagedTunnelLimits;
108+
yield* limits.withCapacity(
109+
{ userId: "user-1", environmentId: "environment-1" },
110+
Effect.void,
111+
);
107112

108-
const query = new PgDialect().sqlToQuery(countConditions[0] as never);
109-
expect(query.sql).toContain(
110-
'("relay_managed_endpoint_allocations"."tunnel_id" is not null)) or ("relay_managed_endpoint_allocations"."updated_at" > $3)',
111-
);
112-
expect(query.params[2]).toBe("2026-10-27T11:45:00.000Z");
113-
expect(query.sql).toContain("not exists");
114-
expect(query.sql).toContain('"relay_environment_links"."revoked_at" is not null');
115-
}).pipe(
116-
Effect.provide(
117-
ManagedTunnelLimits.layer.pipe(
118-
Layer.provide(Layer.succeed(RelayDb.RelayDb, makeFakeDb([], countConditions))),
113+
const query = new PgDialect().sqlToQuery(countConditions[0] as never);
114+
const sqlText = query.sql.replaceAll(/[()]/g, "");
115+
expect(sqlText).toContain(
116+
'"relay_managed_endpoint_allocations"."updated_at" > $3 or "relay_managed_endpoint_allocations"."tunnel_id" is not null and "relay_managed_endpoint_allocations"."tunnel_released_at" is null and not exists',
117+
);
118+
expect(query.params[2]).toBe("2026-10-27T11:45:00.000Z");
119+
expect(sqlText).toContain('"relay_environment_links"."revoked_at" is not null');
120+
}).pipe(
121+
Effect.provide(
122+
ManagedTunnelLimits.layer.pipe(
123+
Layer.provide(Layer.succeed(RelayDb.RelayDb, makeFakeDb([], countConditions))),
124+
),
119125
),
120-
),
121-
);
122-
});
126+
);
127+
},
128+
);
123129
});

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "../persistence/schema.ts";
1616

1717
export const DEFAULT_MANAGED_TUNNEL_LIMIT = 3;
18-
// Reserve-to-tunnel takes two Cloudflare calls capped at 8 seconds each; a tunnel-less row older than this was abandoned.
18+
// Reserve-to-tunnel takes two Cloudflare calls capped at 8 seconds each; a reservation older than this was abandoned.
1919
export const MANAGED_TUNNEL_PENDING_WINDOW_MINUTES = 15;
2020

2121
export class ManagedTunnelLimitPersistenceError extends Schema.TaggedError<ManagedTunnelLimitPersistenceError>()(
@@ -90,37 +90,39 @@ export const make = Effect.gen(function* () {
9090
}),
9191
);
9292
// Count reservations, not links: links are written only after provisioning creates Cloudflare resources.
93-
// Exclude this environment so relinking remains idempotent at the account limit, abandoned
94-
// reservations that never got a tunnel, and allocations whose link was revoked or switched off.
93+
// A row counts while recently reserved (provisioning may be in flight) or while it holds a live tunnel
94+
// for a link that is neither revoked nor publish-only. Exclude this environment so relinking stays idempotent.
9595
const counted = yield* db
9696
.select({ activeTunnels: count() })
9797
.from(relayManagedEndpointAllocations)
9898
.where(
9999
and(
100100
eq(relayManagedEndpointAllocations.userId, input.userId),
101101
ne(relayManagedEndpointAllocations.environmentId, input.environmentId),
102-
isNull(relayManagedEndpointAllocations.tunnelReleasedAt),
103102
or(
104-
isNotNull(relayManagedEndpointAllocations.tunnelId),
105103
gt(relayManagedEndpointAllocations.updatedAt, pendingCutoff),
106-
),
107-
notExists(
108-
new QueryBuilder()
109-
.select({ userId: relayEnvironmentLinks.userId })
110-
.from(relayEnvironmentLinks)
111-
.where(
112-
and(
113-
eq(relayEnvironmentLinks.userId, relayManagedEndpointAllocations.userId),
114-
eq(
115-
relayEnvironmentLinks.environmentId,
116-
relayManagedEndpointAllocations.environmentId,
117-
),
118-
or(
119-
isNotNull(relayEnvironmentLinks.revokedAt),
120-
eq(relayEnvironmentLinks.managedTunnelsEnabled, false),
104+
and(
105+
isNotNull(relayManagedEndpointAllocations.tunnelId),
106+
isNull(relayManagedEndpointAllocations.tunnelReleasedAt),
107+
notExists(
108+
new QueryBuilder()
109+
.select({ userId: relayEnvironmentLinks.userId })
110+
.from(relayEnvironmentLinks)
111+
.where(
112+
and(
113+
eq(relayEnvironmentLinks.userId, relayManagedEndpointAllocations.userId),
114+
eq(
115+
relayEnvironmentLinks.environmentId,
116+
relayManagedEndpointAllocations.environmentId,
117+
),
118+
or(
119+
isNotNull(relayEnvironmentLinks.revokedAt),
120+
eq(relayEnvironmentLinks.managedTunnelsEnabled, false),
121+
),
122+
),
121123
),
122-
),
123124
),
125+
),
124126
),
125127
),
126128
)

0 commit comments

Comments
 (0)