Skip to content

Commit

Permalink
feat: add exponential backoff to db connection
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovicentegc committed Dec 10, 2023
1 parent 319d3c5 commit b6c4544
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
Binary file modified bun.lockb
Binary file not shown.
19 changes: 11 additions & 8 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This approach is taken from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb
import { MongoClient } from "mongodb";
import { backOff } from "exponential-backoff";

if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
Expand Down Expand Up @@ -36,15 +37,17 @@ async function getMongoClient() {
}

async function connectToDatabase() {
try {
const client = await getMongoClient();
await client.connect();
const db = client.db(process.env.MONGODB_DB);
return await backOff(async () => {
try {
const client = await getMongoClient();
await client.connect();
const db = client.db(process.env.MONGODB_DB);

return db;
} catch (error) {
console.error(error);
}
return db;
} catch (error) {
console.error(error);
}
});
}

export async function getCompletionsPendingReviewCollection() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@auth/mongodb-adapter": "^2.0.1",
"@monaco-editor/react": "^4.6.0",
"@sentry/nextjs": "^7.86.0",
"exponential-backoff": "^3.1.1",
"grommet": "^2.33.2",
"monaco-editor": "^0.44.0",
"mongodb": "^6.1.0",
Expand Down

0 comments on commit b6c4544

Please sign in to comment.