feat(database): async connect with background retry and health middleware - #158
Open
Gautam7352 wants to merge 6 commits into
Open
feat(database): async connect with background retry and health middleware#158Gautam7352 wants to merge 6 commits into
Gautam7352 wants to merge 6 commits into
Conversation
…ware - Replace blocking Connect() with non-blocking background goroutine - Retry DB connection every 5s indefinitely; server starts without waiting - Add watchConnection() to detect and recover from lost connections - Expose IsHealthy() via atomic int32 flag (no mutex contention) - Add DBHealthCheck() middleware returning 503 when DB is unreachable - Add testing.go helper SetHealthForTest() for unit tests (non-production only) - Update main.go to use new Connect(cfg) signature and register middleware
…er and sql.DB pool management
…it degraded state, reduce log noise
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.
What's changed
This PR overhauls how the backend connects to and monitors the database, replacing a blocking/polling approach with a non-blocking, self-healing connection model.
Database connection
Connect()is now non-blocking — the HTTP server starts immediately without waiting for the DB to be readysql.Openis called once outside the retry loop (it only validates the DSN, not the connection)atomic.Pointer[sql.DB]DB *sql.DBglobal withGetDB()accessor to eliminate data races on connect/reconnectHealth monitoring
StartHealthMonitor()— a two-speed background ping loop:MarkDegraded()can be signaled from anywhere without blocking (buffered channel, drops duplicates)HandleQueryError()in services/middleware callsMarkDegraded()on real DB errors (excludessql.ErrNoRows)/healthendpoint reads the cached atomic flag instead of doing a live ping on every callOther
.envfile loading andgodotenvdependency — configuration is read exclusively from environment variablesloggerpackage) replacing rawlog.PrintfcallswatchConnectionpolling loop —sql.DBmanages pool health internally