Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

Add retries to Database connection in Ingester (#21)

patcher9922557f57f6f3
diff --git a/src/ingester/db/db.go b/src/ingester/db/db.go
index 13a8f9d2..fa3dc834 100644
--- a/src/ingester/db/db.go
+++ b/src/ingester/db/db.go
@@ -10,6 +10,7 @@ import (
"ingester/obsPlatform"
"net/http"
"sync"
+ "time"
_ "github.com/lib/pq"
"github.com/pkoukk/tiktoken-go"
@@ -62,7 +63,22 @@ type DatabaseConfig struct {
// PingDB attempts to ping the database to check if it's alive.
func PingDB() error {
- return db.Ping()
+ var attempt int
+ retryDelay := 15 * time.Second
+
+ for attempt < 5 {
+ attempt++
+ err := db.Ping()
+ if err == nil {
+ log.Info().Msg("Successfully connected to the database")
+ return nil
+ }
+
+ log.Warn().Err(err).Msgf("Failed to connect to the database on attempt %d, retrying in %s", attempt, retryDelay)
+ time.Sleep(retryDelay)
+ }
+
+ return fmt.Errorf("Failed to connect to the database after %d attempts", attempt)
}
// GenerateSecureRandomKey should generate a secure random string to be used as an API key.
@@ -197,7 +213,7 @@ func initializeDB() error {
return
}
- dbErr = db.Ping()
+ dbErr = PingDB()
if dbErr != nil {
return
}