Skip to content

Commit

Permalink
Add database retry (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
plutoyty authored Jul 12, 2023
1 parent 154658e commit 531c8a9
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions pkg/common/db/relation/mysql_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ import (
"gorm.io/gorm/logger"
)

const (
maxRetry = 100
)

//newMysqlGormDB Initialize the database connection
func newMysqlGormDB() (*gorm.DB, error) {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local",
config.Config.Mysql.Username, config.Config.Mysql.Password, config.Config.Mysql.Address[0], "mysql")
db, err := gorm.Open(mysql.Open(dsn), nil)

db, err := connectToDatabase(dsn, maxRetry)
if err != nil {
time.Sleep(time.Duration(30) * time.Second)
db, err = gorm.Open(mysql.Open(dsn), nil)
if err != nil {
panic(err.Error() + " open failed " + dsn)
}
panic(err.Error() + " Open failed " + dsn)
}
sqlDB, err := db.DB()
if err != nil {
Expand Down Expand Up @@ -82,7 +84,24 @@ func newMysqlGormDB() (*gorm.DB, error) {
return db, nil
}

// gorm mysql
//connectToDatabase Connection retry for mysql
func connectToDatabase(dsn string, maxRetry int) (*gorm.DB, error) {
var db *gorm.DB
var err error
for i := 0; i <= maxRetry; i++ {
db, err = gorm.Open(mysql.Open(dsn), nil)
if err == nil {
return db, nil
}
if mysqlErr, ok := err.(*mysqlDriver.MySQLError); ok && mysqlErr.Number == 1045 {
return nil, err
}
time.Sleep(time.Duration(1) * time.Second)
}
return nil, err
}

// NewGormDB gorm mysql
func NewGormDB() (*gorm.DB, error) {
specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound)
specialerror.AddErrHandler(replaceDuplicateKey)
Expand Down

0 comments on commit 531c8a9

Please sign in to comment.