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
14 changes: 8 additions & 6 deletions internal/knowledge/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"log/slog"
"net/url"
"reflect"
Expand Down Expand Up @@ -102,20 +103,21 @@ func (c Connector) FromSecretRef(ctx context.Context, ref corev1.SecretReference

db, err := sql.Open("postgres", dbUrlStr)
if err != nil {
panic(err)
return nil, err
}

// If the wait time exceeds 10 seconds, we will panic.
// Retry connecting to the database for up to 10 seconds.
maxRetries := 10
for i := range maxRetries {
err := db.PingContext(ctx)
if err == nil {
lastErr := db.PingContext(ctx)
if lastErr == nil {
break
}
if i == maxRetries-1 {
panic("giving up connecting to database")
db.Close()
return nil, fmt.Errorf("giving up connecting to database: %w", lastErr)
}
slog.Error("failed to connect to database, retrying...", "error", err)
slog.Error("failed to connect to database, retrying...", "error", lastErr)
time.Sleep(1 * time.Second)
}

Expand Down
Loading