Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions internal/datastore/embedmd/postgres/connect.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package postgres

import (
"fmt"
"time"

"github.com/golang/glog"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)

const (
// postgresMaxRetries is the maximum number of attempts to retry PostgreSQL connection.
postgresMaxRetries = 25 // 25 attempts with incremental backoff (1s, 2s, 3s, ..., 25s) it's ~5 minutes
)

type PostgresDBConnector struct {
Expand All @@ -18,17 +27,34 @@ func NewPostgresDBConnector(dsn string) *PostgresDBConnector {
}

func (c *PostgresDBConnector) Connect() (*gorm.DB, error) {
var db *gorm.DB
var err error

glog.V(2).Infof("Attempting to connect with DSN: %q", c.DSN)
db, err := gorm.Open(postgres.Open(c.DSN), &gorm.Config{})

for i := range postgresMaxRetries {
db, err = gorm.Open(postgres.Open(c.DSN), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
TranslateError: true,
})
if err == nil {
break
}

glog.Warningf("Retrying connection to PostgreSQL (attempt %d/%d): %v", i+1, postgresMaxRetries, err)

time.Sleep(time.Duration(i+1) * time.Second)
}

if err != nil {
return nil, err
return nil, fmt.Errorf("failed to connect to PostgreSQL: %w", err)
}

c.db = db
glog.Info("Successfully connected to PostgreSQL database")
return db, nil
}
}

func (c *PostgresDBConnector) DB() *gorm.DB {
return c.db
}
}
Loading