Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB Retry Behavior #144

Merged
merged 3 commits into from
Jan 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion internal/storage/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"fmt"
"time"

"github.com/lbryio/lbrytv/internal/monitor"

Expand All @@ -10,6 +11,8 @@ import (
"github.com/volatiletech/sqlboiler/boil"
)

const maxDBConnectAttempts = 7

// Handler implements the app database handler.
type Handler interface {
MigrateUp()
Expand Down Expand Up @@ -59,7 +62,18 @@ 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 db *sqlx.DB
for i := 0; i < maxDBConnectAttempts; i++ {
db, err = sqlx.Connect(c.dialect, dsn)
if err == nil {
break
}
secondsToWait := i + 1
c.logger.Log().Warningf("Attempt %d - could not connect to database...retry in %d seconds: %s", i, secondsToWait, err)
time.Sleep(time.Duration(secondsToWait) * time.Second)
}

if err != nil {
c.logger.LogF(monitor.F{"dsn": dsn}).Info("DB connection failed")
return err
Expand Down