Skip to content

Commit

Permalink
close idle connections after 1 minute (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
kd7lxl authored Oct 30, 2024
1 parent 39b3b45 commit 31bd8f1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
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

0 comments on commit 31bd8f1

Please sign in to comment.