Merge pull request #10715 from overleaf/jpa-web-share-mongo-pool

[web] share mongo connection pool between Mongoose and native db

GitOrigin-RevId: 8bb2a9dc76880144a8681cb564183906df624cc0
This commit is contained in:
Brian Gough 2022-12-01 11:08:54 +00:00 committed by Copybot
parent 4b856dbd70
commit f8a1da1b47
3 changed files with 9 additions and 21 deletions

View file

@ -2,7 +2,6 @@ const mongoose = require('mongoose')
const Settings = require('@overleaf/settings')
const logger = require('@overleaf/logger')
const { addConnectionDrainer } = require('./GracefulShutdown')
const { collectStatsForDb } = require('./mongodb')
if (
typeof global.beforeEach === 'function' &&
@ -61,8 +60,6 @@ async function getNativeDb() {
return mongooseInstance.connection.db
}
getNativeDb().then(db => collectStatsForDb(db, 'mongoose'))
mongoose.getNativeDb = getNativeDb
mongoose.connectionPromise = connectionPromise

View file

@ -1,11 +1,10 @@
const Metrics = require('@overleaf/metrics')
const Settings = require('@overleaf/settings')
const { MongoClient, ObjectId } = require('mongodb')
const { ObjectId } = require('mongodb')
const OError = require('@overleaf/o-error')
const {
addConnectionDrainer,
addOptionalCleanupHandlerAfterDrainingConnections,
} = require('./GracefulShutdown')
const { getNativeDb } = require('./Mongoose')
if (
typeof global.beforeEach === 'function' &&
@ -16,15 +15,6 @@ if (
)
}
const clientPromise = MongoClient.connect(
Settings.mongo.url,
Settings.mongo.options
)
addConnectionDrainer('mongodb', async () => {
const client = await clientPromise
client.close()
})
let setupDbPromise
async function waitForDb() {
if (!setupDbPromise) {
@ -35,8 +25,8 @@ async function waitForDb() {
const db = {}
async function setupDb() {
const internalDb = (await clientPromise).db()
collectStatsForDb(internalDb, 'native')
const internalDb = await getNativeDb()
collectStatsForDb(internalDb, 'shared')
db.contacts = internalDb.collection('contacts')
db.deletedFiles = internalDb.collection('deletedFiles')
@ -92,14 +82,14 @@ async function setupDb() {
}
async function getCollectionNames() {
const internalDb = (await clientPromise).db()
const internalDb = await getNativeDb()
const collections = await internalDb.collections()
return collections.map(collection => collection.collectionName)
}
async function dropTestDatabase() {
const internalDb = (await clientPromise).db()
const internalDb = await getNativeDb()
const dbName = internalDb.databaseName
const env = process.env.NODE_ENV
@ -116,7 +106,7 @@ async function dropTestDatabase() {
* WARNING: Consider using a pre-populated collection from `db` to avoid typos!
*/
async function getCollectionInternal(name) {
const internalDb = (await clientPromise).db()
const internalDb = await getNativeDb()
return internalDb.collection(name)
}

View file

@ -2,7 +2,8 @@ function filterOutput(line) {
return (
!line.startsWith('Using settings from ') &&
!line.startsWith('Using default settings from ') &&
!line.startsWith('CoffeeScript settings file')
!line.startsWith('CoffeeScript settings file') &&
!line.includes('mongoose default connection open')
)
}