-
Notifications
You must be signed in to change notification settings - Fork 8
feat(database): retry connection #36
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
Conversation
The application usually starts before the db is ready to accept connections, so we retry incase the app is fast af
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.
Pull Request Overview
This PR introduces a retry mechanism for connecting to PostgreSQL and MySQL databases to handle scenarios when the database isn’t ready at application startup.
- Adds retry loops for database connection attempts
- Integrates additional logging using otelzap alongside existing logging routines
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| internal/database/pg.go | Implements retry logic for PostgreSQL connection with enhanced logging. |
| internal/database/mysql.go | Implements retry logic for MySQL connection and updates error messaging. |
| maxRetries := 3 | ||
| retryWaitTime := 5 * time.Second | ||
|
|
||
| for i := range maxRetries { |
Copilot
AI
Jun 5, 2025
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.
The for-range loop is incorrectly used here because maxRetries is an integer rather than an iterable. Consider using a standard loop such as 'for i := 0; i < maxRetries; i++' for proper iteration.
| for i := range maxRetries { | |
| for i := 0; i < maxRetries; i++ { |
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.
wrong
internal/database/pg.go
Outdated
| if err != nil { | ||
| otelzap.L().Fatal("Failed to prepare PostgreSQL connection", zap.Error(err)) | ||
| } |
Copilot
AI
Jun 5, 2025
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.
The error check after opening the database connection is ineffective because 'err' is not updated from sql.OpenDB; consider removing this check or revising the error handling logic.
| if err != nil { | |
| otelzap.L().Fatal("Failed to prepare PostgreSQL connection", zap.Error(err)) | |
| } |
| maxRetries := 3 | ||
| retryWaitTime := 5 * time.Second | ||
|
|
||
| for i := range maxRetries { |
Copilot
AI
Jun 5, 2025
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.
The for-range loop is used on an integer (maxRetries) which is invalid; change this to a conventional loop like 'for i := 0; i < maxRetries; i++' to correctly iterate.
| for i := range maxRetries { | |
| for i := 0; i < maxRetries; i++ { |
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.
wrong
| for i := range maxRetries { | ||
| sqldb, err = sql.Open("mysql", dsn) | ||
| if err != nil { | ||
| log.Fatalf("failed to prepare mysql connection: %v", err) |
Copilot
AI
Jun 5, 2025
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.
[nitpick] The code mixes the standard log package with otelzap for logging errors; consider using a consistent logging approach throughout the file.
| log.Fatalf("failed to prepare mysql connection: %v", err) | |
| otelzap.L().Fatal("Failed to prepare MySQL connection", zap.Error(err)) |
The application usually starts before the db is ready to accept connections, so we retry incase the app is fast af