From ca17ac8c91a34e2c3b81e549498d35526d9aed47 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Fri, 9 Dec 2022 23:08:18 -0400 Subject: [PATCH] feat: allow passing params to db url and connection pool params --- waku/node.go | 8 +++--- waku/persistence/sqlite/sqlite.go | 9 ++++++- waku/persistence/store.go | 42 +++++++++++++------------------ 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/waku/node.go b/waku/node.go index 2f772ff5..3662623a 100644 --- a/waku/node.go +++ b/waku/node.go @@ -80,9 +80,9 @@ func freePort() (int, error) { } func validateDBUrl(val string) error { - matched, err := regexp.Match(`^[\w\+]+:\/\/[\w\/\\\.\:\@]+$`, []byte(val)) + matched, err := regexp.Match(`^[\w\+]+:\/\/[\w\/\\\.\:\@]+\?.*$`, []byte(val)) if !matched || err != nil { - return errors.New("invalid 'db url' option format") + return errors.New("invalid db url option format") } return nil } @@ -130,7 +130,7 @@ func Execute(options Options) { dbEngine := dbURLParts[0] dbParams := dbURLParts[1] switch dbEngine { - case "sqlite": + case "sqlite3": db, err = sqlite.NewDB(dbParams) failOnErr(err, "Could not connect to DB") logger.Info("using database: ", zap.String("path", dbParams)) @@ -146,7 +146,7 @@ func Execute(options Options) { if options.Metrics.Enable { metricsServer = metrics.NewMetricsServer(options.Metrics.Address, options.Metrics.Port, logger) go metricsServer.Start() - wmetrics.RecordVersion(context.Background(), node.Version, node.GitCommit) + wmetrics.RecordVersion(ctx, node.Version, node.GitCommit) } nodeOpts := []node.WakuNodeOption{ diff --git a/waku/persistence/sqlite/sqlite.go b/waku/persistence/sqlite/sqlite.go index 3185defa..b80c59c4 100644 --- a/waku/persistence/sqlite/sqlite.go +++ b/waku/persistence/sqlite/sqlite.go @@ -87,7 +87,10 @@ func (q Queries) GetSize() string { // WithDB is a DBOption that lets you use a sqlite3 DBStore. func WithDB(path string) persistence.DBOption { - return persistence.WithDriver("sqlite3", path) + return persistence.WithDriver("sqlite3", path, persistence.ConnectionPoolOptions{ + // Disable concurrent access as not supported by the driver + MaxOpenConnections: 1, + }) } // NewDB creates a sqlite3 DB in the specified path @@ -96,6 +99,10 @@ func NewDB(path string) (*sql.DB, error) { if err != nil { return nil, err } + + // Disable concurrent access as not supported by the driver + db.SetMaxOpenConns(1) + return db, nil } diff --git a/waku/persistence/store.go b/waku/persistence/store.go index dc922cae..5958af85 100644 --- a/waku/persistence/store.go +++ b/waku/persistence/store.go @@ -64,13 +64,28 @@ func WithDB(db *sql.DB) DBOption { } } +type ConnectionPoolOptions struct { + MaxOpenConnections int + MaxIdleConnections int + ConnectionMaxLifetime time.Duration + ConnectionMaxIdleTime time.Duration +} + // WithDriver is a DBOption that will open a *sql.DB connection -func WithDriver(driverName string, datasourceName string) DBOption { +func WithDriver(driverName string, datasourceName string, connectionPoolOptions ...ConnectionPoolOptions) DBOption { return func(d *DBStore) error { db, err := sql.Open(driverName, datasourceName) if err != nil { return err } + + if len(connectionPoolOptions) != 0 { + db.SetConnMaxIdleTime(connectionPoolOptions[0].ConnectionMaxIdleTime) + db.SetConnMaxLifetime(connectionPoolOptions[0].ConnectionMaxLifetime) + db.SetMaxIdleConns(connectionPoolOptions[0].MaxIdleConnections) + db.SetMaxOpenConns(connectionPoolOptions[0].MaxOpenConnections) + } + d.db = db return nil } @@ -119,31 +134,8 @@ func NewDBStore(log *zap.Logger, options ...DBOption) (*DBStore, error) { } } - // Disable concurrent access as not supported by the driver - result.db.SetMaxOpenConns(1) - - var seq string - var name string - var file string // file will be empty if DB is :memory" - err := result.db.QueryRow("PRAGMA database_list").Scan(&seq, &name, &file) - if err != nil { - return nil, err - } - - // readers do not block writers and faster i/o operations - // https://www.sqlite.org/draft/wal.html - // must be set after db is encrypted - var mode string - err = result.db.QueryRow("PRAGMA journal_mode=WAL").Scan(&mode) - if err != nil { - return nil, err - } - if mode != WALMode && file != "" { - return nil, fmt.Errorf("unable to set journal_mode to WAL. actual mode %s", mode) - } - if result.enableMigrations { - err = migrations.Migrate(result.db) + err := migrations.Migrate(result.db) if err != nil { return nil, err }