diff --git a/go.mod b/go.mod index 4528404861039c594452b924ba07f29e44f42d59..a4bba839014fd3058f9564f8ba89d7031b365d96 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 894cad1a5d2c2fa10db1dce3063de77e568f9266..9e5e2ca067503860c6eba96025362990d9f03e43 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 30baa13299bc654b434224f2dc2c7e7c6c63b723..ab1d061e93e62196adf068cfac6849de0cb3c04f 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{