The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as spanner from "@distilled.cloud/gcp/spanner_v1"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Semaphore from "effect/Semaphore"; |
| 5 | import * as GCPBindings from "../GCPBindings.ts"; |
| 6 | import { grantFor } from "../HttpBinding.ts"; |
| 7 | import type { Database } from "./Database.ts"; |
| 8 | import { ExecuteSql, type ExecuteSqlRequest } from "./ExecuteSql.ts"; |
| 9 | |
| 10 | /** |
| 11 | * HTTP implementation of {@link ExecuteSql}. |
| 12 | * |
| 13 | * Reuses one multiplexed session per database for the life of the runtime |
| 14 | * instance (multiplexed sessions serve concurrent requests and are never |
| 15 | * deleted by the client). A session the server has dropped |
| 16 | * (`SessionNotFound`) is replaced once and the statement retried. |
| 17 | * |
| 18 | * @layer |
| 19 | * @provides GCP.Spanner.ExecuteSql |
| 20 | */ |
| 21 | export const ExecuteSqlHttp = Layer.effect( |
| 22 | ExecuteSql, |
| 23 | Effect.gen(function* () { |
| 24 | const bind = yield* GCPBindings.Bind; |
| 25 | const createSession = yield* GCPBindings.withCredentials( |
| 26 | spanner.createProjectsInstancesDatabasesSessions, |
| 27 | ); |
| 28 | const executeSql = yield* GCPBindings.withCredentials( |
| 29 | spanner.executeSqlProjectsInstancesDatabasesSessions, |
| 30 | ); |
| 31 | // Session names by database name; plain values, safe to keep per instance. |
| 32 | const sessions = new Map<string, string>(); |
| 33 | const lock = yield* Semaphore.make(1); |
| 34 | |
| 35 | const sessionFor = (database: string, stale?: string) => |
| 36 | lock.withPermits(1)( |
| 37 | Effect.gen(function* () { |
| 38 | const cached = sessions.get(database); |
| 39 | if (cached !== undefined && cached !== stale) return cached; |
| 40 | const session = yield* createSession({ |
| 41 | database, |
| 42 | body: { session: { multiplexed: true } }, |
| 43 | }); |
| 44 | const name = session.name ?? ""; |
| 45 | sessions.set(database, name); |
| 46 | return name; |
| 47 | }), |
| 48 | ); |
| 49 | |
| 50 | return Effect.fn(function* (database: Database) { |
| 51 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 52 | yield* bind`GCP.Spanner.ExecuteSql(${database})`({ |
| 53 | iam: [ |
| 54 | grantFor({ role: "roles/spanner.databaseUser", on: "spanner.database" }, database.name), |
| 55 | ], |
| 56 | }); |
| 57 | } |
| 58 | const name = yield* database.name; |
| 59 | return Effect.fn(`GCP.Spanner.ExecuteSql(${database.LogicalId})`)(function* ( |
| 60 | request: ExecuteSqlRequest, |
| 61 | ) { |
| 62 | const databaseName = yield* name; |
| 63 | const session = yield* sessionFor(databaseName); |
| 64 | return yield* executeSql({ session, body: request }).pipe( |
| 65 | Effect.catchTag("SessionNotFound", () => |
| 66 | Effect.gen(function* () { |
| 67 | const fresh = yield* sessionFor(databaseName, session); |
| 68 | return yield* executeSql({ session: fresh, body: request }); |
| 69 | }), |
| 70 | ), |
| 71 | ); |
| 72 | }); |
| 73 | }); |
| 74 | }), |
| 75 | ); |