feat(api): MongoDB reconnect, bounded timeouts and graceful shutdown#652
Open
Kuchizu wants to merge 2 commits into
Open
feat(api): MongoDB reconnect, bounded timeouts and graceful shutdown#652Kuchizu wants to merge 2 commits into
Kuchizu wants to merge 2 commits into
Conversation
neSpecc
reviewed
May 13, 2026
Member
neSpecc
left a comment
There was a problem hiding this comment.
I’m only seeing retries on the initial connection. Where is the reconnection on the failing MongoDB connection?
Contributor
There was a problem hiding this comment.
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 on lines
+139
to
142
| const [hawkClient, eventsClient] = await Promise.all([ | ||
| connectWithRetry('hawk', hawkDBUrl), | ||
| connectWithRetry('events', eventsDBUrl), | ||
| ]); |
Comment on lines
+320
to
+321
| process.once('SIGINT', shutdown); | ||
| process.once('SIGTERM', shutdown); |
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 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)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Retry on startup. Queries fail fast during outages instead of hanging. Driver handles recovery on its own.