Add option to set DB connection max life time in seconds and document it.

This commit is contained in:
Ignacio Gómez 2022-03-11 00:08:46 -03:00
parent 7f0d2e90f7
commit 2e45b3b8f5
No known key found for this signature in database
GPG Key ID: 15A77C6BEC604B06
5 changed files with 37 additions and 5 deletions

View File

@ -599,6 +599,7 @@ The following `auth_opt_` options are supported:
| pg_sslkey | | N | SSL/TLS Client Cert. Key |
| pg_sslrootcert | | N | SSL/TLS Root Cert |
| pg_connect_tries | -1 | N | x < 0: try forever, x > 0: try x times |
| pg_max_life_time | | N | connection max life time in seconds |
Depending on the sslmode given, sslcert, sslkey and sslrootcert will be used. Options for sslmode are:
@ -744,6 +745,7 @@ Supported options for `mysql` are:
| mysql_protocol | tcp | N | Connection protocol |
| mysql_socket | | N | Unix socket path |
| mysql_connect_tries | -1 | N | x < 0: try forever, x > 0: try x times |
| mysql_max_life_time | | N | connection max life time on seconds |
Finally, placeholders for mysql differ from those of postgres, changing from $1, $2, etc., to simply ?. These are some **example** queries for `mysql`:
@ -825,6 +827,7 @@ The following `auth_opt_` options are supported:
| sqlite_superquery | | N | SQL for superusers |
| sqlite_aclquery | | N | SQL for ACLs |
| sqlite_connect_tries | -1 | N | x < 0: try forever, x > 0: try x times |
| sqlite_max_life_time | | N | connection max life time in seconds |
SQLite3 allows to connect to an in-memory db, or a single file one, so source maybe `memory` (not :memory:) or the path to a file db.

View File

@ -12,7 +12,7 @@ import (
// OpenDatabase opens the database and performs a ping to make sure the
// database is up.
// Taken from brocaar's lora-app-server: https://github.com/brocaar/lora-app-server
func OpenDatabase(dsn, engine string, tries int) (*sqlx.DB, error) {
func OpenDatabase(dsn, engine string, tries int, maxLifeTime int64) (*sqlx.DB, error) {
db, err := sqlx.Open(engine, dsn)
if err != nil {
@ -41,7 +41,9 @@ func OpenDatabase(dsn, engine string, tries int) (*sqlx.DB, error) {
return nil, fmt.Errorf("couldn't ping database %s: %s", engine, err)
}
db.SetConnMaxLifetime(time.Minute)
if maxLifeTime > 0 {
db.SetConnMaxLifetime(time.Duration(maxLifeTime) * time.Second)
}
return db, nil
}

View File

@ -36,6 +36,7 @@ type Mysql struct {
SocketPath string
AllowNativePasswords bool
hasher hashing.HashComparer
maxLifeTime int64
connectTries int
}
@ -204,8 +205,16 @@ func NewMysql(authOpts map[string]string, logLevel log.Level, hasher hashing.Has
}
}
if maxLifeTime, ok := authOpts["mysql_max_life_time"]; ok {
lifeTime, err := strconv.ParseInt(maxLifeTime, 10, 64)
if err == nil {
mysql.maxLifeTime = lifeTime
}
}
var err error
mysql.DB, err = OpenDatabase(msConfig.FormatDSN(), "mysql", mysql.connectTries)
mysql.DB, err = OpenDatabase(msConfig.FormatDSN(), "mysql", mysql.connectTries, mysql.maxLifeTime)
if err != nil {
return mysql, errors.Errorf("MySql backend error: couldn't open db: %s", err)

View File

@ -30,6 +30,7 @@ type Postgres struct {
SSLKey string
SSLRootCert string
hasher hashing.HashComparer
maxLifeTime int64
connectTries int
}
@ -149,8 +150,16 @@ func NewPostgres(authOpts map[string]string, logLevel log.Level, hasher hashing.
}
}
if maxLifeTime, ok := authOpts["pg_max_life_time"]; ok {
lifeTime, err := strconv.ParseInt(maxLifeTime, 10, 64)
if err == nil {
postgres.maxLifeTime = lifeTime
}
}
var err error
postgres.DB, err = OpenDatabase(connStr, "postgres", postgres.connectTries)
postgres.DB, err = OpenDatabase(connStr, "postgres", postgres.connectTries, postgres.maxLifeTime)
if err != nil {
return postgres, errors.Errorf("PG backend error: couldn't open db: %s", err)

View File

@ -21,6 +21,7 @@ type Sqlite struct {
SuperuserQuery string
AclQuery string
hasher hashing.HashComparer
maxLifeTime int64
connectTries int
}
@ -62,6 +63,14 @@ func NewSqlite(authOpts map[string]string, logLevel log.Level, hasher hashing.Ha
sqlite.AclQuery = aclQuery
}
if maxLifeTime, ok := authOpts["sqlite_max_life_time"]; ok {
lifeTime, err := strconv.ParseInt(maxLifeTime, 10, 64)
if err == nil {
sqlite.maxLifeTime = lifeTime
}
}
//Exit if any mandatory option is missing.
if !sqliteOk {
return sqlite, errors.Errorf("sqlite backend error: missing options: %s", missingOptions)
@ -84,7 +93,7 @@ func NewSqlite(authOpts map[string]string, logLevel log.Level, hasher hashing.Ha
}
var err error
sqlite.DB, err = OpenDatabase(connStr, "sqlite3", sqlite.connectTries)
sqlite.DB, err = OpenDatabase(connStr, "sqlite3", sqlite.connectTries, sqlite.maxLifeTime)
if err != nil {
return sqlite, errors.Errorf("sqlite backend error: couldn't open db %s: %s", connStr, err)