Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow maxPoolSize to be configurable by env var (defaults to 50) #4688

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading