From 2435c01253fc789b834f7d9ab8e3ab9fc4679c52 Mon Sep 17 00:00:00 2001 From: Alexandros Filios Date: Thu, 16 Jan 2025 18:15:50 +0100 Subject: [PATCH] fixup! Fix default db opts Signed-off-by: Alexandros Filios --- .../services/db/driver/sql/common/utils.go | 5 ++++ .../db/driver/sql/sqlite/persistence.go | 25 +++++++++++++++---- .../db/driver/sql/sqlite/unversioned.go | 4 +-- .../db/driver/sql/sqlite/versioned.go | 4 +-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/platform/view/services/db/driver/sql/common/utils.go b/platform/view/services/db/driver/sql/common/utils.go index 18c39dcac..54ca8a716 100644 --- a/platform/view/services/db/driver/sql/common/utils.go +++ b/platform/view/services/db/driver/sql/common/utils.go @@ -104,6 +104,11 @@ func GetOpts(config driver.Config, optsKey string) (*Opts, error) { return &opts, nil } +func CopyPtr[T any](t T) *T { + v := t + return &v +} + func notSetError(key string) error { return fmt.Errorf( "either %s in core.yaml or the %s environment variable must be specified", key, diff --git a/platform/view/services/db/driver/sql/sqlite/persistence.go b/platform/view/services/db/driver/sql/sqlite/persistence.go index 0457a4a69..ebbccc29f 100644 --- a/platform/view/services/db/driver/sql/sqlite/persistence.go +++ b/platform/view/services/db/driver/sql/sqlite/persistence.go @@ -16,6 +16,14 @@ import ( "time" "github.com/hyperledger-labs/fabric-smart-client/platform/common/services/logging" + "github.com/hyperledger-labs/fabric-smart-client/platform/view/services/db/driver/sql/common" +) + +const ( + MaxIdleConnsWrite = 1 + MaxIdleTimeWrite time.Duration = 0 + DefaultMaxIdleConns = 2 + DefaultMaxIdleTime = time.Minute ) const sqlitePragmas = ` @@ -30,34 +38,41 @@ const driverName = "sqlite" var logger = logging.MustGetLogger("view-sdk.db.sqlite") -func openDB(dataSourceName string, maxOpenConns, maxIdleConns int, maxIdleTime time.Duration, skipPragmas bool) (*sql.DB, *sql.DB, error) { +func openDB(dataSourceName string, maxOpenConns int, maxIdleConns *int, maxIdleTime *time.Duration, skipPragmas bool) (*sql.DB, *sql.DB, error) { logger.Infof("Opening read db [%v]", dataSourceName) readDB, err := OpenDB(dataSourceName, maxOpenConns, maxIdleConns, maxIdleTime, skipPragmas) if err != nil { return nil, nil, fmt.Errorf("can't open read %s database: %w", driverName, err) } logger.Infof("Opening write db [%v]", dataSourceName) - writeDB, err := OpenDB(dataSourceName, 1, 1, 0, skipPragmas) + writeDB, err := OpenDB(dataSourceName, 1, common.CopyPtr(MaxIdleConnsWrite), common.CopyPtr(MaxIdleTimeWrite), skipPragmas) if err != nil { return nil, nil, fmt.Errorf("can't open write %s database: %w", driverName, err) } return readDB, writeDB, nil } -func OpenDB(dataSourceName string, maxOpenConns, maxIdleConns int, maxIdleTime time.Duration, skipPragmas bool) (*sql.DB, error) { +func OpenDB(dataSourceName string, maxOpenConns int, maxIdleConns *int, maxIdleTime *time.Duration, skipPragmas bool) (*sql.DB, error) { // Create directories if they do not exist to avoid error "out of memory (14)", see below path := getDir(dataSourceName) if err := os.MkdirAll(path, 0777); err != nil { logger.Warnf("failed creating dir [%s]: %s", path, err) } + if maxIdleConns == nil { + maxIdleConns = common.CopyPtr(DefaultMaxIdleConns) + } + if maxIdleTime == nil { + maxIdleTime = common.CopyPtr(DefaultMaxIdleTime) + } + db, err := sql.Open(driverName, dataSourceName) if err != nil { return nil, fmt.Errorf("can't open %s database: %w", driverName, err) } db.SetMaxOpenConns(maxOpenConns) - db.SetMaxIdleConns(maxIdleConns) - db.SetConnMaxIdleTime(maxIdleTime) + db.SetMaxIdleConns(*maxIdleConns) + db.SetConnMaxIdleTime(*maxIdleTime) if err = db.Ping(); err != nil && strings.Contains(err.Error(), "out of memory (14)") { return nil, fmt.Errorf("can't open %s database, does the folder exist?", driverName) diff --git a/platform/view/services/db/driver/sql/sqlite/unversioned.go b/platform/view/services/db/driver/sql/sqlite/unversioned.go index 749cdb43c..688806290 100644 --- a/platform/view/services/db/driver/sql/sqlite/unversioned.go +++ b/platform/view/services/db/driver/sql/sqlite/unversioned.go @@ -20,7 +20,7 @@ type UnversionedPersistence struct { } func NewUnversioned(opts common.Opts, table string) (*UnversionedPersistence, error) { - readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, *opts.MaxIdleConns, *opts.MaxIdleTime, opts.SkipPragmas) + readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, opts.MaxIdleConns, opts.MaxIdleTime, opts.SkipPragmas) if err != nil { return nil, fmt.Errorf("error opening db: %w", err) } @@ -28,7 +28,7 @@ func NewUnversioned(opts common.Opts, table string) (*UnversionedPersistence, er } func NewUnversionedNotifier(opts common.Opts, table string) (*notifier.UnversionedPersistenceNotifier[*UnversionedPersistence], error) { - readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, *opts.MaxIdleConns, *opts.MaxIdleTime, opts.SkipPragmas) + readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, opts.MaxIdleConns, opts.MaxIdleTime, opts.SkipPragmas) if err != nil { return nil, fmt.Errorf("error opening db: %w", err) } diff --git a/platform/view/services/db/driver/sql/sqlite/versioned.go b/platform/view/services/db/driver/sql/sqlite/versioned.go index 700dd1a41..9c8d3b641 100644 --- a/platform/view/services/db/driver/sql/sqlite/versioned.go +++ b/platform/view/services/db/driver/sql/sqlite/versioned.go @@ -20,7 +20,7 @@ type VersionedPersistence struct { } func NewVersioned(opts common.Opts, table string) (*VersionedPersistence, error) { - readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, *opts.MaxIdleConns, *opts.MaxIdleTime, opts.SkipPragmas) + readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, opts.MaxIdleConns, opts.MaxIdleTime, opts.SkipPragmas) if err != nil { return nil, fmt.Errorf("error opening db: %w", err) } @@ -28,7 +28,7 @@ func NewVersioned(opts common.Opts, table string) (*VersionedPersistence, error) } func NewVersionedNotifier(opts common.Opts, table string) (*notifier.VersionedPersistenceNotifier[*VersionedPersistence], error) { - readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, *opts.MaxIdleConns, *opts.MaxIdleTime, opts.SkipPragmas) + readDB, writeDB, err := openDB(opts.DataSource, opts.MaxOpenConns, opts.MaxIdleConns, opts.MaxIdleTime, opts.SkipPragmas) if err != nil { return nil, fmt.Errorf("error opening db: %w", err) }