From c119a2c011fb4dd39f6ba941fc75a4c6c6c63231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 23 May 2021 19:32:34 -0700 Subject: [PATCH] Add new option DATABASE_CONNECTION_LIFETIME --- cli/cli.go | 1 + config/options.go | 10 ++++++++++ config/parser.go | 2 ++ database/database.go | 4 +++- miniflux.1 | 14 ++++++++++++-- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 6a80f8d6..5f17a85d 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -132,6 +132,7 @@ func Parse() { config.Opts.DatabaseURL(), config.Opts.DatabaseMinConns(), config.Opts.DatabaseMaxConns(), + config.Opts.DatabaseConnectionLifetime(), ) if err != nil { logger.Fatal("Unable to initialize database connection pool: %v", err) diff --git a/config/options.go b/config/options.go index b676e50f..2d62fe9d 100644 --- a/config/options.go +++ b/config/options.go @@ -8,6 +8,7 @@ import ( "fmt" "sort" "strings" + "time" "miniflux.app/version" ) @@ -34,6 +35,7 @@ const ( defaultDatabaseURL = "user=postgres password=postgres dbname=miniflux2 sslmode=disable" defaultDatabaseMaxConns = 20 defaultDatabaseMinConns = 1 + defaultDatabaseConnectionLifetime = 5 defaultListenAddr = "127.0.0.1:8080" defaultCertFile = "" defaultKeyFile = "" @@ -90,6 +92,7 @@ type Options struct { databaseURL string databaseMaxConns int databaseMinConns int + databaseConnectionLifetime int runMigrations bool listenAddr string certFile string @@ -148,6 +151,7 @@ func NewOptions() *Options { databaseURL: defaultDatabaseURL, databaseMaxConns: defaultDatabaseMaxConns, databaseMinConns: defaultDatabaseMinConns, + databaseConnectionLifetime: defaultDatabaseConnectionLifetime, runMigrations: defaultRunMigrations, listenAddr: defaultListenAddr, certFile: defaultCertFile, @@ -249,6 +253,11 @@ func (o *Options) DatabaseMinConns() int { 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. func (o *Options) ListenAddr() string { return o.listenAddr @@ -484,6 +493,7 @@ func (o *Options) SortedOptions() []*Option { "CREATE_ADMIN": o.createAdmin, "DATABASE_MAX_CONNS": o.databaseMaxConns, "DATABASE_MIN_CONNS": o.databaseMinConns, + "DATABASE_CONNECTION_LIFETIME": o.databaseConnectionLifetime, "DATABASE_URL": o.databaseURL, "DEBUG": o.debug, "FETCH_YOUTUBE_WATCH_TIME": o.fetchYouTubeWatchTime, diff --git a/config/parser.go b/config/parser.go index a3f587df..cff06de7 100644 --- a/config/parser.go +++ b/config/parser.go @@ -95,6 +95,8 @@ func (p *Parser) parseLines(lines []string) (err error) { p.opts.databaseMaxConns = parseInt(value, defaultDatabaseMaxConns) case "DATABASE_MIN_CONNS": p.opts.databaseMinConns = parseInt(value, defaultDatabaseMinConns) + case "DATABASE_CONNECTION_LIFETIME": + p.opts.databaseConnectionLifetime = parseInt(value, defaultDatabaseConnectionLifetime) case "RUN_MIGRATIONS": p.opts.runMigrations = parseBool(value, defaultRunMigrations) case "DISABLE_HSTS": diff --git a/database/database.go b/database/database.go index 0c6b4121..d310e072 100644 --- a/database/database.go +++ b/database/database.go @@ -7,13 +7,14 @@ package database // import "miniflux.app/database" import ( "database/sql" "fmt" + "time" // Postgresql driver import _ "github.com/lib/pq" ) // 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) if err != nil { return nil, err @@ -21,6 +22,7 @@ func NewConnectionPool(dsn string, minConnections, maxConnections int) (*sql.DB, db.SetMaxOpenConns(maxConnections) db.SetMaxIdleConns(minConnections) + db.SetConnMaxLifetime(connectionLifetime) return db, nil } diff --git a/miniflux.1 b/miniflux.1 index 7e4b2578..ab1c2696 100644 --- a/miniflux.1 +++ b/miniflux.1 @@ -1,5 +1,5 @@ .\" Manpage for miniflux. -.TH "MINIFLUX" "1" "April 30, 2021" "\ \&" "\ \&" +.TH "MINIFLUX" "1" "May 23, 2021" "\ \&" "\ \&" .SH NAME 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 Default is empty\&. .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 Maximum number of database connections\&. .br @@ -400,7 +405,12 @@ Disabled by default\&. .B MAINTENANCE_MESSAGE Define a custom maintenance message\&. .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 .P