Skip to content

Commit

Permalink
fixup! Fix default db opts
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandros Filios <[email protected]>
  • Loading branch information
alexandrosfilios committed Jan 16, 2025
1 parent 93789ec commit 2435c01
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
5 changes: 5 additions & 0 deletions platform/view/services/db/driver/sql/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 20 additions & 5 deletions platform/view/services/db/driver/sql/sqlite/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions platform/view/services/db/driver/sql/sqlite/unversioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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)
}
return newUnversioned(readDB, writeDB, table), nil
}

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)
}
Expand Down
4 changes: 2 additions & 2 deletions platform/view/services/db/driver/sql/sqlite/versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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)
}
return newVersioned(readDB, writeDB, table), nil
}

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)
}
Expand Down

0 comments on commit 2435c01

Please sign in to comment.