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:
Silvan
2024-07-17 17:16:02 +02:00
committed by GitHub
parent d95c9508a3
commit 99c645cc60
13 changed files with 51 additions and 50 deletions

View File

@@ -1,12 +1,14 @@
package postgres
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"
@@ -71,22 +73,32 @@ 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 {