Skip to content

Commit cf2e4fc

Browse files
authored
feat(postgres): add support for SetConnMaxLifetime (#522)
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.
1 parent 64522f1 commit cf2e4fc

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

bun/bunconnect/connect.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ type ConnectionOptions struct {
2020
MaxIdleConns int
2121
MaxOpenConns int
2222
ConnMaxIdleTime time.Duration
23+
ConnMaxLifetime time.Duration
2324
Connector func(dsn string) (driver.Connector, error) `json:",omitempty"`
2425
}
2526

2627
func (opts ConnectionOptions) String() string {
27-
return fmt.Sprintf("dsn=%s, max-idle-conns=%d, max-open-conns=%d, conn-max-idle-time=%s",
28-
opts.DatabaseSourceName, opts.MaxIdleConns, opts.MaxOpenConns, opts.ConnMaxIdleTime)
28+
return fmt.Sprintf("dsn=%s, max-idle-conns=%d, max-open-conns=%d, conn-max-idle-time=%s, conn-max-lifetime=%s",
29+
opts.DatabaseSourceName, opts.MaxIdleConns, opts.MaxOpenConns, opts.ConnMaxIdleTime, opts.ConnMaxLifetime)
2930
}
3031

3132
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
5152
if options.ConnMaxIdleTime != 0 {
5253
sqldb.SetConnMaxIdleTime(options.ConnMaxIdleTime)
5354
}
55+
if options.ConnMaxLifetime != 0 {
56+
sqldb.SetConnMaxLifetime(options.ConnMaxLifetime)
57+
}
5458
if options.MaxOpenConns != 0 {
5559
sqldb.SetMaxOpenConns(options.MaxOpenConns)
5660
}

bun/bunconnect/flags.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ const (
2424
PostgresMaxIdleConnsFlag = "postgres-max-idle-conns"
2525
PostgresMaxOpenConnsFlag = "postgres-max-open-conns"
2626
PostgresConnMaxIdleTimeFlag = "postgres-conn-max-idle-time"
27+
PostgresConnMaxLifetimeFlag = "postgres-conn-max-lifetime"
2728
)
2829

2930
func AddFlags(flags *pflag.FlagSet) {
3031
flags.String(PostgresURIFlag, "", "Postgres URI")
3132
flags.Bool(PostgresAWSEnableIAMFlag, false, "Enable AWS IAM authentication")
3233
flags.Int(PostgresMaxIdleConnsFlag, 0, "Max Idle connections")
3334
flags.Duration(PostgresConnMaxIdleTimeFlag, time.Minute, "Max Idle time for connections")
35+
flags.Duration(PostgresConnMaxLifetimeFlag, 0, "Max lifetime for connections")
3436
flags.Int(PostgresMaxOpenConnsFlag, 20, "Max opened connections")
3537
}
3638

@@ -87,12 +89,14 @@ func ConnectionOptionsFromFlags(cmd *cobra.Command, opts ...Option) (*Connection
8789
}
8890
maxIdleConns, _ := cmd.Flags().GetInt(PostgresMaxIdleConnsFlag)
8991
connMaxIdleConns, _ := cmd.Flags().GetDuration(PostgresConnMaxIdleTimeFlag)
92+
connMaxLifetime, _ := cmd.Flags().GetDuration(PostgresConnMaxLifetimeFlag)
9093
maxOpenConns, _ := cmd.Flags().GetInt(PostgresMaxOpenConnsFlag)
9194

9295
return &ConnectionOptions{
9396
DatabaseSourceName: postgresUri,
9497
MaxIdleConns: maxIdleConns,
9598
ConnMaxIdleTime: connMaxIdleConns,
99+
ConnMaxLifetime: connMaxLifetime,
96100
MaxOpenConns: maxOpenConns,
97101
Connector: connector,
98102
}, nil

bun/bunconnect/module.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func Module(connectionOptions ConnectionOptions, debug bool) fx.Option {
2323
"max-idle-conns": connectionOptions.MaxIdleConns,
2424
"max-open-conns": connectionOptions.MaxOpenConns,
2525
"max-conn-max-idle-time": connectionOptions.ConnMaxIdleTime,
26+
"conn-max-lifetime": connectionOptions.ConnMaxLifetime,
2627
}).
2728
Infof("opening database connection")
2829

0 commit comments

Comments
 (0)