From 531c8a9876af3cee82111f8546735ec6cbae81e5 Mon Sep 17 00:00:00 2001 From: pluto <83957921+plutoyty@users.noreply.github.com> Date: Wed, 12 Jul 2023 10:06:30 +0800 Subject: [PATCH] Add database retry (#493) --- pkg/common/db/relation/mysql_init.go | 33 ++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/common/db/relation/mysql_init.go b/pkg/common/db/relation/mysql_init.go index 67419c7bf..fe467b675 100644 --- a/pkg/common/db/relation/mysql_init.go +++ b/pkg/common/db/relation/mysql_init.go @@ -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 { @@ -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)