Add new option DATABASE_CONNECTION_LIFETIME

This commit is contained in:
Frédéric Guillot 2021-05-23 19:32:34 -07:00 committed by fguillot
parent e0557d8961
commit c119a2c011
5 changed files with 28 additions and 3 deletions

View file

@ -132,6 +132,7 @@ func Parse() {
config.Opts.DatabaseURL(), config.Opts.DatabaseURL(),
config.Opts.DatabaseMinConns(), config.Opts.DatabaseMinConns(),
config.Opts.DatabaseMaxConns(), config.Opts.DatabaseMaxConns(),
config.Opts.DatabaseConnectionLifetime(),
) )
if err != nil { if err != nil {
logger.Fatal("Unable to initialize database connection pool: %v", err) logger.Fatal("Unable to initialize database connection pool: %v", err)

View file

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"sort" "sort"
"strings" "strings"
"time"
"miniflux.app/version" "miniflux.app/version"
) )
@ -34,6 +35,7 @@ const (
defaultDatabaseURL = "user=postgres password=postgres dbname=miniflux2 sslmode=disable" defaultDatabaseURL = "user=postgres password=postgres dbname=miniflux2 sslmode=disable"
defaultDatabaseMaxConns = 20 defaultDatabaseMaxConns = 20
defaultDatabaseMinConns = 1 defaultDatabaseMinConns = 1
defaultDatabaseConnectionLifetime = 5
defaultListenAddr = "127.0.0.1:8080" defaultListenAddr = "127.0.0.1:8080"
defaultCertFile = "" defaultCertFile = ""
defaultKeyFile = "" defaultKeyFile = ""
@ -90,6 +92,7 @@ type Options struct {
databaseURL string databaseURL string
databaseMaxConns int databaseMaxConns int
databaseMinConns int databaseMinConns int
databaseConnectionLifetime int
runMigrations bool runMigrations bool
listenAddr string listenAddr string
certFile string certFile string
@ -148,6 +151,7 @@ func NewOptions() *Options {
databaseURL: defaultDatabaseURL, databaseURL: defaultDatabaseURL,
databaseMaxConns: defaultDatabaseMaxConns, databaseMaxConns: defaultDatabaseMaxConns,
databaseMinConns: defaultDatabaseMinConns, databaseMinConns: defaultDatabaseMinConns,
databaseConnectionLifetime: defaultDatabaseConnectionLifetime,
runMigrations: defaultRunMigrations, runMigrations: defaultRunMigrations,
listenAddr: defaultListenAddr, listenAddr: defaultListenAddr,
certFile: defaultCertFile, certFile: defaultCertFile,
@ -249,6 +253,11 @@ func (o *Options) DatabaseMinConns() int {
return o.databaseMinConns return o.databaseMinConns
} }
// DatabaseConnectionLifetime returns the maximum amount of time a connection may be reused.
func (o *Options) DatabaseConnectionLifetime() time.Duration {
return time.Duration(o.databaseConnectionLifetime) * time.Minute
}
// ListenAddr returns the listen address for the HTTP server. // ListenAddr returns the listen address for the HTTP server.
func (o *Options) ListenAddr() string { func (o *Options) ListenAddr() string {
return o.listenAddr return o.listenAddr
@ -484,6 +493,7 @@ func (o *Options) SortedOptions() []*Option {
"CREATE_ADMIN": o.createAdmin, "CREATE_ADMIN": o.createAdmin,
"DATABASE_MAX_CONNS": o.databaseMaxConns, "DATABASE_MAX_CONNS": o.databaseMaxConns,
"DATABASE_MIN_CONNS": o.databaseMinConns, "DATABASE_MIN_CONNS": o.databaseMinConns,
"DATABASE_CONNECTION_LIFETIME": o.databaseConnectionLifetime,
"DATABASE_URL": o.databaseURL, "DATABASE_URL": o.databaseURL,
"DEBUG": o.debug, "DEBUG": o.debug,
"FETCH_YOUTUBE_WATCH_TIME": o.fetchYouTubeWatchTime, "FETCH_YOUTUBE_WATCH_TIME": o.fetchYouTubeWatchTime,

View file

@ -95,6 +95,8 @@ func (p *Parser) parseLines(lines []string) (err error) {
p.opts.databaseMaxConns = parseInt(value, defaultDatabaseMaxConns) p.opts.databaseMaxConns = parseInt(value, defaultDatabaseMaxConns)
case "DATABASE_MIN_CONNS": case "DATABASE_MIN_CONNS":
p.opts.databaseMinConns = parseInt(value, defaultDatabaseMinConns) p.opts.databaseMinConns = parseInt(value, defaultDatabaseMinConns)
case "DATABASE_CONNECTION_LIFETIME":
p.opts.databaseConnectionLifetime = parseInt(value, defaultDatabaseConnectionLifetime)
case "RUN_MIGRATIONS": case "RUN_MIGRATIONS":
p.opts.runMigrations = parseBool(value, defaultRunMigrations) p.opts.runMigrations = parseBool(value, defaultRunMigrations)
case "DISABLE_HSTS": case "DISABLE_HSTS":

View file

@ -7,13 +7,14 @@ package database // import "miniflux.app/database"
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"time"
// Postgresql driver import // Postgresql driver import
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
// NewConnectionPool configures the database connection pool. // NewConnectionPool configures the database connection pool.
func NewConnectionPool(dsn string, minConnections, maxConnections int) (*sql.DB, error) { func NewConnectionPool(dsn string, minConnections, maxConnections int, connectionLifetime time.Duration) (*sql.DB, error) {
db, err := sql.Open("postgres", dsn) db, err := sql.Open("postgres", dsn)
if err != nil { if err != nil {
return nil, err return nil, err
@ -21,6 +22,7 @@ func NewConnectionPool(dsn string, minConnections, maxConnections int) (*sql.DB,
db.SetMaxOpenConns(maxConnections) db.SetMaxOpenConns(maxConnections)
db.SetMaxIdleConns(minConnections) db.SetMaxIdleConns(minConnections)
db.SetConnMaxLifetime(connectionLifetime)
return db, nil return db, nil
} }

View file

@ -1,5 +1,5 @@
.\" Manpage for miniflux. .\" Manpage for miniflux.
.TH "MINIFLUX" "1" "April 30, 2021" "\ \&" "\ \&" .TH "MINIFLUX" "1" "May 23, 2021" "\ \&" "\ \&"
.SH NAME .SH NAME
miniflux \- Minimalist and opinionated feed reader miniflux \- Minimalist and opinionated feed reader
@ -174,6 +174,11 @@ Path to a secret key exposed as a file, it should contain $DATABASE_URL value\&.
.br .br
Default is empty\&. Default is empty\&.
.TP .TP
.B DATABASE_CONNECTION_LIFETIME
Set the maximum amount of time a connection may be reused\&.
.br
Default is 5 minutes\&.
.TP
.B DATABASE_MAX_CONNS .B DATABASE_MAX_CONNS
Maximum number of database connections\&. Maximum number of database connections\&.
.br .br
@ -400,7 +405,12 @@ Disabled by default\&.
.B MAINTENANCE_MESSAGE .B MAINTENANCE_MESSAGE
Define a custom maintenance message\&. Define a custom maintenance message\&.
.br .br
Default is "Miniflux is currently under maintenance". Default is "Miniflux is currently under maintenance"\&.
.TP
.B WATCHDOG
Enable or disable Systemd watchdog\&.
.br
Enabled by default\&.
.SH AUTHORS .SH AUTHORS
.P .P