Add pg pool tuning options from kit v0.10.0

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2026-05-12 09:39:39 +02:00
parent 51a8380475
commit d88ae3288b
6 changed files with 69 additions and 28 deletions

View File

@@ -93,6 +93,10 @@ spec:
value: {{ .Values.postgresql.maxConnIdleTimeSeconds | default "1800" | quote }} value: {{ .Values.postgresql.maxConnIdleTimeSeconds | default "1800" | quote }}
- name: PG_MAX_CONN_LIFETIME_SECONDS - name: PG_MAX_CONN_LIFETIME_SECONDS
value: {{ .Values.postgresql.maxConnLifetimeSeconds | default "3600" | quote }} value: {{ .Values.postgresql.maxConnLifetimeSeconds | default "3600" | quote }}
- name: PG_MAX_CONN_LIFETIME_JITTER_SECONDS
value: {{ .Values.postgresql.maxConnLifetimeJitterSeconds | default "300" | quote }}
- name: PG_HEALTH_CHECK_PERIOD_SECONDS
value: {{ .Values.postgresql.healthCheckPeriodSeconds | default "60" | quote }}
{{- if .Values.postgresql.caBundle }} {{- if .Values.postgresql.caBundle }}
- name: PG_CA_BUNDLE - name: PG_CA_BUNDLE
valueFrom: valueFrom:

View File

@@ -311,6 +311,12 @@ postgresql:
# Maximum lifetime of a connection before being recycled, in # Maximum lifetime of a connection before being recycled, in
# seconds. Default 3600 (1 hour) matches pgx defaults. # seconds. Default 3600 (1 hour) matches pgx defaults.
maxConnLifetimeSeconds: 3600 maxConnLifetimeSeconds: 3600
# Jitter added to maxConnLifetime to spread reconnect storms, in
# seconds. Default 300 (5 minutes).
maxConnLifetimeJitterSeconds: 300
# How often the pool checks idle connections, in seconds. Default 60
# (1 minute).
healthCheckPeriodSeconds: 60
# PostgreSQL TLS/SSL configuration # PostgreSQL TLS/SSL configuration
# caBundle: | # caBundle: |
# -----BEGIN CERTIFICATE----- # -----BEGIN CERTIFICATE-----

View File

@@ -81,16 +81,18 @@ func (b *Builder) Build() (*probodconfig.FullConfig, error) {
ExtraHeaderFields: make(map[string]string), ExtraHeaderFields: make(map[string]string),
}, },
Pg: probodconfig.PgConfig{ Pg: probodconfig.PgConfig{
Addr: b.getEnvOrDefault("PG_ADDR", "localhost:5432"), Addr: b.getEnvOrDefault("PG_ADDR", "localhost:5432"),
Username: b.getEnvOrDefault("PG_USERNAME", "probod"), Username: b.getEnvOrDefault("PG_USERNAME", "probod"),
Password: b.getEnvOrDefault("PG_PASSWORD", "probod"), Password: b.getEnvOrDefault("PG_PASSWORD", "probod"),
Database: b.getEnvOrDefault("PG_DATABASE", "probod"), Database: b.getEnvOrDefault("PG_DATABASE", "probod"),
PoolSize: int32(b.getEnvIntOrDefault("PG_POOL_SIZE", 100)), PoolSize: int32(b.getEnvIntOrDefault("PG_POOL_SIZE", 100)),
MinPoolSize: int32(b.getEnvIntOrDefault("PG_MIN_POOL_SIZE", 10)), MinPoolSize: int32(b.getEnvIntOrDefault("PG_MIN_POOL_SIZE", 10)),
MaxConnIdleTimeSeconds: b.getEnvIntOrDefault("PG_MAX_CONN_IDLE_TIME_SECONDS", 1800), MaxConnIdleTimeSeconds: b.getEnvIntOrDefault("PG_MAX_CONN_IDLE_TIME_SECONDS", 1800),
MaxConnLifetimeSeconds: b.getEnvIntOrDefault("PG_MAX_CONN_LIFETIME_SECONDS", 3600), MaxConnLifetimeSeconds: b.getEnvIntOrDefault("PG_MAX_CONN_LIFETIME_SECONDS", 3600),
CACertBundle: pgCACertBundle, MaxConnLifetimeJitterSeconds: b.getEnvIntOrDefault("PG_MAX_CONN_LIFETIME_JITTER_SECONDS", 300),
Debug: b.getEnvBoolOrDefault("PG_DEBUG", false), HealthCheckPeriodSeconds: b.getEnvIntOrDefault("PG_HEALTH_CHECK_PERIOD_SECONDS", 60),
CACertBundle: pgCACertBundle,
Debug: b.getEnvBoolOrDefault("PG_DEBUG", false),
}, },
Auth: probodconfig.AuthConfig{ Auth: probodconfig.AuthConfig{
DisableSignup: b.getEnvBoolOrDefault("AUTH_DISABLE_SIGNUP", false), DisableSignup: b.getEnvBoolOrDefault("AUTH_DISABLE_SIGNUP", false),

View File

@@ -154,6 +154,8 @@ func TestBuilder_Build_Defaults(t *testing.T) {
assert.Equal(t, int32(10), cfg.Probod.Pg.MinPoolSize) assert.Equal(t, int32(10), cfg.Probod.Pg.MinPoolSize)
assert.Equal(t, 1800, cfg.Probod.Pg.MaxConnIdleTimeSeconds) assert.Equal(t, 1800, cfg.Probod.Pg.MaxConnIdleTimeSeconds)
assert.Equal(t, 3600, cfg.Probod.Pg.MaxConnLifetimeSeconds) assert.Equal(t, 3600, cfg.Probod.Pg.MaxConnLifetimeSeconds)
assert.Equal(t, 300, cfg.Probod.Pg.MaxConnLifetimeJitterSeconds)
assert.Equal(t, 60, cfg.Probod.Pg.HealthCheckPeriodSeconds)
assert.False(t, cfg.Probod.Pg.Debug) assert.False(t, cfg.Probod.Pg.Debug)
// Auth config // Auth config
@@ -254,6 +256,8 @@ func TestBuilder_Build_CustomValues(t *testing.T) {
env["PG_MIN_POOL_SIZE"] = "25" env["PG_MIN_POOL_SIZE"] = "25"
env["PG_MAX_CONN_IDLE_TIME_SECONDS"] = "900" env["PG_MAX_CONN_IDLE_TIME_SECONDS"] = "900"
env["PG_MAX_CONN_LIFETIME_SECONDS"] = "7200" env["PG_MAX_CONN_LIFETIME_SECONDS"] = "7200"
env["PG_MAX_CONN_LIFETIME_JITTER_SECONDS"] = "600"
env["PG_HEALTH_CHECK_PERIOD_SECONDS"] = "30"
env["PG_DEBUG"] = "true" env["PG_DEBUG"] = "true"
// Auth // Auth
env["AUTH_DISABLE_SIGNUP"] = "true" env["AUTH_DISABLE_SIGNUP"] = "true"
@@ -331,6 +335,8 @@ func TestBuilder_Build_CustomValues(t *testing.T) {
assert.Equal(t, int32(25), cfg.Probod.Pg.MinPoolSize) assert.Equal(t, int32(25), cfg.Probod.Pg.MinPoolSize)
assert.Equal(t, 900, cfg.Probod.Pg.MaxConnIdleTimeSeconds) assert.Equal(t, 900, cfg.Probod.Pg.MaxConnIdleTimeSeconds)
assert.Equal(t, 7200, cfg.Probod.Pg.MaxConnLifetimeSeconds) assert.Equal(t, 7200, cfg.Probod.Pg.MaxConnLifetimeSeconds)
assert.Equal(t, 600, cfg.Probod.Pg.MaxConnLifetimeJitterSeconds)
assert.Equal(t, 30, cfg.Probod.Pg.HealthCheckPeriodSeconds)
assert.True(t, cfg.Probod.Pg.Debug) assert.True(t, cfg.Probod.Pg.Debug)
// Auth // Auth
assert.True(t, cfg.Probod.Auth.DisableSignup) assert.True(t, cfg.Probod.Auth.DisableSignup)

View File

@@ -91,14 +91,16 @@ func New() *Implm {
Addr: "localhost:8080", Addr: "localhost:8080",
}, },
Pg: PgConfig{ Pg: PgConfig{
Addr: "localhost:5432", Addr: "localhost:5432",
Username: "probod", Username: "probod",
Password: "probod", Password: "probod",
Database: "probod", Database: "probod",
PoolSize: 100, PoolSize: 100,
MinPoolSize: 10, MinPoolSize: 10,
MaxConnIdleTimeSeconds: 1800, MaxConnIdleTimeSeconds: 1800,
MaxConnLifetimeSeconds: 3600, MaxConnLifetimeSeconds: 3600,
MaxConnLifetimeJitterSeconds: 300,
HealthCheckPeriodSeconds: 60,
}, },
ChromeDPAddr: "localhost:9222", ChromeDPAddr: "localhost:9222",
Auth: AuthConfig{ Auth: AuthConfig{
@@ -209,6 +211,7 @@ func (impl *Implm) Run(
pgClient, err := pg.NewClient( pgClient, err := pg.NewClient(
impl.cfg.Pg.Options( impl.cfg.Pg.Options(
pg.WithApplicationName("probod"),
pg.WithLogger(l), pg.WithLogger(l),
pg.WithRegisterer(r), pg.WithRegisterer(r),
pg.WithTracerProvider(tp), pg.WithTracerProvider(tp),

View File

@@ -23,16 +23,18 @@ import (
) )
type PgConfig struct { type PgConfig struct {
Addr string `json:"addr"` Addr string `json:"addr"`
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
Database string `json:"database"` Database string `json:"database"`
PoolSize int32 `json:"pool-size"` PoolSize int32 `json:"pool-size"`
MinPoolSize int32 `json:"min-pool-size"` MinPoolSize int32 `json:"min-pool-size"`
MaxConnIdleTimeSeconds int `json:"max-conn-idle-time-seconds"` MaxConnIdleTimeSeconds int `json:"max-conn-idle-time-seconds"`
MaxConnLifetimeSeconds int `json:"max-conn-lifetime-seconds"` MaxConnLifetimeSeconds int `json:"max-conn-lifetime-seconds"`
CACertBundle string `json:"ca-cert-bundle"` MaxConnLifetimeJitterSeconds int `json:"max-conn-lifetime-jitter-seconds"`
Debug bool `json:"debug"` HealthCheckPeriodSeconds int `json:"health-check-period-seconds"`
CACertBundle string `json:"ca-cert-bundle"`
Debug bool `json:"debug"`
} }
func (cfg PgConfig) Options(options ...pg.Option) []pg.Option { func (cfg PgConfig) Options(options ...pg.Option) []pg.Option {
@@ -66,6 +68,24 @@ func (cfg PgConfig) Options(options ...pg.Option) []pg.Option {
) )
} }
if cfg.MaxConnLifetimeJitterSeconds > 0 {
opts = append(
opts,
pg.WithMaxConnLifetimeJitter(
time.Duration(cfg.MaxConnLifetimeJitterSeconds)*time.Second,
),
)
}
if cfg.HealthCheckPeriodSeconds > 0 {
opts = append(
opts,
pg.WithHealthCheckPeriod(
time.Duration(cfg.HealthCheckPeriodSeconds)*time.Second,
),
)
}
if cfg.Debug { if cfg.Debug {
opts = append(opts, pg.WithDebug()) opts = append(opts, pg.WithDebug())
} }