Skip to content

Commit

Permalink
perf(core): update mongodb default connection settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi committed Nov 5, 2024
1 parent ef41d9f commit 6fa3c9c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const updateWalletInvoiceWithPaymentRequest = wrapAsyncToRunInSpan({
},
})

setupMongoConnection(false)
setupMongoConnection()
.then(async (mongoose) => {
await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams)))
await main()
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/debug/clean-pending-payments-for-dead-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const main = async (): Promise<true | ApplicationError> => {
return true
}

setupMongoConnection(false)
setupMongoConnection()
.then(async (mongoose) => {
await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams)))
await main()
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/debug/migrate-ln-payments-listpayments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ const getFirstIndex = async (pubkey: Pubkey): Promise<number | ApplicationError>
: parseInt(indexMatch) + 1
}

setupMongoConnection(false)
setupMongoConnection()
.then(async (mongoose) => {
await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams)))
await main()
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/debug/migrate-ln-payments-trackpaymentsv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const migrateLnPayment = async (
},
)

setupMongoConnection(false)
setupMongoConnection()
.then(async (mongoose) => {
await Promise.all(lndsConnect.map((lndParams) => isUp(lndParams)))
await main()
Expand Down
2 changes: 1 addition & 1 deletion core/api/src/servers/graphql-main-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
30 changes: 28 additions & 2 deletions core/api/src/services/mongodb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/api/test/integration/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 6fa3c9c

Please sign in to comment.