Merge pull request #661 from hashicorp/maxopenconns
Parameterize max open connections in postgresql and mysql backends
This commit is contained in:
commit
bf464b9a4b
|
@ -68,11 +68,16 @@ func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
|
|||
fmt.Errorf("configure the DB connection with config/connection first")
|
||||
}
|
||||
|
||||
var conn string
|
||||
if err := entry.DecodeJSON(&conn); err != nil {
|
||||
var connConfig connectionConfig
|
||||
if err := entry.DecodeJSON(&connConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conn := connConfig.ConnectionString
|
||||
if len(conn) == 0 {
|
||||
conn = connConfig.ConnectionURL
|
||||
}
|
||||
|
||||
b.db, err = sql.Open("mysql", conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -80,7 +85,7 @@ func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
|
|||
|
||||
// Set some connection pool settings. We don't need much of this,
|
||||
// since the request rate shouldn't be high.
|
||||
b.db.SetMaxOpenConns(2)
|
||||
b.db.SetMaxOpenConns(connConfig.MaxOpenConnections)
|
||||
|
||||
return b.db, nil
|
||||
}
|
||||
|
|
|
@ -13,10 +13,20 @@ func pathConfigConnection(b *backend) *framework.Path {
|
|||
return &framework.Path{
|
||||
Pattern: "config/connection",
|
||||
Fields: map[string]*framework.FieldSchema{
|
||||
"value": &framework.FieldSchema{
|
||||
"connection_url": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: "DB connection string",
|
||||
},
|
||||
"value": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: `
|
||||
DB connection string. Use 'connection_url' instead.
|
||||
This will be deprecated.`,
|
||||
},
|
||||
"max_open_connections": &framework.FieldSchema{
|
||||
Type: framework.TypeInt,
|
||||
Description: "Maximum number of open connections to database",
|
||||
},
|
||||
},
|
||||
|
||||
Callbacks: map[logical.Operation]framework.OperationFunc{
|
||||
|
@ -31,9 +41,16 @@ func pathConfigConnection(b *backend) *framework.Path {
|
|||
func (b *backend) pathConnectionWrite(
|
||||
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
||||
connString := data.Get("value").(string)
|
||||
connURL := data.Get("connection_url").(string)
|
||||
|
||||
maxOpenConns := data.Get("max_open_connections").(int)
|
||||
if maxOpenConns == 0 {
|
||||
maxOpenConns = 2
|
||||
}
|
||||
|
||||
// Verify the string
|
||||
db, err := sql.Open("mysql", connString)
|
||||
|
||||
if err != nil {
|
||||
return logical.ErrorResponse(fmt.Sprintf(
|
||||
"Error validating connection info: %s", err)), nil
|
||||
|
@ -45,7 +62,11 @@ func (b *backend) pathConnectionWrite(
|
|||
}
|
||||
|
||||
// Store it
|
||||
entry, err := logical.StorageEntryJSON("config/connection", connString)
|
||||
entry, err := logical.StorageEntryJSON("config/connection", connectionConfig{
|
||||
ConnectionString: connString,
|
||||
ConnectionURL: connURL,
|
||||
MaxOpenConnections: maxOpenConns,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -58,6 +79,13 @@ func (b *backend) pathConnectionWrite(
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
type connectionConfig struct {
|
||||
ConnectionURL string `json:"connection_url"`
|
||||
// Deprecate "value" in coming releases
|
||||
ConnectionString string `json:"value"`
|
||||
MaxOpenConnections int `json:"max_open_connections"`
|
||||
}
|
||||
|
||||
const pathConfigConnectionHelpSyn = `
|
||||
Configure the connection string to talk to MySQL.
|
||||
`
|
||||
|
|
|
@ -6,9 +6,9 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/lib/pq"
|
||||
"github.com/hashicorp/vault/logical"
|
||||
"github.com/hashicorp/vault/logical/framework"
|
||||
"github.com/lib/pq"
|
||||
)
|
||||
|
||||
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
|
||||
|
@ -70,11 +70,16 @@ func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
|
|||
fmt.Errorf("configure the DB connection with config/connection first")
|
||||
}
|
||||
|
||||
var conn string
|
||||
if err := entry.DecodeJSON(&conn); err != nil {
|
||||
var connConfig connectionConfig
|
||||
if err := entry.DecodeJSON(&connConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
conn := connConfig.ConnectionString
|
||||
if len(conn) == 0 {
|
||||
conn = connConfig.ConnectionURL
|
||||
}
|
||||
|
||||
// Ensure timezone is set to UTC for all the conenctions
|
||||
if strings.HasPrefix(conn, "postgres://") || strings.HasPrefix(conn, "postgresql://") {
|
||||
var err error
|
||||
|
@ -92,7 +97,7 @@ func (b *backend) DB(s logical.Storage) (*sql.DB, error) {
|
|||
|
||||
// Set some connection pool settings. We don't need much of this,
|
||||
// since the request rate shouldn't be high.
|
||||
b.db.SetMaxOpenConns(2)
|
||||
b.db.SetMaxOpenConns(connConfig.MaxOpenConnections)
|
||||
|
||||
return b.db, nil
|
||||
}
|
||||
|
|
|
@ -13,10 +13,20 @@ func pathConfigConnection(b *backend) *framework.Path {
|
|||
return &framework.Path{
|
||||
Pattern: "config/connection",
|
||||
Fields: map[string]*framework.FieldSchema{
|
||||
"value": &framework.FieldSchema{
|
||||
"connection_url": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: "DB connection string",
|
||||
},
|
||||
"value": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: `
|
||||
DB connection string. Use 'connection_url' instead.
|
||||
This will be deprecated.`,
|
||||
},
|
||||
"max_open_connections": &framework.FieldSchema{
|
||||
Type: framework.TypeInt,
|
||||
Description: "Maximum number of open connections to the database",
|
||||
},
|
||||
},
|
||||
|
||||
Callbacks: map[logical.Operation]framework.OperationFunc{
|
||||
|
@ -31,6 +41,12 @@ func pathConfigConnection(b *backend) *framework.Path {
|
|||
func (b *backend) pathConnectionWrite(
|
||||
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
|
||||
connString := data.Get("value").(string)
|
||||
connURL := data.Get("connection_url").(string)
|
||||
|
||||
maxOpenConns := data.Get("max_open_connections").(int)
|
||||
if maxOpenConns == 0 {
|
||||
maxOpenConns = 2
|
||||
}
|
||||
|
||||
// Verify the string
|
||||
db, err := sql.Open("postgres", connString)
|
||||
|
@ -45,7 +61,11 @@ func (b *backend) pathConnectionWrite(
|
|||
}
|
||||
|
||||
// Store it
|
||||
entry, err := logical.StorageEntryJSON("config/connection", connString)
|
||||
entry, err := logical.StorageEntryJSON("config/connection", connectionConfig{
|
||||
ConnectionString: connString,
|
||||
ConnectionURL: connURL,
|
||||
MaxOpenConnections: maxOpenConns,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -59,6 +79,13 @@ func (b *backend) pathConnectionWrite(
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
type connectionConfig struct {
|
||||
ConnectionURL string `json:"connection_url"`
|
||||
// Deprecate "value" in coming releases
|
||||
ConnectionString string `json:"value"`
|
||||
MaxOpenConnections int `json:"max_open_connections"`
|
||||
}
|
||||
|
||||
const pathConfigConnectionHelpSyn = `
|
||||
Configure the connection string to talk to PostgreSQL.
|
||||
`
|
||||
|
|
|
@ -129,6 +129,16 @@ allowed to read.
|
|||
</li>
|
||||
</ul>
|
||||
</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>
|
||||
<dd>
|
||||
|
|
|
@ -135,6 +135,16 @@ subpath for interactive help output.
|
|||
</li>
|
||||
</ul>
|
||||
</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>
|
||||
<dd>
|
||||
|
|
Loading…
Reference in New Issue