Merge branch 'v2.0.0' into v2/multi-database-support

This commit is contained in:
viktorstrate
2021-01-17 12:21:32 +01:00
9 changed files with 60 additions and 38 deletions

View File

@@ -1,10 +1,12 @@
package database
import (
"context"
"log"
"net/url"
"os"
"strings"
"time"
"github.com/photoview/photoview/api/graphql/models"
"github.com/pkg/errors"
@@ -60,11 +62,31 @@ func SetupDatabase() (*gorm.DB, error) {
}
db, err := gorm.Open(databaseDialect, &config)
if err != nil {
return nil, errors.Wrap(err, "could not connect to database")
sqlDB, dbErr := db.DB()
if dbErr != nil {
log.Println(dbErr)
return nil, dbErr
}
// TODO: Add connection retries
sqlDB.SetMaxOpenConns(80)
if err != nil {
for retryCount := 1; retryCount <= 5; retryCount++ {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
if err := sqlDB.PingContext(ctx); err == nil {
cancel()
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 db, nil
}