fix: add retry with backoff to initial database connection

This commit is contained in:
Jordan Knott 2020-08-28 14:22:24 -05:00
parent dd50baa05a
commit 4988176220

View File

@ -36,9 +36,18 @@ func newWebCmd() *cobra.Command {
viper.GetString("database.host"), viper.GetString("database.host"),
viper.GetString("database.name"), viper.GetString("database.name"),
) )
db, err := sqlx.Connect("postgres", connection) var db *sqlx.DB
if err != nil { var err error
log.Panic(err) retryNumber := 0
for i := 0; retryNumber <= 3; i++ {
retryNumber++
db, err = sqlx.Connect("postgres", connection)
if err == nil {
break
}
retryDuration := time.Duration(i*2) * time.Second
log.WithFields(log.Fields{"retryNumber": retryNumber, "retryDuration": retryDuration}).WithError(err).Error("issue connecting to database, retrying")
time.Sleep(retryDuration)
} }
db.SetMaxOpenConns(25) db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25) db.SetMaxIdleConns(25)