Don't just error on db connections. DBs lose connections, it happens. Retry periodically, with increasing intervals before finally returning an error to connect to the database.
@@ -2,6 +2,7 @@ package storage
import (
"fmt"
+ "time"
"github.com/lbryio/lbrytv/internal/monitor"
@@ -10,6 +11,8 @@ import (
"github.com/volatiletech/sqlboiler/boil"
)
+const maxDBConnectAttempts = 10
+
// Handler implements the app database handler.
type Handler interface {
MigrateUp()
@@ -59,7 +62,19 @@ func InitConn(params ConnParams) *Connection {
func (c *Connection) Connect() error {
dsn := MakeDSN(c.params)
c.logger.LogF(monitor.F{"dsn": dsn}).Info("connecting to the DB")
- db, err := sqlx.Connect(c.dialect, dsn)
+ var err error
+ var secondsToWait int
+ var db *sqlx.DB
+ for i := 0; i < maxDBConnectAttempts; i++ {
+ db, err = sqlx.Connect(c.dialect, dsn)
+ if err == nil {
+ break
+ }
+ secondsToWait = secondsToWait + i + 1
+ c.logger.Log().Warningf("Attempt %d - could not connect to database...retry in %d seconds", i, secondsToWait)
+ time.Sleep(time.Duration(secondsToWait) * time.Second)
+ }
+
if err != nil {
c.logger.LogF(monitor.F{"dsn": dsn}).Info("DB connection failed")
return err
