mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:17:32 +00:00
refactor(database): exchange connection pool (#8325)
# Which Problems Are Solved The connection pool of go uses a high amount of database connections. # How the Problems Are Solved The standard lib connection pool was replaced by `pgxpool.Pool` # Additional Changes The `db.BeginTx`-spans are removed because they cause to much noise in the traces. # Additional Context - part of https://github.com/zitadel/zitadel/issues/7639
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package cockroach
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
@@ -70,22 +72,29 @@ func (_ *Config) Decode(configs []interface{}) (dialect.Connector, error) {
|
||||
}
|
||||
|
||||
func (c *Config) Connect(useAdmin bool, pusherRatio, spoolerRatio float64, purpose dialect.DBPurpose) (*sql.DB, error) {
|
||||
client, err := sql.Open("pgx", c.String(useAdmin, purpose.AppName()))
|
||||
connConfig, err := dialect.NewConnectionConfig(c.MaxOpenConns, c.MaxIdleConns, pusherRatio, spoolerRatio, purpose)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
connConfig, err := dialect.NewConnectionConfig(c.MaxOpenConns, c.MaxIdleConns, spoolerRatio, pusherRatio, purpose)
|
||||
config, err := pgxpool.ParseConfig(c.String(useAdmin, purpose.AppName()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.MaxConns = int32(connConfig.MaxOpenConns)
|
||||
config.MaxConnLifetime = c.MaxConnLifetime
|
||||
config.MaxConnIdleTime = c.MaxConnIdleTime
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(context.Background(), config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client.SetMaxOpenConns(int(connConfig.MaxOpenConns))
|
||||
client.SetMaxIdleConns(int(connConfig.MaxIdleConns))
|
||||
client.SetConnMaxLifetime(c.MaxConnLifetime)
|
||||
client.SetConnMaxIdleTime(c.MaxConnIdleTime)
|
||||
if err := pool.Ping(context.Background()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return client, nil
|
||||
return stdlib.OpenDBFromPool(pool), nil
|
||||
}
|
||||
|
||||
func (c *Config) DatabaseName() string {
|
||||
|
Reference in New Issue
Block a user