feat: retry initial MongoDB connections #5807
Merged
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.
Resolves #5806
Impact: minor
Type: feature
Issue
When spinning up separate API and MongoDB containers, due to timing the API container may try to connect to MongoDB before it is fully ready. Now that API starts up faster in 3.0.0, this is more likely. The MongoDB driver library does not auto-retry connecting on the first connection.
Solution
Adds our own auto-retry logic for the first connection, as well as better logging about what is happening with db connections.
Breaking changes
None
Testing
api
service but notmongo
service. Verify that you see logging about retrying 10 times and it eventually gives up and exits the app with the error. (In a development environment, you'll see this whole series of connection logs twice because it first tries to auto-init the replica set.)api
service but notmongo
service. While it's showing the retrying log messages in API logs, start themongo
service. You'll have about 30 seconds window to do so. Verify in the API logs that it eventually shows a successful db connection before it fully times out.api
andmongo
services normally. Verify that it shows a successful db connection in the logs on the first or second try.