mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 04:07:31 +00:00
refactor: consolidate database pools (#9105)
# Which Problems Are Solved Zitadel currently uses 3 database pool, 1 for queries, 1 for pushing events and 1 for scheduled projection updates. This defeats the purpose of a connection pool which already handles multiple connections. During load tests we found that the current structure of connection pools consumes a lot of database resources. The resource usage dropped after we reduced the amount of database pools to 1 because existing connections can be used more efficiently. # How the Problems Are Solved Removed logic to handle multiple connection pools and use a single one. # Additional Changes none # Additional Context part of https://github.com/zitadel/zitadel/issues/8352
This commit is contained in:
@@ -3,7 +3,6 @@ package dialect
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -11,11 +10,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNegativeRatio = errors.New("ratio cannot be negative")
|
||||
ErrHighSumRatio = errors.New("sum of pusher and projection ratios must be < 1")
|
||||
ErrIllegalMaxOpenConns = errors.New("MaxOpenConns of the database must be higher than 3 or 0 for unlimited")
|
||||
ErrIllegalMaxIdleConns = errors.New("MaxIdleConns of the database must be higher than 3 or 0 for unlimited")
|
||||
ErrInvalidPurpose = errors.New("DBPurpose out of range")
|
||||
)
|
||||
|
||||
// ConnectionConfig defines the Max Open and Idle connections for a DB connection pool.
|
||||
@@ -25,28 +21,6 @@ type ConnectionConfig struct {
|
||||
AfterConnect []func(ctx context.Context, c *pgx.Conn) error
|
||||
}
|
||||
|
||||
// takeRatio of MaxOpenConns and MaxIdleConns from config and returns
|
||||
// a new ConnectionConfig with the resulting values.
|
||||
func (c *ConnectionConfig) takeRatio(ratio float64) (*ConnectionConfig, error) {
|
||||
if ratio < 0 {
|
||||
return nil, ErrNegativeRatio
|
||||
}
|
||||
|
||||
out := &ConnectionConfig{
|
||||
MaxOpenConns: uint32(ratio * float64(c.MaxOpenConns)),
|
||||
MaxIdleConns: uint32(ratio * float64(c.MaxIdleConns)),
|
||||
AfterConnect: c.AfterConnect,
|
||||
}
|
||||
if c.MaxOpenConns != 0 && out.MaxOpenConns < 1 && ratio > 0 {
|
||||
out.MaxOpenConns = 1
|
||||
}
|
||||
if c.MaxIdleConns != 0 && out.MaxIdleConns < 1 && ratio > 0 {
|
||||
out.MaxIdleConns = 1
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
var afterConnectFuncs []func(ctx context.Context, c *pgx.Conn) error
|
||||
|
||||
func RegisterAfterConnect(f func(ctx context.Context, c *pgx.Conn) error) {
|
||||
@@ -82,48 +56,10 @@ func RegisterDefaultPgTypeVariants[T any](m *pgtype.Map, name, arrayName string)
|
||||
//
|
||||
// openConns and idleConns must be at least 3 or 0, which means no limit.
|
||||
// The pusherRatio and spoolerRatio must be between 0 and 1.
|
||||
func NewConnectionConfig(openConns, idleConns uint32, pusherRatio, projectionRatio float64, purpose DBPurpose) (*ConnectionConfig, error) {
|
||||
if openConns != 0 && openConns < 3 {
|
||||
return nil, ErrIllegalMaxOpenConns
|
||||
}
|
||||
if idleConns != 0 && idleConns < 3 {
|
||||
return nil, ErrIllegalMaxIdleConns
|
||||
}
|
||||
if pusherRatio+projectionRatio >= 1 {
|
||||
return nil, ErrHighSumRatio
|
||||
}
|
||||
|
||||
queryConfig := &ConnectionConfig{
|
||||
func NewConnectionConfig(openConns, idleConns uint32) *ConnectionConfig {
|
||||
return &ConnectionConfig{
|
||||
MaxOpenConns: openConns,
|
||||
MaxIdleConns: idleConns,
|
||||
AfterConnect: afterConnectFuncs,
|
||||
}
|
||||
pusherConfig, err := queryConfig.takeRatio(pusherRatio)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("event pusher: %w", err)
|
||||
}
|
||||
|
||||
spoolerConfig, err := queryConfig.takeRatio(projectionRatio)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("projection spooler: %w", err)
|
||||
}
|
||||
|
||||
// subtract the claimed amount
|
||||
if queryConfig.MaxOpenConns > 0 {
|
||||
queryConfig.MaxOpenConns -= pusherConfig.MaxOpenConns + spoolerConfig.MaxOpenConns
|
||||
}
|
||||
if queryConfig.MaxIdleConns > 0 {
|
||||
queryConfig.MaxIdleConns -= pusherConfig.MaxIdleConns + spoolerConfig.MaxIdleConns
|
||||
}
|
||||
|
||||
switch purpose {
|
||||
case DBPurposeQuery:
|
||||
return queryConfig, nil
|
||||
case DBPurposeEventPusher:
|
||||
return pusherConfig, nil
|
||||
case DBPurposeProjectionSpooler:
|
||||
return spoolerConfig, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("%w: %v", ErrInvalidPurpose, purpose)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user