mysql: made max_open_connections configurable

This commit is contained in:
vishalnayak 2015-10-01 21:15:56 -04:00
parent 2051101c43
commit 644a655920
3 changed files with 34 additions and 4 deletions

View file

@ -68,11 +68,13 @@ func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
fmt.Errorf("configure the DB connection with config/connection first") fmt.Errorf("configure the DB connection with config/connection first")
} }
var conn string var connConfig connectionConfig
if err := entry.DecodeJSON(&conn); err != nil { if err := entry.DecodeJSON(&connConfig); err != nil {
return nil, err return nil, err
} }
conn := connConfig.ConnectionString
b.db, err = sql.Open("mysql", conn) b.db, err = sql.Open("mysql", conn)
if err != nil { if err != nil {
return nil, err return nil, err
@ -80,7 +82,7 @@ func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
// Set some connection pool settings. We don't need much of this, // Set some connection pool settings. We don't need much of this,
// since the request rate shouldn't be high. // since the request rate shouldn't be high.
b.db.SetMaxOpenConns(2) b.db.SetMaxOpenConns(connConfig.MaxOpenConnections)
return b.db, nil return b.db, nil
} }

View file

@ -17,6 +17,10 @@ func pathConfigConnection(b *backend) *framework.Path {
Type: framework.TypeString, Type: framework.TypeString,
Description: "DB connection string", Description: "DB connection string",
}, },
"max_open_connections": &framework.FieldSchema{
Type: framework.TypeInt,
Description: "Maximum number of open connections to database",
},
}, },
Callbacks: map[logical.Operation]framework.OperationFunc{ Callbacks: map[logical.Operation]framework.OperationFunc{
@ -32,8 +36,14 @@ func (b *backend) pathConnectionWrite(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) { req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
connString := data.Get("value").(string) connString := data.Get("value").(string)
maxOpenConns := data.Get("max_open_connections").(int)
if maxOpenConns == 0 {
maxOpenConns = 2
}
// Verify the string // Verify the string
db, err := sql.Open("mysql", connString) db, err := sql.Open("mysql", connString)
if err != nil { if err != nil {
return logical.ErrorResponse(fmt.Sprintf( return logical.ErrorResponse(fmt.Sprintf(
"Error validating connection info: %s", err)), nil "Error validating connection info: %s", err)), nil
@ -45,7 +55,10 @@ func (b *backend) pathConnectionWrite(
} }
// Store it // Store it
entry, err := logical.StorageEntryJSON("config/connection", connString) entry, err := logical.StorageEntryJSON("config/connection", connectionConfig{
ConnectionString: connString,
MaxOpenConnections: maxOpenConns,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -58,6 +71,11 @@ func (b *backend) pathConnectionWrite(
return nil, nil return nil, nil
} }
type connectionConfig struct {
ConnectionString string `json:"value"`
MaxOpenConnections int `json:"max_open_connections"`
}
const pathConfigConnectionHelpSyn = ` const pathConfigConnectionHelpSyn = `
Configure the connection string to talk to MySQL. Configure the connection string to talk to MySQL.
` `

View file

@ -129,6 +129,16 @@ allowed to read.
</li> </li>
</ul> </ul>
</dd> </dd>
<dd>
<ul>
<li>
<span class="param">max_open_connections</span>
<span class="param-flags">optional</span>
Maximum number of open connections to the database.
Defaults to 2.
</li>
</ul>
</dd>
<dt>Returns</dt> <dt>Returns</dt>
<dd> <dd>