diff --git a/apps/api/.env.example b/apps/api/.env.example index bd9dbcc..27698f9 100644 --- a/apps/api/.env.example +++ b/apps/api/.env.example @@ -9,6 +9,7 @@ DATABASE_URL=postgresql://photoreport:CHANGE_ME@pr-db:5432/photoreport # API_DB_POOL_SIZE= # API_INSTANCES= # DB_MAX_CONNECTIONS= +# DB_SUPERUSER_RESERVED_CONNECTIONS= # At least 32 random characters, e.g. `openssl rand -base64 48` JWT_SECRET= ACCESS_TOKEN_TTL_SEC=900 diff --git a/apps/api/src/config.ts b/apps/api/src/config.ts index e0da0cd..37d2daa 100644 --- a/apps/api/src/config.ts +++ b/apps/api/src/config.ts @@ -19,6 +19,8 @@ const Env = z.object({ API_INSTANCES: z.coerce.number().int().positive().default(2), /** Postgres' max_connections, so the worker leaves it enough room for the API and admin/migration connections. */ DB_MAX_CONNECTIONS: z.coerce.number().int().positive().default(100), + /** Postgres' superuser_reserved_connections: never available to the app, whatever max_connections says. */ + DB_SUPERUSER_RESERVED_CONNECTIONS: z.coerce.number().int().nonnegative().default(3), JWT_SECRET: z.string().min(32, "JWT_SECRET must have at least 32 characters"), ACCESS_TOKEN_TTL_SEC: z.coerce.number().int().positive().default(900), REFRESH_TOKEN_TTL_DAYS: z.coerce.number().int().positive().default(30), diff --git a/apps/api/src/lib/capacity.ts b/apps/api/src/lib/capacity.ts index a6bd441..4dc1c1c 100644 --- a/apps/api/src/lib/capacity.ts +++ b/apps/api/src/lib/capacity.ts @@ -134,13 +134,31 @@ export class Pressure { /** * How many Postgres connections the worker's pool may open for `wanted`, leaving room under `max` (Postgres' - * `max_connections`) for the API's own pools (one per PM2 cluster instance), the realtime broadcaster's one - * connection, and headroom for ad hoc connections (migrations, scripts, psql) that aren't sized here. + * `max_connections`) for: the API's own Prisma pool, one per PM2 cluster instance; each of those instances' + * realtime connections (`attachRealtime` in `realtime.ts`: a 2-connection publish pool plus one dedicated + * `LISTEN` client, so 3 per instance — not just the worker's own 1-connection remote broadcaster); Postgres' + * `superuser_reserved_connections` (reserved below `max_connections`, never available to the app no matter + * how much headroom is left — this is what "reserved for roles with the SUPERUSER attribute" means); and + * extra headroom for ad hoc connections (migrations, scripts, psql, admin tools) that aren't sized here. */ export function dbConnectionBudget( wanted: number, - { max = 100, apiInstances = 2, apiPoolSize = 10, headroom = 10 }: { max?: number; apiInstances?: number; apiPoolSize?: number; headroom?: number } = {}, + { + max = 100, + apiInstances = 2, + apiPoolSize = 10, + realtimePerInstance = 3, + superuserReserved = 3, + headroom = 10, + }: { + max?: number; + apiInstances?: number; + apiPoolSize?: number; + realtimePerInstance?: number; + superuserReserved?: number; + headroom?: number; + } = {}, ): number { - const available = max - apiInstances * apiPoolSize - 1 - headroom; + const available = max - superuserReserved - apiInstances * (apiPoolSize + realtimePerInstance) - 1 - headroom; return Math.max(1, Math.min(wanted, available)); } diff --git a/apps/api/src/worker.ts b/apps/api/src/worker.ts index abe9043..0e61d45 100644 --- a/apps/api/src/worker.ts +++ b/apps/api/src/worker.ts @@ -64,11 +64,17 @@ const capacity = planCapacity({ }); log.info(capacity, "capacity"); // Enough connections for everything running at once, within what's left of Postgres' max_connections once the -// API's cluster, the realtime broadcaster and ad hoc connections (migrations, scripts, psql) have theirs. +// API's cluster (Prisma pool and realtime connections), the superuser-reserved slots, the realtime broadcaster +// and ad hoc connections (migrations, scripts, psql) have theirs. const wanted = 10 + Math.ceil((capacity.archiveDownloads + capacity.faces + capacity.ingest) / 2); const db = createDb( config.DATABASE_URL, - dbConnectionBudget(wanted, { max: config.DB_MAX_CONNECTIONS, apiInstances: config.API_INSTANCES, apiPoolSize: config.API_DB_POOL_SIZE }), + dbConnectionBudget(wanted, { + max: config.DB_MAX_CONNECTIONS, + apiInstances: config.API_INSTANCES, + apiPoolSize: config.API_DB_POOL_SIZE, + superuserReserved: config.DB_SUPERUSER_RESERVED_CONNECTIONS, + }), ); const storage = new LocalStorage(config.STORAGE_ROOT); const broadcaster = createRemoteBroadcaster(config.DATABASE_URL, (err) => log.error({ err }, "realtime publish failed")); diff --git a/apps/api/test/capacity.test.ts b/apps/api/test/capacity.test.ts index b750469..5bf2431 100644 --- a/apps/api/test/capacity.test.ts +++ b/apps/api/test/capacity.test.ts @@ -40,15 +40,15 @@ describe("capacity", () => { }); describe("dbConnectionBudget", () => { - it("stays under what the API's cluster, the broadcaster and headroom leave of max_connections", () => { - // 100 - (2 api instances * 10) - 1 broadcaster - 10 headroom = 69. - expect(dbConnectionBudget(500)).toBe(69); + it("stays under what the API's cluster, the superuser reserve, the broadcaster and headroom leave of max_connections", () => { + // 100 - 3 superuser reserved - (2 api instances * (10 pool + 3 realtime)) - 1 broadcaster - 10 headroom = 60. + expect(dbConnectionBudget(500)).toBe(60); expect(dbConnectionBudget(10)).toBe(10); }); it("follows a differently sized API cluster or Postgres limit", () => { - expect(dbConnectionBudget(500, { max: 200, apiInstances: 4, apiPoolSize: 15 })).toBe(129); - expect(dbConnectionBudget(500, { max: 40, apiInstances: 2, apiPoolSize: 10 })).toBe(9); + expect(dbConnectionBudget(500, { max: 200, apiInstances: 4, apiPoolSize: 15 })).toBe(114); + expect(dbConnectionBudget(500, { max: 40, apiInstances: 2, apiPoolSize: 10 })).toBe(1); }); it("never goes below 1, even if the API and headroom already exceed max_connections", () => {