Files
storage/sqlite3/config.go
iRedMail ff1db4e2a4 Public sqlite db options: MaxOpenConns, MaxIdleConns, ConnMaxLifetime. (#310)
* Public sqlite db options: MaxOpenConns, MaxIdleConns, ConnMaxLifetime.

* Update sqlite3/README.md.

* Add comment lines for the public options.

* Typo

* Set default values for MaxIdleConns, MaxOpenConns, ConnMaxLifetime.
2022-01-25 12:58:19 +01:00

92 lines
2.0 KiB
Go

package sqlite3
import "time"
// Config defines the config for storage.
type Config struct {
// Database name
//
// Optional. Default is "fiber"
Database string
// Table name
//
// Optional. Default is "fiber_storage"
Table string
// Reset clears any existing keys in existing Table
//
// Optional. Default is false
Reset bool
// Time before deleting expired keys
//
// Optional. Default is 10 * time.Second
GCInterval time.Duration
// //////////////////////////////////
// Adaptor related config options //
// //////////////////////////////////
// MaxIdleConns sets the maximum number of connections in the idle connection pool.
//
// Optional. Default is 100.
MaxIdleConns int
// MaxOpenConns sets the maximum number of open connections to the database.
//
// Optional. Default is 100.
MaxOpenConns int
// ConnMaxLifetime sets the maximum amount of time a connection may be reused.
//
// Optional. Default is 1 second.
ConnMaxLifetime time.Duration
}
// ConfigDefault is the default config
var ConfigDefault = Config{
// General config options
Database: "./fiber.sqlite3",
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
// Adaptor related config options
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: 1 * time.Second,
}
// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}
// Override default config
cfg := config[0]
// Set default values
if cfg.Database == "" {
cfg.Database = ConfigDefault.Database
}
if cfg.Table == "" {
cfg.Table = ConfigDefault.Table
}
if int(cfg.GCInterval.Seconds()) <= 0 {
cfg.GCInterval = ConfigDefault.GCInterval
}
if cfg.MaxIdleConns <= 0 {
cfg.MaxIdleConns = ConfigDefault.MaxIdleConns
}
if cfg.MaxOpenConns <= 0 {
cfg.MaxOpenConns = ConfigDefault.MaxOpenConns
}
if cfg.ConnMaxLifetime == 0 {
cfg.ConnMaxLifetime = ConfigDefault.ConnMaxLifetime
}
return cfg
}