Add database retry

pull/493/head
杨腾宇 1 year ago
parent b5d1adb465
commit 2b041d8f6a

@ -30,16 +30,18 @@ import (
"gorm.io/gorm/logger" "gorm.io/gorm/logger"
) )
const (
maxRetry = 100
)
//newMysqlGormDB Initialize the database connection
func newMysqlGormDB() (*gorm.DB, error) { func newMysqlGormDB() (*gorm.DB, error) {
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&loc=Local", 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") 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 { if err != nil {
time.Sleep(time.Duration(30) * time.Second) panic(err.Error() + " Open failed " + dsn)
db, err = gorm.Open(mysql.Open(dsn), nil)
if err != nil {
panic(err.Error() + " open failed " + dsn)
}
} }
sqlDB, err := db.DB() sqlDB, err := db.DB()
if err != nil { if err != nil {
@ -82,7 +84,24 @@ func newMysqlGormDB() (*gorm.DB, error) {
return db, nil 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) { func NewGormDB() (*gorm.DB, error) {
specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound) specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound)
specialerror.AddErrHandler(replaceDuplicateKey) specialerror.AddErrHandler(replaceDuplicateKey)

Loading…
Cancel
Save