Skip to content

Conversation

@flemzord
Copy link
Member

@flemzord flemzord commented Nov 3, 2025

Summary

This PR adds support for configuring database connection max lifetime using SetConnMaxLifetime.

Changes

  • Added ConnMaxLifetime field to ConnectionOptions struct (bun/bunconnect/connect.go:23)
  • Added --postgres-conn-max-lifetime CLI flag with default value of 0 (disabled)
  • Added SetConnMaxLifetime() call in OpenSQLDB() when value is non-zero (bun/bunconnect/connect.go:55-57)
  • Updated connection logging to include conn-max-lifetime parameter

Benefits

According to database/sql documentation, SetConnMaxLifetime sets the maximum amount of time a connection may be reused. This helps:

  • Prevent stale or degraded connections from lingering in the pool
  • Accommodate databases that disconnect idle sessions after extended periods
  • Ensure connections are periodically refreshed rather than held indefinitely

Usage

# Set connection lifetime to 10 minutes
--postgres-conn-max-lifetime=10m

# Set connection lifetime to 1 hour
--postgres-conn-max-lifetime=1h

# Disable (default)
--postgres-conn-max-lifetime=0

Test plan

  • Verify the new --postgres-conn-max-lifetime flag is available
  • Test with different lifetime values (e.g., 10m, 1h)
  • Verify connections are properly closed and replaced after the specified lifetime
  • Check that logging includes the new connection configuration parameter

@flemzord flemzord requested a review from a team as a code owner November 3, 2025 15:05
@coderabbitai
Copy link

coderabbitai bot commented Nov 3, 2025

Walkthrough

Adds a new ConnectionOptions field ConnMaxLifetime time.Duration, exposes a new flag to set it, threads the value through flag parsing into ConnectionOptions, logs the value when opening the DB, and applies it to the opened *sql.DB via SetConnMaxLifetime when non-zero.

Changes

Cohort / File(s) Summary
Connection options and application
bun/bunconnect/connect.go
Added ConnMaxLifetime time.Duration to ConnectionOptions; updated string representation to include conn-max-lifetime; when opening the DB, apply SetConnMaxLifetime on the resulting *sql.DB if ConnMaxLifetime is non-zero.
Flags / CLI wiring
bun/bunconnect/flags.go
Added PostgresConnMaxLifetimeFlag constant and flag registration; parse flag as a duration and populate ConnectionOptions.ConnMaxLifetime from flags.
Logging
bun/bunconnect/module.go
Added "conn-max-lifetime" field to the "opening database connection" log entry sourced from connectionOptions.ConnMaxLifetime.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant CLI as CLI (flags)
    participant Config as ConnectionOptions
    participant Module as module.Open (logs)
    participant DB as OpenSQLDB / *sql.DB

    CLI->>Config: parse flags (includes conn-max-lifetime)
    Config->>Module: pass ConnectionOptions
    Module->>DB: OpenSQLDB(connectionOptions)
    DB->>DB: sql.Open / connector setup
    DB->>DB: SetConnMaxLifetime(conn-max-lifetime)  %% applied if non-zero
    DB-->>Module: return *sql.DB
    Module-->>CLI: log "opening database connection" (includes conn-max-lifetime)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Areas to check:
    • Correct parsing and defaulting of the new duration flag in flags.go.
    • Proper threading of ConnMaxLifetime into ConnectionOptions.
    • Ensure SetConnMaxLifetime is invoked only when non-zero and does not alter existing connection parameter handling (MaxOpenConns, MaxIdleConns, ConnMaxIdleTime).
    • Verify the new log field is formatted as intended in module.go.

Poem

🐰 I nudge a flag, a tiny tweak so bright,
Conn lifetime set to keep the pool polite,
Logs whisper the span in morning's light,
Conns rest and run, just right —
Hoppity hops, dbs sleep tight.

Pre-merge checks and finishing touches

✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title 'feat(postgres): add support for SetConnMaxLifetime' directly and clearly summarizes the main change in the changeset. The title accurately reflects that this PR adds support for configuring database connection maximum lifetime via SetConnMaxLifetime, which is the primary objective evidenced by the changes across all three modified files (connect.go, flags.go, and module.go).
Description check ✅ Passed The pull request description clearly outlines the purpose of the changes: adding support for configuring database connection max lifetime using SetConnMaxLifetime. It provides specific details about what was changed (new ConnMaxLifetime field, CLI flag, SetConnMaxLifetime call, and logging updates), references official Go documentation for context, explains the benefits, shows usage examples, and includes a test plan. The description is well-organized and directly related to the changeset.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/postgres-conn-max-lifetime

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov
Copy link

codecov bot commented Nov 3, 2025

Codecov Report

❌ Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 27.63%. Comparing base (a20343d) to head (0abea09).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
bun/bunconnect/connect.go 0.00% 4 Missing ⚠️
bun/bunconnect/flags.go 0.00% 3 Missing ⚠️
bun/bunconnect/module.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #522      +/-   ##
==========================================
- Coverage   34.77%   27.63%   -7.15%     
==========================================
  Files         139      162      +23     
  Lines        6061     6605     +544     
==========================================
- Hits         2108     1825     -283     
- Misses       3842     4669     +827     
  Partials      111      111              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.
@flemzord flemzord added this pull request to the merge queue Nov 3, 2025
Merged via the queue into main with commit cf2e4fc Nov 3, 2025
6 of 8 checks passed
@flemzord flemzord deleted the feat/postgres-conn-max-lifetime branch November 3, 2025 20:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants