Add support for conn_max_lifetime (#2270)

This adds support to configure MySQL conn_max_lifetime.
This commit is contained in:
Stephan Miehe 2020-07-30 09:00:42 -07:00 committed by GitHub
parent cf4d8ecfee
commit 2ad5205a4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 24 deletions

View File

@ -254,6 +254,19 @@ Maximum idle connections to database. This value should be equal to or less than
max_idle_conns: 50 max_idle_conns: 50
``` ```
##### `conn_max_lifetime`
Maximum amount of time, in seconds, a connection may be reused.
- Default value: 0 (Unlimited)
- Environment variable: `KOLIDE_MYSQL_CONN_MAX_LIFETIME`
- Config file format:
```
mysql:
conn_max_lifetime: 50
```
#### Redis #### Redis
##### `redis_address` ##### `redis_address`

View File

@ -17,18 +17,19 @@ const (
// MysqlConfig defines configs related to MySQL // MysqlConfig defines configs related to MySQL
type MysqlConfig struct { type MysqlConfig struct {
Protocol string Protocol string
Address string Address string
Username string Username string
Password string Password string
Database string Database string
TLSCert string `yaml:"tls_cert"` TLSCert string `yaml:"tls_cert"`
TLSKey string `yaml:"tls_key"` TLSKey string `yaml:"tls_key"`
TLSCA string `yaml:"tls_ca"` TLSCA string `yaml:"tls_ca"`
TLSServerName string `yaml:"tls_server_name"` TLSServerName string `yaml:"tls_server_name"`
TLSConfig string `yaml:"tls_config"` //tls=customValue in DSN TLSConfig string `yaml:"tls_config"` //tls=customValue in DSN
MaxOpenConns int `yaml:"max_open_conns"` MaxOpenConns int `yaml:"max_open_conns"`
MaxIdleConns int `yaml:"max_idle_conns"` MaxIdleConns int `yaml:"max_idle_conns"`
ConnMaxLifetime int `yaml:"conn_max_lifetime"`
} }
// RedisConfig defines configs related to Redis // RedisConfig defines configs related to Redis
@ -160,6 +161,7 @@ func (man Manager) addConfigs() {
"MySQL TLS config value. Use skip-verify, true, false or custom key.") "MySQL TLS config value. Use skip-verify, true, false or custom key.")
man.addConfigInt("mysql.max_open_conns", 50, "MySQL maximum open connection handles.") man.addConfigInt("mysql.max_open_conns", 50, "MySQL maximum open connection handles.")
man.addConfigInt("mysql.max_idle_conns", 50, "MySQL maximum idle connection handles.") man.addConfigInt("mysql.max_idle_conns", 50, "MySQL maximum idle connection handles.")
man.addConfigInt("mysql.conn_max_lifetime", 0, "MySQL maximum amount of time a connection may be reused.")
// Redis // Redis
man.addConfigString("redis.address", "localhost:6379", man.addConfigString("redis.address", "localhost:6379",
@ -262,18 +264,19 @@ func (man Manager) LoadConfig() KolideConfig {
return KolideConfig{ return KolideConfig{
Mysql: MysqlConfig{ Mysql: MysqlConfig{
Protocol: man.getConfigString("mysql.protocol"), Protocol: man.getConfigString("mysql.protocol"),
Address: man.getConfigString("mysql.address"), Address: man.getConfigString("mysql.address"),
Username: man.getConfigString("mysql.username"), Username: man.getConfigString("mysql.username"),
Password: man.getConfigString("mysql.password"), Password: man.getConfigString("mysql.password"),
Database: man.getConfigString("mysql.database"), Database: man.getConfigString("mysql.database"),
TLSCert: man.getConfigString("mysql.tls_cert"), TLSCert: man.getConfigString("mysql.tls_cert"),
TLSKey: man.getConfigString("mysql.tls_key"), TLSKey: man.getConfigString("mysql.tls_key"),
TLSCA: man.getConfigString("mysql.tls_ca"), TLSCA: man.getConfigString("mysql.tls_ca"),
TLSServerName: man.getConfigString("mysql.tls_server_name"), TLSServerName: man.getConfigString("mysql.tls_server_name"),
TLSConfig: man.getConfigString("mysql.tls_config"), TLSConfig: man.getConfigString("mysql.tls_config"),
MaxOpenConns: man.getConfigInt("mysql.max_open_conns"), MaxOpenConns: man.getConfigInt("mysql.max_open_conns"),
MaxIdleConns: man.getConfigInt("mysql.max_idle_conns"), MaxIdleConns: man.getConfigInt("mysql.max_idle_conns"),
ConnMaxLifetime: man.getConfigInt("mysql.conn_max_lifetime"),
}, },
Redis: RedisConfig{ Redis: RedisConfig{
Address: man.getConfigString("redis.address"), Address: man.getConfigString("redis.address"),

View File

@ -117,6 +117,7 @@ func New(config config.MysqlConfig, c clock.Clock, opts ...DBOption) (*Datastore
db.SetMaxIdleConns(config.MaxIdleConns) db.SetMaxIdleConns(config.MaxIdleConns)
db.SetMaxOpenConns(config.MaxOpenConns) db.SetMaxOpenConns(config.MaxOpenConns)
db.SetConnMaxLifetime(time.Second * time.Duration(config.ConnMaxLifetime))
var dbError error var dbError error
for attempt := 0; attempt < options.maxAttempts; attempt++ { for attempt := 0; attempt < options.maxAttempts; attempt++ {