mirror of
https://github.com/knadh/listmonk.git
synced 2024-12-27 17:37:57 +08:00
feat: Set default values for DB connection parameters
- Configures `max_open` and `max_idle` in default configs to `25`. This changes the previous behaviour of connection pooling where both the values were unset (from default config) and causes unbounded connection limit and no connection reuse. - Configures `db.SetConnMaxLifetime` which sets the maximum time the connection can be reused in a pool. - Sets `max_conn_lifetime` in default config as `5 minutes`. Closes https://github.com/knadh/listmonk/issues/225
This commit is contained in:
parent
c60cc3525f
commit
ad8f290dad
3 changed files with 17 additions and 8 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/lib/pq"
|
||||
|
@ -84,14 +85,15 @@ type Queries struct {
|
|||
|
||||
// dbConf contains database config required for connecting to a DB.
|
||||
type dbConf struct {
|
||||
Host string `koanf:"host"`
|
||||
Port int `koanf:"port"`
|
||||
User string `koanf:"user"`
|
||||
Password string `koanf:"password"`
|
||||
DBName string `koanf:"database"`
|
||||
SSLMode string `koanf:"ssl_mode"`
|
||||
MaxOpen int `koanf:"max_open"`
|
||||
MaxIdle int `koanf:"max_idle"`
|
||||
Host string `koanf:"host"`
|
||||
Port int `koanf:"port"`
|
||||
User string `koanf:"user"`
|
||||
Password string `koanf:"password"`
|
||||
DBName string `koanf:"database"`
|
||||
SSLMode string `koanf:"ssl_mode"`
|
||||
MaxOpen int `koanf:"max_open"`
|
||||
MaxIdle int `koanf:"max_idle"`
|
||||
MaxLifetime time.Duration `koanf:"max_lifetime"`
|
||||
}
|
||||
|
||||
// connectDB initializes a database connection.
|
||||
|
@ -104,6 +106,7 @@ func connectDB(c dbConf) (*sqlx.DB, error) {
|
|||
}
|
||||
db.SetMaxOpenConns(c.MaxOpen)
|
||||
db.SetMaxIdleConns(c.MaxIdle)
|
||||
db.SetConnMaxLifetime(c.MaxLifetime)
|
||||
return db, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -10,3 +10,6 @@
|
|||
password = "listmonk"
|
||||
database = "listmonk"
|
||||
ssl_mode = "disable"
|
||||
max_open = 25
|
||||
max_idle = 25
|
||||
max_lifetime = "300s"
|
||||
|
|
|
@ -17,3 +17,6 @@
|
|||
password = "listmonk"
|
||||
database = "listmonk"
|
||||
ssl_mode = "disable"
|
||||
max_open = 25
|
||||
max_idle = 25
|
||||
max_lifetime = "300s"
|
||||
|
|
Loading…
Reference in a new issue