Add DB connection retries

This commit is contained in:
Heather Huggins 2016-09-23 11:15:07 -07:00
parent 9f40327562
commit a5e3ff8023
37 changed files with 74 additions and 9 deletions

View File

@ -98,12 +98,37 @@ func (pgSQL *pgSQL) Ping() bool {
func Open(config *config.DatabaseConfig) (database.Datastore, error) {
// Run migrations.
if err := migrate(config.Source); err != nil {
try.Do(func(attempt int) (retry bool, err error) {
retry = attempt < 5 // try 5 times
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("panic: %v", r))
}
}()
value, err = SomeFunction()
return
})
}
if err != nil {
log.Error(err)
return nil, database.ErrCantOpen
}
// Open database.
db, err := sql.Open("postgres", config.Source)
if err != nil {
try.Do(func(attempt int) (retry bool, err error) {
retry = attempt < 5 // try 5 times
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("panic: %v", r))
}
}()
value, err = SomeFunction()
return
})
}
if err != nil {
log.Error(err)
return nil, database.ErrCantOpen
@ -220,12 +245,36 @@ func OpenForTest(name string, withTestData bool) (*pgSQLTest, error) {
// Create database.
if err := createDatabase(dataSourceDefaultDatabase, dbName); err != nil {
try.Do(func(attempt int) (retry bool, err error) {
retry = attempt < 5 // try 5 times
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("panic: %v", r))
}
}()
value, err = SomeFunction()
return
})
}
if err != nil {
log.Error(err)
return nil, database.ErrCantOpen
}
// Open database.
db, err := Open(&config.DatabaseConfig{Source: dataSourceTestDatabase, CacheSize: 0})
try.Do(func(attempt int) (retry bool, err error) {
retry = attempt < 5 // try 5 times
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("panic: %v", r))
}
}()
value, err = SomeFunction()
return
})
}
if err != nil {
dropDatabase(dataSourceDefaultDatabase, dbName)
log.Error(err)
@ -237,12 +286,21 @@ func OpenForTest(name string, withTestData bool) (*pgSQLTest, error) {
_, filename, _, _ := runtime.Caller(0)
d, _ := ioutil.ReadFile(path.Join(path.Dir(filename)) + "/testdata/data.sql")
_, err = db.(*pgSQL).Exec(string(d))
try.Do(func(attempt int) (retry bool, err error) {
retry = attempt < 5 // try 5 times
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("panic: %v", r))
}
}()
value, err = SomeFunction()
return
})
}
if err != nil {
dropDatabase(dataSourceDefaultDatabase, dbName)
log.Error(err)
return nil, database.ErrCantOpen
}
}
return &pgSQLTest{
pgSQL: db.(*pgSQL),

@ -0,0 +1 @@
Subproject commit 97347ec44d927c854b93d6722964aa459e42a106

@ -0,0 +1 @@
Subproject commit a63dfec412dc3675ec36888f359f8a1e5c3f740d

@ -0,0 +1 @@
Subproject commit 3ac0863d7acf3bc44daf49afef8919af12f704ef

@ -0,0 +1 @@
Subproject commit 87d4004f2ab62d0d255e0a38f1680aa534549fe3

@ -0,0 +1 @@
Subproject commit 7cafcd837844e784b526369c9bce262804aebc60

@ -0,0 +1 @@
Subproject commit ed8eb9e318d7a84ce5915b495b7d35e0cfe7b5a8

@ -0,0 +1 @@
Subproject commit 66b8e73f3f5cda9f96b69efd03dd3d7fc4a5cdb8