fix(relay): coordinate endpoint lifecycle with leases
This commit is contained in:
7 files changed
+1725
-33
No files matched your search
+7
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE "relay_environment_lifecycle_leases" (
|
||||
"user_id" varchar(191),
|
||||
"environment_id" varchar(191),
|
||||
"owner_id" varchar(64) NOT NULL,
|
||||
"expires_at" varchar(64) NOT NULL,
|
||||
CONSTRAINT "relay_environment_lifecycle_leases_pkey" PRIMARY KEY("user_id","environment_id")
|
||||
);
|
||||
+1569
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,62 @@
|
||||
import { expect, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Result from "effect/Result";
|
||||
|
||||
import * as RelayDb from "./db.ts";
|
||||
import { relayEnvironmentLifecycleLeases } from "./persistence/schema.ts";
|
||||
|
||||
it.effect("leases lifecycle work without changing its transaction boundary", () => {
|
||||
const calls: Array<string> = [];
|
||||
let ownerId = "";
|
||||
const db = {
|
||||
$client: {
|
||||
withTransaction: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
||||
Effect.sync(() => {
|
||||
calls.push("transaction");
|
||||
}).pipe(Effect.andThen(effect)),
|
||||
},
|
||||
insert: (table: unknown) => {
|
||||
expect(table).toBe(relayEnvironmentLifecycleLeases);
|
||||
return {
|
||||
values: (values: { readonly ownerId: string }) => {
|
||||
ownerId = values.ownerId;
|
||||
calls.push("acquire");
|
||||
return {
|
||||
onConflictDoUpdate: () => ({
|
||||
returning: () => Effect.succeed([{ ownerId }]),
|
||||
}),
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
delete: (table: unknown) => {
|
||||
expect(table).toBe(relayEnvironmentLifecycleLeases);
|
||||
return {
|
||||
where: () =>
|
||||
Effect.sync(() => {
|
||||
calls.push("release");
|
||||
}),
|
||||
};
|
||||
},
|
||||
} as unknown as RelayDb.RelayDb["Service"];
|
||||
const layer = RelayDb.RelayTransactions.layer.pipe(
|
||||
Layer.provide(Layer.succeed(RelayDb.RelayDb, db)),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const transactions = yield* RelayDb.RelayTransactions;
|
||||
const result = yield* Effect.result(
|
||||
transactions.withEnvironmentLock(
|
||||
{ userId: "user-1", environmentId: "environment-1" },
|
||||
Effect.sync(() => {
|
||||
calls.push("work");
|
||||
}).pipe(Effect.andThen(Effect.fail("expected failure" as const))),
|
||||
),
|
||||
);
|
||||
|
||||
expect(Result.isFailure(result)).toBe(true);
|
||||
expect(ownerId).not.toBe("");
|
||||
expect(calls).toEqual(["acquire", "work", "release"]);
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
+74
-29
@@ -6,11 +6,15 @@ import * as Alchemy from "alchemy";
|
||||
import * as RemovalPolicy from "alchemy/RemovalPolicy";
|
||||
import type { EffectPgDatabase } from "drizzle-orm/effect-postgres";
|
||||
import * as Context from "effect/Context";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import type { SqlError } from "effect/unstable/sql/SqlError";
|
||||
import * as Random from "effect/Random";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { and, eq, lte } from "drizzle-orm";
|
||||
|
||||
import { relayDatabaseMode } from "./dbConfig.ts";
|
||||
import { relayEnvironmentLifecycleLeases } from "./persistence/schema.ts";
|
||||
|
||||
export class RelayDb extends Context.Service<
|
||||
RelayDb,
|
||||
@@ -19,38 +23,79 @@ export class RelayDb extends Context.Service<
|
||||
}
|
||||
>()("t3code-relay/db/RelayDb") {}
|
||||
|
||||
export class EnvironmentLifecycleLeasePersistenceError extends Schema.TaggedErrorClass<EnvironmentLifecycleLeasePersistenceError>()(
|
||||
"EnvironmentLifecycleLeasePersistenceError",
|
||||
{
|
||||
userId: Schema.String,
|
||||
environmentId: Schema.String,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
override get message(): string {
|
||||
return `Failed to acquire the lifecycle lease for user '${this.userId}', environment '${this.environmentId}'`;
|
||||
}
|
||||
}
|
||||
|
||||
function withEnvironmentLock<A, E, R>(
|
||||
db: RelayDb["Service"],
|
||||
input: { readonly userId: string; readonly environmentId: string },
|
||||
effect: Effect.Effect<A, E, R>,
|
||||
): Effect.Effect<A, E | SqlError, R> {
|
||||
// A blocking advisory lock lets waiters occupy the whole pool. Each waiter
|
||||
// instead tries inside a short transaction, releasing its connection before
|
||||
// retrying. The winning transaction pins all protected database work to the
|
||||
// lock-owning connection and releases the lock automatically on completion.
|
||||
const attempt = db.$client.withTransaction(
|
||||
Effect.gen(function* () {
|
||||
const rows = yield* db.$client.unsafe<{ readonly locked: boolean }>(
|
||||
"SELECT pg_try_advisory_xact_lock(hashtext($1), hashtext($2)) AS locked",
|
||||
[input.userId, input.environmentId],
|
||||
): Effect.Effect<A, E | EnvironmentLifecycleLeasePersistenceError, R> {
|
||||
return Effect.gen(function* () {
|
||||
const ownerId = `${yield* Random.nextInt}:${yield* Random.nextInt}`;
|
||||
const acquire = (): Effect.Effect<void, EnvironmentLifecycleLeasePersistenceError> =>
|
||||
Effect.suspend(() =>
|
||||
Effect.gen(function* () {
|
||||
const now = yield* DateTime.now;
|
||||
const nowIso = DateTime.formatIso(now);
|
||||
const expiresAt = DateTime.formatIso(DateTime.add(now, { seconds: 30 }));
|
||||
const rows = yield* db
|
||||
.insert(relayEnvironmentLifecycleLeases)
|
||||
.values({ ...input, ownerId, expiresAt })
|
||||
.onConflictDoUpdate({
|
||||
target: [
|
||||
relayEnvironmentLifecycleLeases.userId,
|
||||
relayEnvironmentLifecycleLeases.environmentId,
|
||||
],
|
||||
set: { ownerId, expiresAt },
|
||||
setWhere: lte(relayEnvironmentLifecycleLeases.expiresAt, nowIso),
|
||||
})
|
||||
.returning({ ownerId: relayEnvironmentLifecycleLeases.ownerId })
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(cause) => new EnvironmentLifecycleLeasePersistenceError({ ...input, cause }),
|
||||
),
|
||||
);
|
||||
if (rows[0]?.ownerId !== ownerId) {
|
||||
yield* Effect.sleep("50 millis");
|
||||
return yield* acquire();
|
||||
}
|
||||
}),
|
||||
);
|
||||
if (rows[0]?.locked !== true) {
|
||||
return { acquired: false } as const;
|
||||
}
|
||||
// Preserve provider checkpoints on domain failures by committing their
|
||||
// Result. SQL failures still abort the transaction at the driver level.
|
||||
return { acquired: true, result: yield* Effect.result(effect) } as const;
|
||||
}),
|
||||
);
|
||||
const acquire = (): Effect.Effect<A, E | SqlError, R> =>
|
||||
attempt.pipe(
|
||||
Effect.flatMap((outcome) =>
|
||||
outcome.acquired
|
||||
? Effect.fromResult(outcome.result)
|
||||
: Effect.sleep("50 millis").pipe(Effect.andThen(Effect.suspend(acquire))),
|
||||
),
|
||||
);
|
||||
return Effect.suspend(acquire);
|
||||
const release = db
|
||||
.delete(relayEnvironmentLifecycleLeases)
|
||||
.where(
|
||||
and(
|
||||
eq(relayEnvironmentLifecycleLeases.userId, input.userId),
|
||||
eq(relayEnvironmentLifecycleLeases.environmentId, input.environmentId),
|
||||
eq(relayEnvironmentLifecycleLeases.ownerId, ownerId),
|
||||
),
|
||||
)
|
||||
.pipe(
|
||||
Effect.catch((error) =>
|
||||
Effect.logWarning("failed to release relay environment lifecycle lease", {
|
||||
...input,
|
||||
ownerId,
|
||||
error,
|
||||
}),
|
||||
),
|
||||
);
|
||||
// Public relay requests are capped at nine seconds. A 30-second lease keeps
|
||||
// a live request fenced while guaranteeing crash recovery without manual
|
||||
// connection cleanup or provider I/O inside a transaction.
|
||||
yield* acquire();
|
||||
return yield* effect.pipe(Effect.ensuring(release));
|
||||
});
|
||||
}
|
||||
|
||||
export class RelayTransactions extends Context.Service<
|
||||
@@ -60,7 +105,7 @@ export class RelayTransactions extends Context.Service<
|
||||
readonly withEnvironmentLock: <A, E, R>(
|
||||
input: { readonly userId: string; readonly environmentId: string },
|
||||
effect: Effect.Effect<A, E, R>,
|
||||
) => Effect.Effect<A, E | SqlError, R>;
|
||||
) => Effect.Effect<A, E | EnvironmentLifecycleLeasePersistenceError, R>;
|
||||
}
|
||||
>()("t3code-relay/db/RelayTransactions") {
|
||||
static readonly layer = Layer.effect(
|
||||
|
||||
@@ -14,7 +14,6 @@ import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Schema from "effect/Schema";
|
||||
import type { SqlError } from "effect/unstable/sql/SqlError";
|
||||
|
||||
import * as DpopProofs from "../auth/DpopProofs.ts";
|
||||
import * as RelayTokens from "../auth/RelayTokens.ts";
|
||||
@@ -70,7 +69,7 @@ export type EnvironmentLinkError =
|
||||
| DpopProofs.DpopProofReplayPersistenceError
|
||||
| EnvironmentLinks.EnvironmentLinkUpsertPersistenceError
|
||||
| EnvironmentCredentials.EnvironmentCredentialCreatePersistenceError
|
||||
| SqlError
|
||||
| RelayDb.EnvironmentLifecycleLeasePersistenceError
|
||||
| ManagedEndpointProvider.ManagedEndpointProviderError;
|
||||
|
||||
export class EnvironmentLinker extends Context.Service<
|
||||
|
||||
@@ -19,7 +19,6 @@ import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
|
||||
import * as HttpTraceContext from "effect/unstable/http/HttpTraceContext";
|
||||
import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
|
||||
import * as HttpApiError from "effect/unstable/httpapi/HttpApiError";
|
||||
import * as SqlError from "effect/unstable/sql/SqlError";
|
||||
import { encodeOAuthScope } from "@t3tools/shared/oauthScope";
|
||||
import { httpHeaderRedactionLayer } from "@t3tools/shared/httpObservability";
|
||||
|
||||
@@ -1097,7 +1096,7 @@ const RelayCommonPersistenceError = Schema.Union([
|
||||
AgentActivityRows.AgentActivityRowListPersistenceError,
|
||||
LiveActivities.LiveActivityDeliveryMarkPersistenceError,
|
||||
DeliveryAttempts.DeliveryAttemptRecordPersistenceError,
|
||||
SqlError.SqlError,
|
||||
RelayDb.EnvironmentLifecycleLeasePersistenceError,
|
||||
]);
|
||||
type RelayCommonPersistenceError = typeof RelayCommonPersistenceError.Type;
|
||||
const isRelayCommonPersistenceError = Schema.is(RelayCommonPersistenceError);
|
||||
|
||||
@@ -83,6 +83,17 @@ export const relayEnvironmentLinks = pgTable(
|
||||
],
|
||||
);
|
||||
|
||||
export const relayEnvironmentLifecycleLeases = pgTable(
|
||||
"relay_environment_lifecycle_leases",
|
||||
{
|
||||
userId: varchar("user_id", { length: 191 }).notNull(),
|
||||
environmentId: varchar("environment_id", { length: 191 }).notNull(),
|
||||
ownerId: varchar("owner_id", { length: 64 }).notNull(),
|
||||
expiresAt: varchar("expires_at", { length: 64 }).notNull(),
|
||||
},
|
||||
(table) => [primaryKey({ columns: [table.userId, table.environmentId] })],
|
||||
);
|
||||
|
||||
export const relayManagedEndpointAllocations = pgTable(
|
||||
"relay_managed_endpoint_allocations",
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user