// Copyright (C) 2019 Storj Labs, Inc. // See LICENSE for copying information. package dbutil import ( "database/sql" "flag" "time" monkit "gopkg.in/spacemonkeygo/monkit.v2" ) var ( maxIdleConns = flag.Int("db.max_idle_conns", 20, "Maximum Amount of Idle Database connections, -1 means the stdlib default") maxOpenConns = flag.Int("db.max_open_conns", 25, "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") ) // ConfigurableDB contains methods for configuring a database. type ConfigurableDB interface { SetMaxIdleConns(int) SetMaxOpenConns(int) SetConnMaxLifetime(time.Duration) Stats() sql.DBStats } // Configure Sets Connection Boundaries and adds db_stats monitoring to monkit func Configure(db ConfigurableDB, 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) })) }