From 6fa3c9c9a42d82ba57473cae640433ad05a75689 Mon Sep 17 00:00:00 2001 From: Juan P Lopez Date: Tue, 5 Nov 2024 11:55:15 -0500 Subject: [PATCH] perf(core): update mongodb default connection settings --- .../add-payment-request-all-wallet-invoice.ts | 2 +- .../clean-pending-payments-for-dead-node.ts | 2 +- .../debug/migrate-ln-payments-listpayments.ts | 2 +- .../migrate-ln-payments-trackpaymentsv2.ts | 2 +- core/api/src/servers/graphql-main-server.ts | 2 +- core/api/src/services/mongodb/index.ts | 30 +++++++++++++++++-- core/api/test/integration/jest.setup.js | 2 +- 7 files changed, 34 insertions(+), 8 deletions(-) diff --git a/core/api/src/debug/add-payment-request-all-wallet-invoice.ts b/core/api/src/debug/add-payment-request-all-wallet-invoice.ts index 0dc281d7da..b24f9c1b56 100644 --- a/core/api/src/debug/add-payment-request-all-wallet-invoice.ts +++ b/core/api/src/debug/add-payment-request-all-wallet-invoice.ts @@ -135,7 +135,7 @@ const updateWalletInvoiceWithPaymentRequest = wrapAsyncToRunInSpan({ }, }) -setupMongoConnection(false) +setupMongoConnection() .then(async (mongoose) => { await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams))) await main() diff --git a/core/api/src/debug/clean-pending-payments-for-dead-node.ts b/core/api/src/debug/clean-pending-payments-for-dead-node.ts index 09c1fd2828..4347a3d53a 100644 --- a/core/api/src/debug/clean-pending-payments-for-dead-node.ts +++ b/core/api/src/debug/clean-pending-payments-for-dead-node.ts @@ -79,7 +79,7 @@ const main = async (): Promise => { return true } -setupMongoConnection(false) +setupMongoConnection() .then(async (mongoose) => { await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams))) await main() diff --git a/core/api/src/debug/migrate-ln-payments-listpayments.ts b/core/api/src/debug/migrate-ln-payments-listpayments.ts index 8da4d7dc94..6684231e79 100644 --- a/core/api/src/debug/migrate-ln-payments-listpayments.ts +++ b/core/api/src/debug/migrate-ln-payments-listpayments.ts @@ -259,7 +259,7 @@ const getFirstIndex = async (pubkey: Pubkey): Promise : parseInt(indexMatch) + 1 } -setupMongoConnection(false) +setupMongoConnection() .then(async (mongoose) => { await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams))) await main() diff --git a/core/api/src/debug/migrate-ln-payments-trackpaymentsv2.ts b/core/api/src/debug/migrate-ln-payments-trackpaymentsv2.ts index 7efac48227..837e7239d6 100644 --- a/core/api/src/debug/migrate-ln-payments-trackpaymentsv2.ts +++ b/core/api/src/debug/migrate-ln-payments-trackpaymentsv2.ts @@ -137,7 +137,7 @@ const migrateLnPayment = async ( }, ) -setupMongoConnection(false) +setupMongoConnection() .then(async (mongoose) => { await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams))) await main() diff --git a/core/api/src/servers/graphql-main-server.ts b/core/api/src/servers/graphql-main-server.ts index c81eecee2c..a5a642927d 100644 --- a/core/api/src/servers/graphql-main-server.ts +++ b/core/api/src/servers/graphql-main-server.ts @@ -7,7 +7,7 @@ import { activateLndHealthCheck } from "@/services/lnd/health" import { baseLogger } from "@/services/logger" import { setupMongoConnection } from "@/services/mongodb" -setupMongoConnection(true) +setupMongoConnection({ syncIndexes: true }) .then(async () => { activateLndHealthCheck() diff --git a/core/api/src/services/mongodb/index.ts b/core/api/src/services/mongodb/index.ts index ae17284fe4..8c0035bde1 100644 --- a/core/api/src/services/mongodb/index.ts +++ b/core/api/src/services/mongodb/index.ts @@ -17,6 +17,30 @@ import { WalletCurrency } from "@/domain/shared" import { lazyLoadLedgerAdmin } from "@/services/ledger" import { WalletsRepository } from "@/services/mongoose" +type SetupMongoConnectionArgs = { + syncIndexes: boolean + options?: mongoose.ConnectOptions +} + +const DEFAULT_MONGODB_OPTIONS = { + autoIndex: false, + maxPoolSize: 50, + minPoolSize: 10, + socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity + connectTimeoutMS: 20000, // Give up initial connection after 20 seconds + serverSelectionTimeoutMS: 10000, // Keep trying to send operations for 10 seconds + retryWrites: true, + writeConcern: { + w: "majority", // Wait for majority acknowledgment + j: true, // Wait for journal commit + wtimeout: 5000, // Write timeout + }, + maxConnecting: 10, // Maximum number of concurrent connection attempts + compression: ["snappy", "zlib"], // Enable compression + readPreference: "primaryPreferred", // Prefer primary but allow secondary reads + readConcern: { level: "majority" }, // Read from majority-committed data +} as const + export const ledgerAdmin = lazyLoadLedgerAdmin({ bankOwnerWalletResolver: async () => { const result = await Account.findOne({ role: "bankowner" }, { defaultWalletId: 1 }) @@ -63,9 +87,11 @@ export const ledgerAdmin = lazyLoadLedgerAdmin({ // TODO add an event listenever if we got disconnecter from MongoDb // after a first successful connection -export const setupMongoConnection = async (syncIndexes = false) => { +export const setupMongoConnection = async ( + { syncIndexes, options }: SetupMongoConnectionArgs = { syncIndexes: false }, +) => { try { - await mongoose.connect(MONGODB_CON, { autoIndex: false }) + await mongoose.connect(MONGODB_CON, { ...DEFAULT_MONGODB_OPTIONS, ...options }) } catch (err) { baseLogger.fatal(`error connecting to mongodb`) throw err diff --git a/core/api/test/integration/jest.setup.js b/core/api/test/integration/jest.setup.js index b9f215f2b8..99443719e9 100644 --- a/core/api/test/integration/jest.setup.js +++ b/core/api/test/integration/jest.setup.js @@ -13,7 +13,7 @@ jest.mock("@/services/lnd/auth", () => { jest.mock("@/app/prices/get-current-price", () => require("test/mocks/get-current-price")) beforeAll(async () => { - mongoose = await setupMongoConnection(true) + mongoose = await setupMongoConnection({ syncIndexes: true }) }) afterAll(async () => {