Skip to content

Commit

Permalink
Public sqlite db options: MaxOpenConns, MaxIdleConns, ConnMaxLifetime. (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
iredmail authored Jan 25, 2022
1 parent 5048d24 commit ff1db4e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
41 changes: 33 additions & 8 deletions sqlite3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ store := sqlite3.New()

// Initialize custom config
store := sqlite3.New(sqlite3.Config{
Database: "./fiber.sqlite3",
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
Database: "./fiber.sqlite3",
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: 1 * time.Second,
})
```

Expand All @@ -70,15 +73,37 @@ type Config struct {
//
// 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
}
```

### Default Config
```go
var ConfigDefault = Config{
Database: "./fiber.sqlite3",
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
Database: "./fiber.sqlite3",
Table: "fiber_storage",
Reset: false,
GCInterval: 10 * time.Second,
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: 1 * time.Second,
}
```
36 changes: 28 additions & 8 deletions sqlite3/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,24 @@ type Config struct {
// Optional. Default is 10 * time.Second
GCInterval time.Duration

////////////////////////////////////
// //////////////////////////////////
// Adaptor related config options //
////////////////////////////////////
// //////////////////////////////////

maxIdleConns int
maxOpenConns int
connMaxLifetime time.Duration
// 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
Expand All @@ -42,9 +53,9 @@ var ConfigDefault = Config{
GCInterval: 10 * time.Second,

// Adaptor related config options
maxOpenConns: 100,
maxIdleConns: 100,
connMaxLifetime: 1 * time.Second,
MaxOpenConns: 100,
MaxIdleConns: 100,
ConnMaxLifetime: 1 * time.Second,
}

// Helper function to set default values
Expand All @@ -67,5 +78,14 @@ func configDefault(config ...Config) Config {
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
}
6 changes: 3 additions & 3 deletions sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func New(config ...Config) *Storage {
}

// Set database options
db.SetMaxOpenConns(cfg.maxOpenConns)
db.SetMaxIdleConns(cfg.maxIdleConns)
db.SetConnMaxLifetime(cfg.connMaxLifetime)
db.SetMaxOpenConns(cfg.MaxOpenConns)
db.SetMaxIdleConns(cfg.MaxIdleConns)
db.SetConnMaxLifetime(cfg.ConnMaxLifetime)

// Ping database
if err := db.Ping(); err != nil {
Expand Down

0 comments on commit ff1db4e

Please sign in to comment.