make connection pool params configurable
This commit is contained in:
parent
fb6fdfcc13
commit
8dc31e4ca5
@ -24,19 +24,25 @@ import (
|
|||||||
|
|
||||||
// Env variables
|
// Env variables
|
||||||
const (
|
const (
|
||||||
DATABASE_NAME = "DATABASE_NAME"
|
DATABASE_NAME = "DATABASE_NAME"
|
||||||
DATABASE_HOSTNAME = "DATABASE_HOSTNAME"
|
DATABASE_HOSTNAME = "DATABASE_HOSTNAME"
|
||||||
DATABASE_PORT = "DATABASE_PORT"
|
DATABASE_PORT = "DATABASE_PORT"
|
||||||
DATABASE_USER = "DATABASE_USER"
|
DATABASE_USER = "DATABASE_USER"
|
||||||
DATABASE_PASSWORD = "DATABASE_PASSWORD"
|
DATABASE_PASSWORD = "DATABASE_PASSWORD"
|
||||||
|
DATABASE_MAX_IDLE_CONNECTIONS = "DATABASE_MAX_IDLE_CONNECTIONS"
|
||||||
|
DATABASE_MAX_OPEN_CONNECTIONS = "DATABASE_MAX_OPEN_CONNECTIONS"
|
||||||
|
DATABASE_MAX_CONN_LIFETIME = "DATABASE_MAX_CONN_LIFETIME"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Database struct {
|
type Database struct {
|
||||||
Hostname string
|
Hostname string
|
||||||
Name string
|
Name string
|
||||||
User string
|
User string
|
||||||
Password string
|
Password string
|
||||||
Port int
|
Port int
|
||||||
|
MaxIdle int
|
||||||
|
MaxOpen int
|
||||||
|
MaxLifetime int
|
||||||
}
|
}
|
||||||
|
|
||||||
func DbConnectionString(dbConfig Database) string {
|
func DbConnectionString(dbConfig Database) string {
|
||||||
@ -57,9 +63,16 @@ func (d *Database) Init() {
|
|||||||
viper.BindEnv("database.port", DATABASE_PORT)
|
viper.BindEnv("database.port", DATABASE_PORT)
|
||||||
viper.BindEnv("database.user", DATABASE_USER)
|
viper.BindEnv("database.user", DATABASE_USER)
|
||||||
viper.BindEnv("database.password", DATABASE_PASSWORD)
|
viper.BindEnv("database.password", DATABASE_PASSWORD)
|
||||||
|
viper.BindEnv("database.maxIdle", DATABASE_MAX_IDLE_CONNECTIONS)
|
||||||
|
viper.BindEnv("database.maxOpen", DATABASE_MAX_OPEN_CONNECTIONS)
|
||||||
|
viper.BindEnv("database.maxLifetime", DATABASE_MAX_CONN_LIFETIME)
|
||||||
|
|
||||||
d.Name = viper.GetString("database.name")
|
d.Name = viper.GetString("database.name")
|
||||||
d.Hostname = viper.GetString("database.hostname")
|
d.Hostname = viper.GetString("database.hostname")
|
||||||
d.Port = viper.GetInt("database.port")
|
d.Port = viper.GetInt("database.port")
|
||||||
d.User = viper.GetString("database.user")
|
d.User = viper.GetString("database.user")
|
||||||
d.Password = viper.GetString("database.password")
|
d.Password = viper.GetString("database.password")
|
||||||
|
d.MaxIdle = viper.GetInt("database.maxIdle")
|
||||||
|
d.MaxOpen = viper.GetInt("database.maxOpen")
|
||||||
|
d.MaxLifetime = viper.GetInt("database.maxLifetime")
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/lib/pq" //postgres driver
|
_ "github.com/lib/pq" //postgres driver
|
||||||
"github.com/vulcanize/vulcanizedb/pkg/config"
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
||||||
@ -35,6 +37,16 @@ func NewDB(databaseConfig config.Database, node core.Node) (*DB, error) {
|
|||||||
if connectErr != nil {
|
if connectErr != nil {
|
||||||
return &DB{}, ErrDBConnectionFailed(connectErr)
|
return &DB{}, ErrDBConnectionFailed(connectErr)
|
||||||
}
|
}
|
||||||
|
if databaseConfig.MaxOpen > 0 {
|
||||||
|
db.SetMaxOpenConns(databaseConfig.MaxOpen)
|
||||||
|
}
|
||||||
|
if databaseConfig.MaxIdle > 0 {
|
||||||
|
db.SetMaxIdleConns(databaseConfig.MaxIdle)
|
||||||
|
}
|
||||||
|
if databaseConfig.MaxLifetime > 0 {
|
||||||
|
lifetime := time.Duration(databaseConfig.MaxLifetime) * time.Second
|
||||||
|
db.SetConnMaxLifetime(lifetime)
|
||||||
|
}
|
||||||
pg := DB{DB: db, Node: node}
|
pg := DB{DB: db, Node: node}
|
||||||
nodeErr := pg.CreateNode(&node)
|
nodeErr := pg.CreateNode(&node)
|
||||||
if nodeErr != nil {
|
if nodeErr != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user