Skip to content

Commit

Permalink
separate db pool for internal operations (#2163)
Browse files Browse the repository at this point in the history
  • Loading branch information
ichub authored Nov 12, 2024
1 parent f33429a commit 18c53b0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions apps/passport-server/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
8 changes: 7 additions & 1 deletion apps/passport-server/src/database/postgresConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions apps/passport-server/src/database/postgresPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { logger } from "../util/logger";
import { getDatabaseConfiguration } from "./postgresConfiguration";
import { migrateDatabase } from "./postgresMigrations";

export async function getDB(): Promise<Pool> {
export async function getDB(overwriteMaxConnections?: number): Promise<Pool> {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
await sqlTransaction(this.context.dbPool, (client) =>
await sqlTransaction(this.context.internalPool, (client) =>
this.loadAndInstantiatePipelines(client)
);

Expand Down Expand Up @@ -357,7 +357,7 @@ export class PipelineExecutorSubservice {
}

let runInfo = await performPipelineLoad(
this.context.dbPool,
this.context.internalPool,
pipelineSlot,
this.pipelineSubservice,
this.userSubservice,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion apps/passport-server/src/services/semaphoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions apps/passport-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export enum ServerMode {

export interface ApplicationContext {
dbPool: Pool;
internalPool: Pool;
honeyClient: Libhoney | null;
resourcesDir: string;
publicResourcesDir: string;
Expand Down

0 comments on commit 18c53b0

Please sign in to comment.