Skip to content

Commit

Permalink
tapd: allow configuring all connection params for Postgres
Browse files Browse the repository at this point in the history
We want to be able to specify the number of idle connections, and other
lifetime parameters for Postgres as well.
  • Loading branch information
guggero committed Oct 24, 2023
1 parent d9a8efd commit e731dd4
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions tapdb/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (

const (
dsnTemplate = "postgres://%v:%v@%v:%d/%v?sslmode=%v"

// defaultMaxIdleConns is the number of permitted idle connections.
defaultMaxIdleConns = 6

// defaultConnMaxIdleTime is the amount of time a connection can be
// idle before it is closed.
defaultConnMaxIdleTime = 5 * time.Minute
)

var (
Expand All @@ -27,14 +34,17 @@ var (

// PostgresConfig holds the postgres database configuration.
type PostgresConfig struct {
SkipMigrations bool `long:"skipmigrations" description:"Skip applying migrations on startup."`
Host string `long:"host" description:"Database server hostname."`
Port int `long:"port" description:"Database server port."`
User string `long:"user" description:"Database user."`
Password string `long:"password" description:"Database user's password."`
DBName string `long:"dbname" description:"Database name to use."`
MaxOpenConnections int `long:"maxconnections" description:"Max open connections to keep alive to the database server."`
RequireSSL bool `long:"requiressl" description:"Whether to require using SSL (mode: require) when connecting to the server."`
SkipMigrations bool `long:"skipmigrations" description:"Skip applying migrations on startup."`
Host string `long:"host" description:"Database server hostname."`
Port int `long:"port" description:"Database server port."`
User string `long:"user" description:"Database user."`
Password string `long:"password" description:"Database user's password."`
DBName string `long:"dbname" description:"Database name to use."`
MaxOpenConnections int `long:"maxconnections" description:"Max open connections to keep alive to the database server."`
MaxIdleConnections int `long:"maxidleconnections" description:"Max number of idle connections to keep in the connection pool."`
ConnMaxLifetime time.Duration `long:"connmaxlifetime" description:"Max amount of time a connection can be reused for before it is closed."`
ConnMaxIdleTime time.Duration `long:"connmaxidletime" description:"Max amount of time a connection can be idle for before it is closed."`
RequireSSL bool `long:"requiressl" description:"Whether to require using SSL (mode: require) when connecting to the server."`
}

// DSN returns the dns to connect to the database.
Expand Down Expand Up @@ -77,9 +87,25 @@ func NewPostgresStore(cfg *PostgresConfig) (*PostgresStore, error) {
maxConns = cfg.MaxOpenConnections
}

maxIdleConns := defaultMaxIdleConns
if cfg.MaxIdleConnections > 0 {
maxIdleConns = cfg.MaxIdleConnections
}

connMaxLifetime := defaultConnMaxLifetime
if cfg.ConnMaxLifetime > 0 {
connMaxLifetime = cfg.ConnMaxLifetime
}

connMaxIdleTime := defaultConnMaxIdleTime
if cfg.ConnMaxIdleTime > 0 {
connMaxIdleTime = cfg.ConnMaxIdleTime
}

rawDb.SetMaxOpenConns(maxConns)
rawDb.SetMaxIdleConns(maxConns)
rawDb.SetConnMaxLifetime(defaultConnMaxLifetime)
rawDb.SetMaxIdleConns(maxIdleConns)
rawDb.SetConnMaxLifetime(connMaxLifetime)
rawDb.SetConnMaxIdleTime(connMaxIdleTime)

if !cfg.SkipMigrations {
// Now that the database is open, populate the database with
Expand Down

0 comments on commit e731dd4

Please sign in to comment.