Skip to content

feat(api): MongoDB reconnect, bounded timeouts and graceful shutdown#652

Open
Kuchizu wants to merge 2 commits into
masterfrom
feat/mongo-reconnect
Open

feat(api): MongoDB reconnect, bounded timeouts and graceful shutdown#652
Kuchizu wants to merge 2 commits into
masterfrom
feat/mongo-reconnect

Conversation

@Kuchizu
Copy link
Copy Markdown
Member

@Kuchizu Kuchizu commented May 13, 2026

Retry on startup. Queries fail fast during outages instead of hanging. Driver handles recovery on its own.

@Kuchizu Kuchizu requested review from e11sy and neSpecc May 13, 2026 12:00
Copy link
Copy Markdown
Member

@neSpecc neSpecc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m only seeing retries on the initial connection. Where is the reconnection on the failing MongoDB connection?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves MongoDB connection resilience and shutdown behavior in the API: it adds bounded MongoDB timeouts (so queries fail fast during outages), startup retry logic, and graceful shutdown handlers to close connections cleanly.

Changes:

  • Add MongoClient options to bound server selection / socket timeouts and enable retry reads/writes.
  • Implement MongoDB startup connect-with-retry plus heartbeat transition logging, and add closeConnections() for graceful shutdown.
  • Register SIGINT/SIGTERM shutdown handlers to close HTTP, MongoDB, and Redis connections; bump package version to 1.5.1.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/mongo.ts Adds bounded Mongo options, startup retry connect, heartbeat monitoring, and connection close helper.
src/index.ts Adds SIGINT/SIGTERM shutdown handling to close HTTP/Mongo/Redis.
package.json Bumps package version.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/mongo.ts
Comment on lines +139 to 142
const [hawkClient, eventsClient] = await Promise.all([
connectWithRetry('hawk', hawkDBUrl),
connectWithRetry('events', eventsDBUrl),
]);
Comment thread src/index.ts
Comment on lines +320 to +321
process.once('SIGINT', shutdown);
process.once('SIGTERM', shutdown);
Comment thread src/mongo.ts
Comment on lines +8 to +10
const reconnectTries = Number(process.env.MONGO_RECONNECT_TRIES) || 60;
const reconnectInterval = Number(process.env.MONGO_RECONNECT_INTERVAL) || 1000;

Comment thread src/mongo.ts
Comment on lines +76 to +95
async function connectWithRetry(name: string, url: string): Promise<MongoClient> {
for (let attempt = 1; attempt <= reconnectTries; attempt++) {
const client = new MongoClient(url, connectionConfig);

try {
await client.connect();
console.log(`[Mongo:${name}] connected`);

return client;
} catch (err) {
await client.close().catch(() => undefined);

const message = (err as Error)?.message ?? String(err);

if (attempt === reconnectTries) {
throw new Error(`[Mongo:${name}] failed after ${reconnectTries} attempts: ${message}`);
}
console.warn(`[Mongo:${name}] attempt ${attempt}/${reconnectTries} failed: ${message}`);
await new Promise((resolve) => setTimeout(resolve, reconnectInterval));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants