From 4988176220e9b59f5fc828ea50559409e8771e01 Mon Sep 17 00:00:00 2001 From: Jordan Knott Date: Fri, 28 Aug 2020 14:22:24 -0500 Subject: [PATCH] fix: add retry with backoff to initial database connection --- internal/commands/web.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/commands/web.go b/internal/commands/web.go index 70d173c..9385480 100644 --- a/internal/commands/web.go +++ b/internal/commands/web.go @@ -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)