Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVOPS-29207 close idle connections after 1 minute #352

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/dbclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ func (p *client) getDB(dbname string) (*sql.DB, error) {
return nil, err
}
u.Path = "/" + dbname
return sql.Open("postgres", u.String())
// thayward: Why is this creating a new pool instead of using p.DB?
db, err := sql.Open("postgres", u.String())
if err != nil {
return nil, err
}
db.SetConnMaxIdleTime(time.Minute)
return db, nil
}

type Config struct {
Expand Down Expand Up @@ -98,6 +104,7 @@ func newPostgresClient(ctx context.Context, cfg Config) (*client, error) {
if err != nil {
return nil, err
}
db.SetConnMaxIdleTime(time.Minute)

// create a new connection to the database to run admin commands.

Expand All @@ -111,6 +118,7 @@ func newPostgresClient(ctx context.Context, cfg Config) (*client, error) {
if err != nil {
return nil, err
}
adminDB.SetConnMaxIdleTime(time.Minute)

if err := adminDB.PingContext(ctx); err != nil {
adminDB.Close()
Expand Down
2 changes: 2 additions & 0 deletions pkg/pgctl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/url"
"os/exec"
"time"

"github.com/go-logr/logr"
)
Expand Down Expand Up @@ -96,6 +97,7 @@ func getDB(dsn string, db *sql.DB) (*sql.DB, error) {
if db, err = sql.Open("postgres", dsn); err != nil {
return nil, err
}
db.SetConnMaxIdleTime(time.Minute)
}
if err = db.Ping(); err != nil {
return nil, err
Expand Down
Loading