From 1304398b149a64509d44c6f03886717f28a754e6 Mon Sep 17 00:00:00 2001 From: Ivan Chub Date: Mon, 11 Nov 2024 20:49:19 -0800 Subject: [PATCH] add env var for changing the database pool size (#2158) --- apps/passport-server/.env.example | 3 +++ .../src/database/postgresConfiguration.ts | 11 ++++++++++- turbo.json | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/passport-server/.env.example b/apps/passport-server/.env.example index 3337ab8dc5..c71e0e3dbd 100644 --- a/apps/passport-server/.env.example +++ b/apps/passport-server/.env.example @@ -237,3 +237,6 @@ IS_LOCAL_HTTPS=false # `next` field in its response, we instead know that we've loaded all orders when # there are 404 errors for pages that are not the first page, and no other errors. # PRETIX_BATCH_ENABLED_FOR=[""] + +# The number of database connections to keep open. +# DB_POOL_SIZE=32 diff --git a/apps/passport-server/src/database/postgresConfiguration.ts b/apps/passport-server/src/database/postgresConfiguration.ts index dc7ee0187f..4d589d386c 100644 --- a/apps/passport-server/src/database/postgresConfiguration.ts +++ b/apps/passport-server/src/database/postgresConfiguration.ts @@ -26,6 +26,15 @@ export function getDatabaseConfiguration(): PoolOptionsExplicit & SslSettings { throw new Error("Missing or incorrect env variable: DATABASE_SSL"); } + let poolSize = 32; + if (process.env.DB_POOL_SIZE !== undefined) { + poolSize = parseInt(process.env.DB_POOL_SIZE, 10); + if (isNaN(poolSize)) { + poolSize = 32; + } + poolSize = Math.min(Math.max(poolSize, 32), 70); + } + // 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 { @@ -43,7 +52,7 @@ export function getDatabaseConfiguration(): PoolOptionsExplicit & SslSettings { // Pool configuration connectionTimeoutMillis: 16_000, idleTimeoutMillis: 8_000, - poolSize: 32, // max connection # on render is 97 + poolSize: poolSize, // max connection # on render is 97 waitForAvailableConnectionTimeoutMillis: 30_000, databaseStartupTimeoutMillis: 30_000, readOnlyTransactionReconnectTimeoutMillis: 30_000, diff --git a/turbo.json b/turbo.json index a55e474900..24c0ab35b1 100644 --- a/turbo.json +++ b/turbo.json @@ -243,6 +243,7 @@ "SELF_HOSTED_PODBOX_MODE", "GENERIC_ISSUANCE_TEST_MODE", "PRETIX_BATCH_ENABLED_FOR", + "DB_POOL_SIZE", "//// add env vars above this line ////" ] }