From 53dae816fcc2e9e232b3389a03cd012531426f82 Mon Sep 17 00:00:00 2001 From: nick-funk Date: Thu, 31 Oct 2024 13:27:36 -0600 Subject: [PATCH] allow `maxPoolSize` to be configurable by env var (defaults to 50) - env is defined as `MONGODB_MAX_POOL_SIZE` --- server/src/core/server/config.ts | 6 ++++++ server/src/core/server/data/context.ts | 6 ++++-- server/src/core/server/services/mongodb/index.ts | 11 ++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/server/src/core/server/config.ts b/server/src/core/server/config.ts index 5caaeeb9a3..c9449a4990 100644 --- a/server/src/core/server/config.ts +++ b/server/src/core/server/config.ts @@ -186,6 +186,12 @@ const config = convict({ env: "MONGODB_ARCHIVE_URI", sensitive: true, }, + mongodb_max_pool_size: { + doc: "Max pool size for the MongoDB driver.", + format: Number, + default: 50, + env: "MONGODB_MAX_POOL_SIZE", + }, redis: { doc: "The Redis database to connect to.", format: "redis-uri", diff --git a/server/src/core/server/data/context.ts b/server/src/core/server/data/context.ts index d51c94ffba..2e133a103c 100644 --- a/server/src/core/server/data/context.ts +++ b/server/src/core/server/data/context.ts @@ -140,9 +140,11 @@ export function isArchivingEnabled(config: Config): boolean { export async function createMongoContext( config: Config ): Promise { + const maxPoolSize = config.get("mongodb_max_pool_size"); + // Setup MongoDB. const liveURI = config.get("mongodb"); - const live = (await createMongoDB(liveURI)).db; + const live = (await createMongoDB(liveURI, maxPoolSize)).db; // If we have an archive URI, use it, otherwise, default // to using the live database @@ -154,7 +156,7 @@ export async function createMongoContext( ) { archive = live; } else { - archive = (await createMongoDB(archiveURI)).db; + archive = (await createMongoDB(archiveURI, maxPoolSize)).db; } return new MongoContextImpl(live, archive); diff --git a/server/src/core/server/services/mongodb/index.ts b/server/src/core/server/services/mongodb/index.ts index 95628e69be..c0d2451892 100644 --- a/server/src/core/server/services/mongodb/index.ts +++ b/server/src/core/server/services/mongodb/index.ts @@ -3,7 +3,10 @@ import { Db, MongoClient } from "mongodb"; import { WrappedInternalError } from "coral-server/errors"; import logger from "coral-server/logger"; -async function createMongoClient(mongoURI: string): Promise { +async function createMongoClient( + mongoURI: string, + maxPoolSize = 50 +): Promise { try { return await MongoClient.connect(mongoURI, { // believe we don't need this since the new driver only uses @@ -11,6 +14,7 @@ async function createMongoClient(mongoURI: string): Promise { // issues with URL's and want to investigate into it. // useNewUrlParser: true, ignoreUndefined: true, + maxPoolSize, }); } catch (err) { throw new WrappedInternalError( @@ -45,10 +49,11 @@ interface CreateMongoDbResult { * @param config application configuration. */ export async function createMongoDB( - mongoURI: string + mongoURI: string, + maxPoolSize?: number ): Promise { // Connect and create a client for MongoDB. - const client = await createMongoClient(mongoURI); + const client = await createMongoClient(mongoURI, maxPoolSize); logger.info("mongodb has connected");