From 18c53b072f96aa85d5a9c5154219a9988b2704ae Mon Sep 17 00:00:00 2001 From: Ivan Chub Date: Tue, 12 Nov 2024 00:16:49 -0800 Subject: [PATCH] separate db pool for internal operations (#2163) --- apps/passport-server/src/application.ts | 3 +++ .../passport-server/src/database/postgresConfiguration.ts | 8 +++++++- apps/passport-server/src/database/postgresPool.ts | 4 ++-- .../subservices/PipelineExecutorSubservice.ts | 6 +++--- apps/passport-server/src/services/semaphoreService.ts | 2 +- apps/passport-server/src/types.ts | 1 + 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/apps/passport-server/src/application.ts b/apps/passport-server/src/application.ts index 4733feb96d..ee36613bf0 100644 --- a/apps/passport-server/src/application.ts +++ b/apps/passport-server/src/application.ts @@ -40,10 +40,13 @@ export async function startApplication( logger(`[INIT] Starting application in mode ${mode}`); const dbPool = await getDB(); + const internalPool = await getDB(10); + const honeyClient = getHoneycombAPI(); const context: ApplicationContext = { dbPool, + internalPool, honeyClient, resourcesDir: path.join(process.cwd(), "resources"), publicResourcesDir: path.join(process.cwd(), "public"), diff --git a/apps/passport-server/src/database/postgresConfiguration.ts b/apps/passport-server/src/database/postgresConfiguration.ts index c586f32072..8b84afb246 100644 --- a/apps/passport-server/src/database/postgresConfiguration.ts +++ b/apps/passport-server/src/database/postgresConfiguration.ts @@ -9,7 +9,9 @@ export interface DBConfiguration extends ClientConfig { port: number; } -export function getDatabaseConfiguration(): PoolOptionsExplicit & SslSettings { +export function getDatabaseConfiguration( + overwriteMaxConnections?: number +): PoolOptionsExplicit & SslSettings { if (process.env.DATABASE_USERNAME === undefined) { throw new Error("Missing environment variable: DATABASE_USERNAME"); } @@ -35,6 +37,10 @@ export function getDatabaseConfiguration(): PoolOptionsExplicit & SslSettings { poolSize = Math.min(Math.max(poolSize, 32), 500); } + if (overwriteMaxConnections !== undefined) { + poolSize = overwriteMaxConnections; + } + // defaults here: https://github.com/postgres-pool/postgres-pool/blob/9d623823dc365b5edea3303cab6ae519bfaa94f7/src/index.ts#L264C10-L290 // docs here: https://github.com/postgres-pool/postgres-pool/blob/9d623823dc365b5edea3303cab6ae519bfaa94f7/src/index.ts#L29-L130 return { diff --git a/apps/passport-server/src/database/postgresPool.ts b/apps/passport-server/src/database/postgresPool.ts index faac776ff7..c5c58556b8 100644 --- a/apps/passport-server/src/database/postgresPool.ts +++ b/apps/passport-server/src/database/postgresPool.ts @@ -3,11 +3,11 @@ import { logger } from "../util/logger"; import { getDatabaseConfiguration } from "./postgresConfiguration"; import { migrateDatabase } from "./postgresMigrations"; -export async function getDB(): Promise { +export async function getDB(overwriteMaxConnections?: number): Promise { logger("[INIT] Initializing Postgres client"); try { - const pool = new Pool(getDatabaseConfiguration()); + const pool = new Pool(getDatabaseConfiguration(overwriteMaxConnections)); const client = await pool.connect(); await migrateDatabase(client); client.release(); diff --git a/apps/passport-server/src/services/generic-issuance/subservices/PipelineExecutorSubservice.ts b/apps/passport-server/src/services/generic-issuance/subservices/PipelineExecutorSubservice.ts index eb97371f91..fc2739bfe8 100644 --- a/apps/passport-server/src/services/generic-issuance/subservices/PipelineExecutorSubservice.ts +++ b/apps/passport-server/src/services/generic-issuance/subservices/PipelineExecutorSubservice.ts @@ -101,7 +101,7 @@ export class PipelineExecutorSubservice { * schedules a load loop which loads data for each {@link Pipeline} once per minute. */ public async start(startLoadLoop?: boolean): Promise { - await sqlTransaction(this.context.dbPool, (client) => + await sqlTransaction(this.context.internalPool, (client) => this.loadAndInstantiatePipelines(client) ); @@ -357,7 +357,7 @@ export class PipelineExecutorSubservice { } let runInfo = await performPipelineLoad( - this.context.dbPool, + this.context.internalPool, pipelineSlot, this.pipelineSubservice, this.userSubservice, @@ -502,7 +502,7 @@ export class PipelineExecutorSubservice { // function - that will be done the next time `startPipelineLoadLoop` // is called. await namedSqlTransaction( - this.context.dbPool, + this.context.internalPool, "startPipelineLoadLoop", async (client) => { await this.loadAndInstantiatePipelines(client); diff --git a/apps/passport-server/src/services/semaphoreService.ts b/apps/passport-server/src/services/semaphoreService.ts index 5b41d21f18..c31d2fb9d0 100644 --- a/apps/passport-server/src/services/semaphoreService.ts +++ b/apps/passport-server/src/services/semaphoreService.ts @@ -57,7 +57,7 @@ export class SemaphoreService { public groupDevconnectOrganizers = (): NamedGroup => this.getNamedGroup("7"); public constructor(config: ApplicationContext) { - this.dbPool = config.dbPool; + this.dbPool = config.internalPool; this.groups = SemaphoreService.createGroups(); } diff --git a/apps/passport-server/src/types.ts b/apps/passport-server/src/types.ts index d88c78ce84..5c6f27584e 100644 --- a/apps/passport-server/src/types.ts +++ b/apps/passport-server/src/types.ts @@ -50,6 +50,7 @@ export enum ServerMode { export interface ApplicationContext { dbPool: Pool; + internalPool: Pool; honeyClient: Libhoney | null; resourcesDir: string; publicResourcesDir: string;