Postgresql fix (#224)
* Add option to set DB connection max life time in seconds and document it. Co-authored-by: Thomas Tvedt <thomas@gulost.net>
This commit is contained in:
parent
6975f3a176
commit
98d8b4a6aa
|
@ -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.
|
||||
|
||||
|
|
|
@ -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,5 +41,9 @@ func OpenDatabase(dsn, engine string, tries int) (*sqlx.DB, error) {
|
|||
return nil, fmt.Errorf("couldn't ping database %s: %s", engine, err)
|
||||
}
|
||||
|
||||
if maxLifeTime > 0 {
|
||||
db.SetConnMaxLifetime(time.Duration(maxLifeTime) * time.Second)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
2
go.mod
2
go.mod
|
@ -9,7 +9,7 @@ require (
|
|||
github.com/golang/protobuf v1.4.2
|
||||
github.com/gopherjs/gopherjs v0.0.0-20190328170749-bb2674552d8f // indirect
|
||||
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
|
||||
github.com/jmoiron/sqlx v1.2.0
|
||||
github.com/jmoiron/sqlx v1.3.4
|
||||
github.com/klauspost/compress v1.10.6 // indirect
|
||||
github.com/lib/pq v1.5.2
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible
|
||||
|
|
12
go.sum
12
go.sum
|
@ -23,8 +23,6 @@ github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
|
|||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-redis/redis/v8 v8.0.0-beta.2 h1:9S28J9QMBotgI3tGgXbX1Wk9i8QYC3Orw4bTLoPrQeI=
|
||||
github.com/go-redis/redis/v8 v8.0.0-beta.2/go.mod h1:o1M7JtsgfDYyv3o+gBn/jJ1LkqpnCrmil7PSppZGBak=
|
||||
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
|
||||
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
|
||||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
||||
|
@ -97,8 +95,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
|
|||
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
|
||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
|
||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
||||
github.com/jmoiron/sqlx v1.2.0 h1:41Ip0zITnmWNR/vHV+S4m+VoUivnWY5E4OJfLZjCJMA=
|
||||
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
|
||||
github.com/jmoiron/sqlx v1.3.4 h1:wv+0IJZfL5z0uZoUjlpKgHkgaFSYD+r9CfrXjEXsO7w=
|
||||
github.com/jmoiron/sqlx v1.3.4/go.mod h1:2BljVx/86SuTyjE+aPYlHCTNvZrnJXghYGpNiXLBMCQ=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
|
@ -122,14 +120,12 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
|
|||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.5.2 h1:yTSXVswvWUOQ3k1sd7vJfDrbSl8lKuscqFJRqjC0ifw=
|
||||
github.com/lib/pq v1.5.2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
|
||||
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
|
||||
github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4=
|
||||
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
|
||||
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
|
||||
|
|
Loading…
Reference in New Issue