Skip to content

Commit

Permalink
add an internal-only db pool
Browse files Browse the repository at this point in the history
  • Loading branch information
ichub committed Nov 12, 2024
1 parent f33429a commit 6c1a770
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 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(5);

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 | undefined
): 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
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 6c1a770

Please sign in to comment.