Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion cli/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func doSetup() int {
fmt.Println(" Pinging db..")

store := factory.CreateStore()
defer store.Close("setup")
store.Connect("setup")
defer store.Close("setup")

fmt.Println("Running db Migrations..")
if err := db.Migrate(store, nil); err != nil {
Expand Down
59 changes: 44 additions & 15 deletions db/sql/SqlDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,60 @@ type SqlDb struct {
connection SqlDbConnection
}

const (
connectMaxAttempts = 12
connectRetryBackoff = 5 * time.Second
)

func (d *SqlDb) Sql() *gorp.DbMap {
return d.connection.sql
}

func (d *SqlDbConnection) Connect() {
sqlDb, err := connect()
if err != nil {
panic(err)
}
var (
sqlDb *sql.DB
err error
lastErr error
)

err = sqlDb.Ping()
if err != nil {
if err = sqlDb.Close(); err != nil {
log.Warn("Cannot close database connection: " + err.Error())
for i := 0; i < connectMaxAttempts; i++ {
sqlDb, err = connect()
if err == nil {
err = sqlDb.Ping()
}

if err = createDb(); err != nil {
panic(err)
if err == nil {
break
}

sqlDb, err = connect()
if err != nil {
panic(err)
lastErr = err

if sqlDb != nil {
if closeErr := sqlDb.Close(); closeErr != nil {
log.Warn("Cannot close database connection: " + closeErr.Error())
}
}

if err = sqlDb.Ping(); err != nil {
panic(err)
if createErr := createDb(); createErr != nil {
log.Warn("Cannot create database: " + createErr.Error())
}

if i < connectMaxAttempts-1 {
log.Warnf(
"Cannot connect to database (attempt %d/%d): %v. Retrying in %s",
i+1,
connectMaxAttempts,
lastErr,
connectRetryBackoff,
)
time.Sleep(connectRetryBackoff)
}
}

if lastErr != nil {
panic(fmt.Errorf("cannot connect to database after %d attempts: %w", connectMaxAttempts, lastErr))
Comment on lines +84 to +85
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Check current error state before panicking after retries

The retry loop breaks on a successful connect()+Ping, but lastErr is never cleared, so any earlier transient failure causes this panic path to execute even after a later successful attempt. In a normal startup race (e.g., DB ready on attempt 2+), setup/runtime will still fail after the loop despite having a valid connection. This regresses the intended retry behavior; the post-loop failure check should be based on the final err (or reset lastErr on success).

Useful? React with 👍 / 👎.

}

cfg, err := util.Config.GetDBConfig()
if err != nil {
panic(err)
Expand Down Expand Up @@ -105,10 +129,15 @@ func (d *SqlDbConnection) Connect() {
}

func (d *SqlDbConnection) Close() {
if d.sql == nil || d.sql.Db == nil {
return
}
err := d.sql.Db.Close()
if err != nil {
panic(err)
}

d.sql = nil
}

func CreateTestStore() *SqlDb {
Expand Down
6 changes: 6 additions & 0 deletions db/sql/SqlDb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ func TestValidatePort(t *testing.T) {
t.Error("invalid postgres query")
}
}

func TestCloseWithoutConnect(t *testing.T) {
d := SqlDb{}

d.connection.Close()
}
Loading