feat: allow passing params to db url and connection pool params

This commit is contained in:
Richard Ramos 2022-12-09 23:08:18 -04:00 committed by RichΛrd
parent ff10795e28
commit ca17ac8c91
3 changed files with 29 additions and 30 deletions

View File

@ -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{

View File

@ -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
}

View File

@ -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
}