Skip to content

Commit

Permalink
Add MySQL db max idle connections and connections lifetime assignment (
Browse files Browse the repository at this point in the history
…#4211)

Allow the storage backend for MySQL to use a custom connection lifetime and max idle connection value if the parameter is specified in the config file of vault otherwise do not set in order to leave at default value.
  • Loading branch information
rin1221 authored and jefferai committed Mar 28, 2018
1 parent 94b2878 commit 90e3ad2
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions physical/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ func NewMySQLBackend(conf map[string]string, logger log.Logger) (physical.Backen
}
dbTable := database + "." + table

maxIdleConnStr, ok := conf["max_idle_connections"]
var maxIdleConnInt int = nil
if ok {
maxParmaxIdleConnInt, err = strconv.Atoi(maxIdleConnStr)
if err != nil {
return nil, errwrap.Wrapf("failed parsing max_idle_connections parameter: {{err}}", err)
}
if logger.IsDebug() {
logger.Debug("mysql: max_idle_connections set", "max_idle_connections", maxIdleConnInt)
}
}

maxConnLifeStr, ok := conf["max_connection_lifetime"]
var maxConnLifeInt int = nil
if ok {
maxConnLifeInt, err = strconv.Atoi(maxConnLifeStr)
if err != nil {
return nil, errwrap.Wrapf("failed parsing max_connection_lifetime parameter: {{err}}", err)
}
if logger.IsDebug() {
logger.Debug("mysql: max_connection_lifetime set", "max_connection_lifetime", maxConnLifeInt)
}
}

maxParStr, ok := conf["max_parallel"]
var maxParInt int
if ok {
Expand Down Expand Up @@ -101,9 +125,13 @@ func NewMySQLBackend(conf map[string]string, logger log.Logger) (physical.Backen
if err != nil {
return nil, fmt.Errorf("failed to connect to mysql: %v", err)
}

db.SetMaxOpenConns(maxParInt)

if maxIdleConnInt != nil {
db.SetMaxIdleConns(maxIdleConnInt)
}
if maxConnLifeInt != nil {
db.SetConnMaxLifetime(time.Second * maxConnLifeInt)
}
// Check schema exists
var schemaExist bool
schemaRows, err := db.Query("SELECT SCHEMA_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = ?", database)
Expand Down

3 comments on commit 90e3ad2

@sethvargo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke the build and compiling...

@sethvargo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jefferai
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy to fix :-)

Please sign in to comment.