Update api/database/database.go
Co-authored-by: Viktor Strate Kløvedal <viktorstrate@gmail.com>
This commit is contained in:
parent
5b7637cb3c
commit
85ce3a1018
|
@ -50,23 +50,20 @@ func SetupDatabase() (*gorm.DB, error) {
|
||||||
sqlDB.SetMaxOpenConns(80)
|
sqlDB.SetMaxOpenConns(80)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
for retryCount := 1; retryCount <= 5; retryCount++ {
|
for retryCount := 1; retryCount <= 5; retryCount++ {
|
||||||
select {
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
case <-ctx.Done():
|
|
||||||
log.Println(ctx.Err())
|
if err := sqlDB.PingContext(ctx); err == nil {
|
||||||
return nil, err
|
cancel()
|
||||||
default:
|
return db, nil
|
||||||
if err := sqlDB.PingContext(ctx); err != nil {
|
|
||||||
log.Printf("WARN: Could not ping database: %s, Will retry after 1 second", err)
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
} else {
|
|
||||||
return db, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
log.Printf("WARN: Could not ping database: %s. Will retry after 5 seconds\n", err)
|
||||||
|
time.Sleep(time.Duration(5) * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue