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.name"),
)
db, err := sqlx.Connect("postgres", connection)
if err != nil {
log.Panic(err)
var db *sqlx.DB
var err error
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.SetMaxIdleConns(25)