Skip to content

fix: add circuit breaker to MongoDB logger to prevent 10s delays on every request #101

@rishab11250

Description

@rishab11250

Description

logApiAccess() in logger.js attempts a full MongoDB connection on every single API profile request with a 10-second serverSelectionTimeoutMS. If MongoDB is unreachable, every profile render is delayed by up to 10 seconds. There is no circuit breaker — once a connection fails, the next request repeats the entire 10-second wait.

This creates cascading latency: if MongoDB goes down, every subsequent profile request suffers a 10s delay before eventually succeeding or failing.

Steps to Reproduce

  1. Set an invalid MONGODB_URI
  2. Request /api/profile?username=octocat
  3. Observe ~10 second delay
  4. Request again — same 10 second delay repeats

Root Cause

src/utils/logger.js — connectToDatabase() doesn't cache failure state. The connectionAttempted variable on line 4 is declared but never read after being set.

Suggested Fix

Cache the failure state so that if a MongoDB connection attempt fails, subsequent requests within the same process skip the connection attempt entirely and log a warning instead of retrying.

Files to Modify

  • src/utils/logger.js

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions