From 06db08816878076946658e92d40735da73b0724e Mon Sep 17 00:00:00 2001 From: marvin Date: Fri, 12 Jul 2024 10:34:36 +0800 Subject: [PATCH 1/2] perf db connect poll --- config/config.go | 21 ++++++++++++--------- database/bun.go | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/config/config.go b/config/config.go index 9becf9e..46b9818 100644 --- a/config/config.go +++ b/config/config.go @@ -41,15 +41,18 @@ type Contract struct { // Database type Database struct { - Path string `yaml:"path,omitempty"` - Kind string `yaml:"kind" validate:"required,oneof=sqlite postgres mysql clickhouse elasticsearch"` - Host string `yaml:"host" validate:"required_with=Port User Database"` - Port int `yaml:"port" validate:"required_with=Host User Database,gt=-1,lt=65535"` - User string `yaml:"user" validate:"required_with=Host Port Database"` - Password string `yaml:"password"` - Database string `yaml:"database" validate:"required_with=Host Port User"` - SchemaName string `yaml:"schema_name"` - ApplicationName string `yaml:"application_name"` + Path string `yaml:"path,omitempty"` + Kind string `yaml:"kind" validate:"required,oneof=sqlite postgres mysql clickhouse elasticsearch"` + Host string `yaml:"host" validate:"required_with=Port User Database"` + Port int `yaml:"port" validate:"required_with=Host User Database,gt=-1,lt=65535"` + User string `yaml:"user" validate:"required_with=Host Port Database"` + Password string `yaml:"password"` + Database string `yaml:"database" validate:"required_with=Host Port User"` + SchemaName string `yaml:"schema_name"` + ApplicationName string `yaml:"application_name"` + MaxOpenConnections int `yaml:"max_open_connections"` + MaxIdleConnections int `yaml:"max_idle_connections"` + MaxLifetimeConnections int `yaml:"max_lifetime_connections"` } // Hasura - diff --git a/database/bun.go b/database/bun.go index 461224a..2c1f5b3 100644 --- a/database/bun.go +++ b/database/bun.go @@ -6,6 +6,7 @@ import ( "fmt" "net/url" "runtime" + "time" "github.com/dipdup-net/go-lib/config" "github.com/pkg/errors" @@ -69,8 +70,23 @@ func (db *Bun) Connect(ctx context.Context, cfg config.Database) error { db.conn = bun.NewDB(db.sqldb, pgdialect.New()) } maxOpenConns := 4 * runtime.GOMAXPROCS(0) + maxIdleConns := maxOpenConns + maxLifetime := time.Minute + if cfg.MaxOpenConnections > 0 { + maxOpenConns = cfg.MaxOpenConnections + } + + if cfg.MaxIdleConnections > 0 { + maxIdleConns = cfg.MaxIdleConnections + } + + if cfg.MaxLifetimeConnections > 0 { + maxLifetime = time.Duration(maxLifetime) * time.Second + } + db.sqldb.SetMaxOpenConns(maxOpenConns) - db.sqldb.SetMaxIdleConns(maxOpenConns) + db.sqldb.SetMaxIdleConns(maxIdleConns) + db.sqldb.SetConnMaxLifetime(maxLifetime) return nil } From 2ade6415590854b1d369092015e333e64c86a82d Mon Sep 17 00:00:00 2001 From: marvin Date: Fri, 12 Jul 2024 12:04:51 +0800 Subject: [PATCH 2/2] update maxLifetime --- database/bun.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/bun.go b/database/bun.go index 2c1f5b3..67ea5aa 100644 --- a/database/bun.go +++ b/database/bun.go @@ -81,7 +81,7 @@ func (db *Bun) Connect(ctx context.Context, cfg config.Database) error { } if cfg.MaxLifetimeConnections > 0 { - maxLifetime = time.Duration(maxLifetime) * time.Second + maxLifetime = time.Duration(cfg.MaxLifetimeConnections) * time.Second } db.sqldb.SetMaxOpenConns(maxOpenConns)