better mysql connection pool management

adding SetConnMaxLifetime configuration (https://golang.org/pkg/database/sql/#DB.SetConnMaxLifetime)
to enable refresh of connections.
This commit is contained in:
LazyDBA247-Anyvision 2021-01-14 08:14:21 +02:00
parent f17aa1d06c
commit 8eed763b97
2 changed files with 5 additions and 2 deletions

View file

@ -118,6 +118,7 @@ password = ""
database = "" # create or use an existing database database = "" # create or use an existing database
connection_max_idle = 2 connection_max_idle = 2
connection_max_open = 100 connection_max_open = 100
connection_max_lifetime_seconds = 0
interpolateParams = false interpolateParams = false
[postgres] # or cockroachdb [postgres] # or cockroachdb

View file

@ -35,12 +35,13 @@ func (store *MysqlStore) Initialize(configuration util.Configuration, prefix str
configuration.GetString(prefix+"database"), configuration.GetString(prefix+"database"),
configuration.GetInt(prefix+"connection_max_idle"), configuration.GetInt(prefix+"connection_max_idle"),
configuration.GetInt(prefix+"connection_max_open"), configuration.GetInt(prefix+"connection_max_open"),
configuration.GetInt(prefix+"connection_max_lifetime_seconds"),
configuration.GetBool(prefix+"interpolateParams"), configuration.GetBool(prefix+"interpolateParams"),
) )
} }
func (store *MysqlStore) initialize(user, password, hostname string, port int, database string, maxIdle, maxOpen int, func (store *MysqlStore) initialize(user, password, hostname string, port int, database string, maxIdle, maxOpen,
interpolateParams bool) (err error) { maxLifetimeSeconds int, interpolateParams bool) (err error) {
// //
store.SqlInsert = "INSERT INTO filemeta (dirhash,name,directory,meta) VALUES(?,?,?,?)" store.SqlInsert = "INSERT INTO filemeta (dirhash,name,directory,meta) VALUES(?,?,?,?)"
store.SqlUpdate = "UPDATE filemeta SET meta=? WHERE dirhash=? AND name=? AND directory=?" store.SqlUpdate = "UPDATE filemeta SET meta=? WHERE dirhash=? AND name=? AND directory=?"
@ -65,6 +66,7 @@ func (store *MysqlStore) initialize(user, password, hostname string, port int, d
store.DB.SetMaxIdleConns(maxIdle) store.DB.SetMaxIdleConns(maxIdle)
store.DB.SetMaxOpenConns(maxOpen) store.DB.SetMaxOpenConns(maxOpen)
store.DB.SetConnMaxLifetime(maxLifetimeSeconds*time.Second)
if err = store.DB.Ping(); err != nil { if err = store.DB.Ping(); err != nil {
return fmt.Errorf("connect to %s error:%v", sqlUrl, err) return fmt.Errorf("connect to %s error:%v", sqlUrl, err)