Skip to content

Commit 593fc2a

Browse files
fix(relay): released or withdrawn tunnels stop holding a pending quota slot
Releasing a tunnel or revoking a link bumped the allocation into the 15-minute pending window, so a user at their limit who freed a slot was refused a new environment. The pending branch now skips rows whose last write was the release, and links withdrawn after the last reservation. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 487841a commit 593fc2a

2 files changed

Lines changed: 69 additions & 29 deletions

File tree

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Context from "effect/Context";
33
import * as Effect from "effect/Effect";
44
import * as Layer from "effect/Layer";
55
import * as Semaphore from "effect/Semaphore";
6+
import { PgDialect } from "drizzle-orm/pg-core";
67

78
import * as RelayDb from "../db.ts";
89
import { relayManagedEndpointAllocations } from "../persistence/schema.ts";
@@ -13,7 +14,7 @@ class FakeTransaction extends Context.Service<FakeTransaction, { holdsLock: bool
1314
) {}
1415

1516
// Models Postgres just enough: the advisory lock only works inside a transaction and is held until it ends.
16-
function makeFakeDb(reserved: ReadonlyArray<unknown>) {
17+
function makeFakeDb(reserved: ReadonlyArray<unknown>, countConditions: Array<unknown> = []) {
1718
const userLock = Semaphore.makeUnsafe(1);
1819
return {
1920
$client: {
@@ -39,10 +40,13 @@ function makeFakeDb(reserved: ReadonlyArray<unknown>) {
3940
}),
4041
select: () => ({
4142
from: (table: unknown) => ({
42-
where: () =>
43-
table === relayManagedEndpointAllocations
44-
? Effect.yieldNow.pipe(Effect.as([{ activeTunnels: reserved.length }]))
45-
: { limit: () => Effect.succeed([]) },
43+
where: (condition: unknown) => {
44+
if (table !== relayManagedEndpointAllocations) {
45+
return { limit: () => Effect.succeed([]) };
46+
}
47+
countConditions.push(condition);
48+
return Effect.yieldNow.pipe(Effect.as([{ activeTunnels: reserved.length }]));
49+
},
4650
}),
4751
}),
4852
} as unknown as RelayDb.RelayDb["Service"];
@@ -82,4 +86,30 @@ describe("ManagedTunnelLimits", () => {
8286
),
8387
);
8488
});
89+
90+
it.effect("pending reservations stop counting once released or withdrawn", () => {
91+
const countConditions: Array<unknown> = [];
92+
93+
return Effect.gen(function* () {
94+
const limits = yield* ManagedTunnelLimits.ManagedTunnelLimits;
95+
yield* limits.withCapacity({ userId: "user-1", environmentId: "environment-1" }, Effect.void);
96+
97+
expect(countConditions).toHaveLength(1);
98+
const query = new PgDialect().sqlToQuery(countConditions[0] as never);
99+
// claimRelease writes updatedAt = tunnelReleasedAt; a later reserve moves updatedAt past it.
100+
expect(query.sql).toContain(
101+
'"relay_managed_endpoint_allocations"."updated_at" > "relay_managed_endpoint_allocations"."tunnel_released_at"',
102+
);
103+
// A link revoked or made publish-only after the last reservation no longer holds a slot.
104+
expect(query.sql).toContain(
105+
'"relay_environment_links"."updated_at" >= "relay_managed_endpoint_allocations"."updated_at"',
106+
);
107+
}).pipe(
108+
Effect.provide(
109+
ManagedTunnelLimits.layer.pipe(
110+
Layer.provide(Layer.succeed(RelayDb.RelayDb, makeFakeDb([], countConditions))),
111+
),
112+
),
113+
);
114+
});
85115
});

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

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { and, count, eq, gt, isNotNull, isNull, ne, notExists, or, sql } from "drizzle-orm";
1+
import { and, count, eq, exists, gt, gte, isNotNull, isNull, ne, not, or, sql } from "drizzle-orm";
22
import { QueryBuilder } from "drizzle-orm/pg-core";
33
import * as Context from "effect/Context";
44
import * as DateTime from "effect/DateTime";
@@ -105,36 +105,46 @@ const make = Effect.gen(function* () {
105105
);
106106
// Counts allocations, not links: a link is written only after its tunnel exists.
107107
// Excludes this environment so relinking at the limit stays idempotent.
108+
const allocation = relayManagedEndpointAllocations;
109+
const withdrawnLink = (since?: typeof allocation.updatedAt) =>
110+
exists(
111+
new QueryBuilder()
112+
.select({ userId: relayEnvironmentLinks.userId })
113+
.from(relayEnvironmentLinks)
114+
.where(
115+
and(
116+
eq(relayEnvironmentLinks.userId, allocation.userId),
117+
eq(relayEnvironmentLinks.environmentId, allocation.environmentId),
118+
or(
119+
isNotNull(relayEnvironmentLinks.revokedAt),
120+
eq(relayEnvironmentLinks.managedTunnelsEnabled, false),
121+
),
122+
since === undefined ? undefined : gte(relayEnvironmentLinks.updatedAt, since),
123+
),
124+
),
125+
);
108126
const counted = yield* db
109127
.select({ activeTunnels: count() })
110-
.from(relayManagedEndpointAllocations)
128+
.from(allocation)
111129
.where(
112130
and(
113-
eq(relayManagedEndpointAllocations.userId, input.userId),
114-
ne(relayManagedEndpointAllocations.environmentId, input.environmentId),
131+
eq(allocation.userId, input.userId),
132+
ne(allocation.environmentId, input.environmentId),
115133
or(
116-
gt(relayManagedEndpointAllocations.updatedAt, pendingCutoff),
134+
// Reserved but not yet holding a tunnel. Release writes updatedAt = tunnelReleasedAt and
135+
// a relink reserves before rewriting its link, so only a newer reservation counts here.
117136
and(
118-
isNotNull(relayManagedEndpointAllocations.tunnelId),
119-
isNull(relayManagedEndpointAllocations.tunnelReleasedAt),
120-
notExists(
121-
new QueryBuilder()
122-
.select({ userId: relayEnvironmentLinks.userId })
123-
.from(relayEnvironmentLinks)
124-
.where(
125-
and(
126-
eq(relayEnvironmentLinks.userId, relayManagedEndpointAllocations.userId),
127-
eq(
128-
relayEnvironmentLinks.environmentId,
129-
relayManagedEndpointAllocations.environmentId,
130-
),
131-
or(
132-
isNotNull(relayEnvironmentLinks.revokedAt),
133-
eq(relayEnvironmentLinks.managedTunnelsEnabled, false),
134-
),
135-
),
136-
),
137+
gt(allocation.updatedAt, pendingCutoff),
138+
or(
139+
isNull(allocation.tunnelReleasedAt),
140+
gt(allocation.updatedAt, allocation.tunnelReleasedAt),
137141
),
142+
not(withdrawnLink(allocation.updatedAt)),
143+
),
144+
and(
145+
isNotNull(allocation.tunnelId),
146+
isNull(allocation.tunnelReleasedAt),
147+
not(withdrawnLink()),
138148
),
139149
),
140150
),

0 commit comments

Comments
 (0)