Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions cmd/template/dbdriver/files/service/mysql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ func New() Service {
dbInstance = &service{
db: db,
}

// Set a timeout for the Ping operation.
//
// TODO (H0llyW00dzZ): Use an environment variable to customize the timeout (e.g., "10*time.Second").
// For now, explicitly setting it to 10 seconds should be sufficient, as it is only used during initialization to avoid runtime confusion.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// Note (H0llyW00dzZ): It's not possible to handle this without using log.Fatalf (e.g., by returning nil and fmt.Errorf("failed to connect database: %w", err))
// because the db.New function does not currently return an error. To improve error handling, the function db.New should be refactored to return an error,
// allowing the caller to manage it appropriately.
if err = db.PingContext(ctx); err != nil {
log.Fatalf("Failed to connect database: %v", err)
}
return dbInstance
}

Expand Down