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

Regression: Better MongoDB connection management for micro services #25323

Merged
merged 2 commits into from
Apr 27, 2022
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
9 changes: 3 additions & 6 deletions apps/meteor/ee/server/services/authorization/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { api } from '../../../../server/sdk/api';
import { Authorization } from '../../../../server/services/authorization/service';
import { getConnection } from '../mongo';

getConnection()
.then((db) => api.registerService(new Authorization(db)))
.catch((error) => {
console.error(error);
process.exit(1);
});
getConnection().then((db) => {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
api.registerService(new Authorization(db));
});
51 changes: 36 additions & 15 deletions apps/meteor/ee/server/services/mongo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MongoClient, Db, Collection } from 'mongodb';
import { MongoClient } from 'mongodb';
import type { Db, Collection } from 'mongodb';

const { MONGO_URL = 'mongodb://localhost:27017/rocketchat' } = process.env;

Expand All @@ -14,23 +15,43 @@ export enum Collections {
Settings = 'rocketchat_settings',
}

let db: Db;
export async function getConnection(poolSize = 5): Promise<Db> {
if (!db) {
const client = new MongoClient(MONGO_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
poolSize,
});

await client.connect();
db = client.db(name);
}
function connectDb(poolSize: number): Promise<MongoClient> {
const client = new MongoClient(MONGO_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
poolSize,
});

return db;
return client.connect().catch((error) => {
// exits the process in case of any error
console.error(error);
process.exit(1);
});
}

let db: Db;

export const getConnection = ((): ((poolSize?: number) => Promise<Db>) => {
let client: Promise<MongoClient>;

return async (poolSize = 5): Promise<Db> => {
if (db) {
return db;
}
if (!client) {
client = connectDb(poolSize);
client.then((c) => {
db = c.db(name);
});
}
// if getConnection was called multiple times before it was connected, wait for the connection
return (await client).db(name);
};
})();

export async function getCollection<T>(name: Collections): Promise<Collection<T>> {
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
await getConnection();
if (!db) {
db = await getConnection();
}
return db.collection<T>(name);
}