mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 20:39:08 +00:00
Merge branch 'v2.0.0' into v2/multi-database-support
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user