From a4903fd9da6d09fd1fa50d9f7ba898746e897bd5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Manuel=20He=C3=9F?= <hessm@t-systems.com>
Date: Tue, 6 Feb 2024 12:00:31 +0100
Subject: [PATCH] add retry for connection check

---
 go.mod                        |  1 +
 go.sum                        |  2 ++
 internal/database/postgres.go | 20 ++++++++++++++++++--
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/go.mod b/go.mod
index 4528404..a4bba83 100644
--- a/go.mod
+++ b/go.mod
@@ -6,6 +6,7 @@ require (
 	github.com/cloudevents/sdk-go/v2 v2.14.0
 	github.com/gin-gonic/gin v1.9.1
 	github.com/jackc/pgx/v5 v5.5.2
+	github.com/sethvargo/go-retry v0.2.4
 	github.com/sirupsen/logrus v1.9.3
 	github.com/spf13/viper v1.18.2
 	github.com/stretchr/testify v1.8.4
diff --git a/go.sum b/go.sum
index 894cad1..9e5e2ca 100644
--- a/go.sum
+++ b/go.sum
@@ -150,6 +150,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
 github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
 github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
 github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
+github.com/sethvargo/go-retry v0.2.4 h1:T+jHEQy/zKJf5s95UkguisicE0zuF9y7+/vgz08Ocec=
+github.com/sethvargo/go-retry v0.2.4/go.mod h1:1afjQuvh7s4gflMObvjLPaWgluLLyhA1wmVZ6KLpICw=
 github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
 github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
 github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
diff --git a/internal/database/postgres.go b/internal/database/postgres.go
index 30baa13..ab1d061 100644
--- a/internal/database/postgres.go
+++ b/internal/database/postgres.go
@@ -6,8 +6,11 @@ import (
 	"fmt"
 	"github.com/jackc/pgx/v5"
 	"github.com/jackc/pgx/v5/pgxpool"
+	"github.com/sethvargo/go-retry"
+	log "github.com/sirupsen/logrus"
 	entity2 "gitlab.eclipse.org/eclipse/xfsc/organisational-credential-manager-w-stack/status-list-service/internal/entity"
 	"regexp"
+	"time"
 )
 
 // TODO: queries as constants?
@@ -24,8 +27,21 @@ func newPostgresConnection(username string, password string, host string, port i
 		return nil, err
 	}
 
-	if err = conn.Ping(context.Background()); err != nil {
-		return nil, fmt.Errorf("connection check failed: %w", err)
+	bf := retry.NewFibonacci(time.Millisecond * 500)
+	bf = retry.WithCappedDuration(time.Second*30, bf)
+	bf = retry.WithJitter(time.Millisecond*50, bf)
+	bf = retry.WithMaxDuration(time.Minute*2, bf)
+
+	if err = retry.Do(context.Background(), bf, func(_ context.Context) error {
+		if err = conn.Ping(context.Background()); err != nil {
+			err = fmt.Errorf("connection check failed: %w", err)
+			log.Error(err)
+			err = retry.RetryableError(err)
+			return err
+		}
+		return nil
+	}); err != nil {
+		return nil, err
 	}
 
 	return &postgresConnection{
-- 
GitLab