We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
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
_:hammer_and_wrench: Refactor suggestion_
Enhance database connection configuration for production readiness.
Consider adding the following improvements:
Here's a suggested implementation:
-func NewDatabaseConnection(env *Env) (*gorm.DB, error) { - return gorm.Open(postgres.Open(env.DATABASE_URL), &gorm.Config{}) -} +func NewDatabaseConnection(env *Env) (*gorm.DB, error) { + if env.DATABASE_URL == "" { + return nil, fmt.Errorf("DATABASE_URL is required") + } + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + config := &gorm.Config{ + PrepareStmt: true, + NowFunc: func() time.Time { + return time.Now().UTC() + }, + } + + var db *gorm.DB + var err error + + for retries := 3; retries > 0; retries-- { + db, err = gorm.Open(postgres.Open(env.DATABASE_URL), config) + if err == nil { + break + } + time.Sleep(time.Second) + } + if err != nil { + return nil, fmt.Errorf("failed to connect to database: %w", err) + } + + sqlDB, err := db.DB() + if err != nil { + return nil, fmt.Errorf("failed to get underlying *sql.DB: %w", err) + } + + // Set connection pool settings + sqlDB.SetMaxIdleConns(10) + sqlDB.SetMaxOpenConns(100) + sqlDB.SetConnMaxLifetime(time.Hour) + + return db, nil +}
Don't forget to add the required imports:
import ( "context" "fmt" "time" )
Originally posted by @coderabbitai[bot] in #12 (comment)
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Enhance database connection configuration for production readiness.
Consider adding the following improvements:
Here's a suggested implementation:
Don't forget to add the required imports:
Originally posted by @coderabbitai[bot] in #12 (comment)
The text was updated successfully, but these errors were encountered: