2019-05-21 15:30:06 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package dbutil
|
|
|
|
|
2019-06-04 22:30:21 +01:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"flag"
|
|
|
|
|
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-06-05 00:53:38 +01:00
|
|
|
maxIdleConns = flag.Int("db.max_idle_conns", 50, "Maximum Amount of Idle Database connections, -1 means the stdlib default")
|
|
|
|
maxOpenConns = flag.Int("db.max_open_conns", 100, "Maximum Amount of Open Database connections, -1 means the stdlib default")
|
|
|
|
connMaxLifetime = flag.Duration("db.conn_max_lifetime", -1, "Maximum Database Connection Lifetime, -1ns means the stdlib default")
|
2019-06-04 22:30:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Configure Sets Connection Boundaries and adds db_stats monitoring to monkit
|
|
|
|
func Configure(db *sql.DB, mon *monkit.Scope) {
|
|
|
|
if *maxIdleConns >= 0 {
|
|
|
|
db.SetMaxIdleConns(*maxIdleConns)
|
|
|
|
}
|
|
|
|
if *maxOpenConns >= 0 {
|
|
|
|
db.SetMaxOpenConns(*maxOpenConns)
|
|
|
|
}
|
|
|
|
if *connMaxLifetime >= 0 {
|
|
|
|
db.SetConnMaxLifetime(*connMaxLifetime)
|
|
|
|
}
|
|
|
|
mon.Chain("db_stats", monkit.StatSourceFunc(
|
|
|
|
func(cb func(name string, val float64)) {
|
|
|
|
monkit.StatSourceFromStruct(db.Stats()).Stats(cb)
|
|
|
|
}))
|
|
|
|
}
|