Skip to content

Commit

Permalink
allow maxPoolSize to be configurable by env var (defaults to 50)
Browse files Browse the repository at this point in the history
- env is defined as `MONGODB_MAX_POOL_SIZE`
  • Loading branch information
nick-funk committed Oct 31, 2024
1 parent 8094b3e commit 53dae81
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 6 additions & 0 deletions server/src/core/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions server/src/core/server/data/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ export function isArchivingEnabled(config: Config): boolean {
export async function createMongoContext(
config: Config
): Promise<MongoContext> {
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
Expand All @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions server/src/core/server/services/mongodb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { Db, MongoClient } from "mongodb";
import { WrappedInternalError } from "coral-server/errors";
import logger from "coral-server/logger";

async function createMongoClient(mongoURI: string): Promise<MongoClient> {
async function createMongoClient(
mongoURI: string,
maxPoolSize = 50
): Promise<MongoClient> {
try {
return await MongoClient.connect(mongoURI, {
// believe we don't need this since the new driver only uses
// the new URL parser. But am leaving this here in case we have
// issues with URL's and want to investigate into it.
// useNewUrlParser: true,
ignoreUndefined: true,
maxPoolSize,
});
} catch (err) {
throw new WrappedInternalError(
Expand Down Expand Up @@ -45,10 +49,11 @@ interface CreateMongoDbResult {
* @param config application configuration.
*/
export async function createMongoDB(
mongoURI: string
mongoURI: string,
maxPoolSize?: number
): Promise<CreateMongoDbResult> {
// Connect and create a client for MongoDB.
const client = await createMongoClient(mongoURI);
const client = await createMongoClient(mongoURI, maxPoolSize);

logger.info("mongodb has connected");

Expand Down

0 comments on commit 53dae81

Please sign in to comment.