-
Notifications
You must be signed in to change notification settings - Fork 1
fix: faster MongoDB retry with reduced timeouts #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,22 +1,62 @@ | ||||||||
| """MongoDB connection management using Beanie ODM.""" | ||||||||
|
|
||||||||
| import asyncio | ||||||||
|
|
||||||||
| import structlog | ||||||||
| from beanie import init_beanie | ||||||||
| from pymongo import AsyncMongoClient | ||||||||
|
|
||||||||
| from app.config import settings | ||||||||
|
|
||||||||
| logger = structlog.get_logger() | ||||||||
|
|
||||||||
| client: AsyncMongoClient | None = None | ||||||||
|
|
||||||||
| MAX_RETRIES = 10 | ||||||||
| RETRY_DELAY = 3 # seconds | ||||||||
|
|
||||||||
|
|
||||||||
| async def init_db() -> None: | ||||||||
| """Initialize MongoDB connection and Beanie ODM.""" | ||||||||
| """Initialize MongoDB connection and Beanie ODM with retry logic.""" | ||||||||
| global client | ||||||||
| client = AsyncMongoClient(settings.mongo_uri) | ||||||||
| db = client[settings.mongo_db] | ||||||||
|
|
||||||||
| from app.models import ALL_MODELS | ||||||||
|
|
||||||||
| await init_beanie(database=db, document_models=ALL_MODELS) | ||||||||
| for attempt in range(1, MAX_RETRIES + 1): | ||||||||
| try: | ||||||||
| client = AsyncMongoClient( | ||||||||
| settings.mongo_uri, | ||||||||
| serverSelectionTimeoutMS=5000, | ||||||||
| connectTimeoutMS=5000, | ||||||||
| socketTimeoutMS=10000, | ||||||||
| ) | ||||||||
| db = client[settings.mongo_db] | ||||||||
| await init_beanie(database=db, document_models=ALL_MODELS) | ||||||||
|
Comment on lines
+26
to
+33
|
||||||||
| if attempt > 1: | ||||||||
| logger.info("Database connected after retry", attempt=attempt) | ||||||||
| return | ||||||||
| except Exception as exc: | ||||||||
| if attempt == MAX_RETRIES: | ||||||||
|
Comment on lines
+37
to
+38
|
||||||||
| logger.error( | ||||||||
| "Database connection failed after all retries", | ||||||||
| attempts=MAX_RETRIES, | ||||||||
| error=str(exc), | ||||||||
|
||||||||
| error=str(exc), | |
| error=str(exc), | |
| exc_info=True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MAX_RETRIES,RETRY_DELAY, and the timeout values are hard-coded. If this needs tuning per environment (local dev vs. CI vs. prod), consider moving these toSettings(env-configurable) so behavior can be adjusted without a redeploy/code change.