From 0abea094883c1c0b358fd21f4903a7d9115cc582 Mon Sep 17 00:00:00 2001 From: Maxence Maireaux Date: Mon, 3 Nov 2025 16:09:26 +0100 Subject: [PATCH] feat(postgres): add support for SetConnMaxLifetime Add configuration support for database connection max lifetime: - Add ConnMaxLifetime field to ConnectionOptions struct - Add postgres-conn-max-lifetime CLI flag (default: 0 - disabled) - Call SetConnMaxLifetime in OpenSQLDB when value is non-zero - Include ConnMaxLifetime in connection logging and String() output This allows configuring how long a connection may be reused before being closed and replaced, helping prevent stale connections and ensuring periodic connection refresh in the database pool. --- bun/bunconnect/connect.go | 8 ++++++-- bun/bunconnect/flags.go | 4 ++++ bun/bunconnect/module.go | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/bun/bunconnect/connect.go b/bun/bunconnect/connect.go index f856443d..aed72520 100644 --- a/bun/bunconnect/connect.go +++ b/bun/bunconnect/connect.go @@ -20,12 +20,13 @@ type ConnectionOptions struct { MaxIdleConns int MaxOpenConns int ConnMaxIdleTime time.Duration + ConnMaxLifetime time.Duration Connector func(dsn string) (driver.Connector, error) `json:",omitempty"` } func (opts ConnectionOptions) String() string { - return fmt.Sprintf("dsn=%s, max-idle-conns=%d, max-open-conns=%d, conn-max-idle-time=%s", - opts.DatabaseSourceName, opts.MaxIdleConns, opts.MaxOpenConns, opts.ConnMaxIdleTime) + return fmt.Sprintf("dsn=%s, max-idle-conns=%d, max-open-conns=%d, conn-max-idle-time=%s, conn-max-lifetime=%s", + opts.DatabaseSourceName, opts.MaxIdleConns, opts.MaxOpenConns, opts.ConnMaxIdleTime, opts.ConnMaxLifetime) } func OpenSQLDB(ctx context.Context, options ConnectionOptions, hooks ...bun.QueryHook) (*bun.DB, error) { @@ -51,6 +52,9 @@ func OpenSQLDB(ctx context.Context, options ConnectionOptions, hooks ...bun.Quer if options.ConnMaxIdleTime != 0 { sqldb.SetConnMaxIdleTime(options.ConnMaxIdleTime) } + if options.ConnMaxLifetime != 0 { + sqldb.SetConnMaxLifetime(options.ConnMaxLifetime) + } if options.MaxOpenConns != 0 { sqldb.SetMaxOpenConns(options.MaxOpenConns) } diff --git a/bun/bunconnect/flags.go b/bun/bunconnect/flags.go index fc0d6e69..8bd0c13d 100644 --- a/bun/bunconnect/flags.go +++ b/bun/bunconnect/flags.go @@ -24,6 +24,7 @@ const ( PostgresMaxIdleConnsFlag = "postgres-max-idle-conns" PostgresMaxOpenConnsFlag = "postgres-max-open-conns" PostgresConnMaxIdleTimeFlag = "postgres-conn-max-idle-time" + PostgresConnMaxLifetimeFlag = "postgres-conn-max-lifetime" ) func AddFlags(flags *pflag.FlagSet) { @@ -31,6 +32,7 @@ func AddFlags(flags *pflag.FlagSet) { flags.Bool(PostgresAWSEnableIAMFlag, false, "Enable AWS IAM authentication") flags.Int(PostgresMaxIdleConnsFlag, 0, "Max Idle connections") flags.Duration(PostgresConnMaxIdleTimeFlag, time.Minute, "Max Idle time for connections") + flags.Duration(PostgresConnMaxLifetimeFlag, 0, "Max lifetime for connections") flags.Int(PostgresMaxOpenConnsFlag, 20, "Max opened connections") } @@ -87,12 +89,14 @@ func ConnectionOptionsFromFlags(cmd *cobra.Command, opts ...Option) (*Connection } maxIdleConns, _ := cmd.Flags().GetInt(PostgresMaxIdleConnsFlag) connMaxIdleConns, _ := cmd.Flags().GetDuration(PostgresConnMaxIdleTimeFlag) + connMaxLifetime, _ := cmd.Flags().GetDuration(PostgresConnMaxLifetimeFlag) maxOpenConns, _ := cmd.Flags().GetInt(PostgresMaxOpenConnsFlag) return &ConnectionOptions{ DatabaseSourceName: postgresUri, MaxIdleConns: maxIdleConns, ConnMaxIdleTime: connMaxIdleConns, + ConnMaxLifetime: connMaxLifetime, MaxOpenConns: maxOpenConns, Connector: connector, }, nil diff --git a/bun/bunconnect/module.go b/bun/bunconnect/module.go index beb8d020..de4f50ac 100644 --- a/bun/bunconnect/module.go +++ b/bun/bunconnect/module.go @@ -23,6 +23,7 @@ func Module(connectionOptions ConnectionOptions, debug bool) fx.Option { "max-idle-conns": connectionOptions.MaxIdleConns, "max-open-conns": connectionOptions.MaxOpenConns, "max-conn-max-idle-time": connectionOptions.ConnMaxIdleTime, + "conn-max-lifetime": connectionOptions.ConnMaxLifetime, }). Infof("opening database connection")