Просмотр исходного кода

Allow independent pools to be used for mongo/mongoose

GitOrigin-RevId: c0c7d8e3388fd9402d7b22b70eda9455b4ecc205
andrew rumble 2 лет назад
Родитель
Сommit
e08c60424d
1 измененных файлов с 28 добавлено и 5 удалено
  1. 28 5
      services/web/app/src/infrastructure/mongodb.js

+ 28 - 5
services/web/app/src/infrastructure/mongodb.js

@@ -2,6 +2,7 @@ const mongodb = require('mongodb-legacy')
 const OError = require('@overleaf/o-error')
 const Settings = require('@overleaf/settings')
 const Mongoose = require('./Mongoose')
+const { addConnectionDrainer } = require('./GracefulShutdown')
 
 // Ensure Mongoose is using the same mongodb instance as the mongodb module,
 // otherwise we will get multiple versions of the ObjectId class. Mongoose
@@ -13,7 +14,6 @@ if (Mongoose.mongo !== mongodb) {
   )
 }
 
-const { getNativeDb } = Mongoose
 const { ObjectId, ReadPreference } = mongodb
 
 if (
@@ -39,8 +39,31 @@ async function waitForDb() {
 }
 
 const db = {}
+
+let clientPromise
+
+async function getClient() {
+  if (!clientPromise) {
+    clientPromise = mongodb.MongoClient.connect(
+      Settings.mongo.url,
+      Settings.mongo.options
+    )
+  }
+  return await clientPromise
+}
+
+addConnectionDrainer('mongodb', async () => {
+  const client = await getClient()
+  await client.close()
+})
+
+async function getInternalDb() {
+  const client = await getClient()
+  return client.db()
+}
+
 async function setupDb() {
-  const internalDb = await getNativeDb()
+  const internalDb = await getInternalDb()
 
   db.contacts = internalDb.collection('contacts')
   db.deletedFiles = internalDb.collection('deletedFiles')
@@ -101,14 +124,14 @@ async function setupDb() {
 }
 
 async function getCollectionNames() {
-  const internalDb = await getNativeDb()
+  const internalDb = await getInternalDb()
 
   const collections = await internalDb.collections()
   return collections.map(collection => collection.collectionName)
 }
 
 async function dropTestDatabase() {
-  const internalDb = await getNativeDb()
+  const internalDb = await getInternalDb()
   const dbName = internalDb.databaseName
   const env = process.env.NODE_ENV
 
@@ -125,7 +148,7 @@ async function dropTestDatabase() {
  * WARNING: Consider using a pre-populated collection from `db` to avoid typos!
  */
 async function getCollectionInternal(name) {
-  const internalDb = await getNativeDb()
+  const internalDb = await getInternalDb()
   return internalDb.collection(name)
 }