Skip to content

Commit

Permalink
fix(db): sets max open and idle connections for postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Nov 30, 2023
1 parent b01f1f1 commit 0f6f91c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Code reorganisation, a lot of code has moved, please review the following PRs accordingly [#1444](https://github.com/juanfont/headscale/pull/1444)

### Changes
- Set max open and idle connections for postgres

## 0.22.3 (2023-05-12)

Expand All @@ -19,7 +20,7 @@
### Changes

- Add environment flags to enable pprof (profiling) [#1382](https://github.com/juanfont/headscale/pull/1382)
- Profiles are continously generated in our integration tests.
- Profiles are continously generated in our integration tests.
- Fix systemd service file location in `.deb` packages [#1391](https://github.com/juanfont/headscale/pull/1391)
- Improvements on Noise implementation [#1379](https://github.com/juanfont/headscale/pull/1379)
- Replace node filter logic, ensuring nodes with access can see eachother [#1381](https://github.com/juanfont/headscale/pull/1381)
Expand Down
13 changes: 12 additions & 1 deletion hscontrol/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
const (
dbVersion = "1"

_pgsqlMaxOpenConnections = 10
_pgsqlMaxIdleConnections = 10
_pgsqlMaxConnectionLifetime = 1 * time.Hour

errValueNotFound = Error("not found")
ErrCannotParsePrefix = Error("cannot parse prefix")
)
Expand Down Expand Up @@ -251,10 +255,17 @@ func (h *Headscale) openDB() (*gorm.DB, error) {
sqlDB.SetConnMaxIdleTime(time.Hour)

case Postgres:
db, err = gorm.Open(postgres.Open(h.dbString), &gorm.Config{
db, err := gorm.Open(postgres.Open(h.dbString), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: log,
})

sqlDB, _ := db.DB()
sqlDB.SetMaxOpenConns(_pgsqlMaxOpenConnections)
sqlDB.SetMaxIdleConns(_pgsqlMaxIdleConnections)
sqlDB.SetConnMaxLifetime(_pgsqlMaxConnectionLifetime)

return db, err
}

if err != nil {
Expand Down

0 comments on commit 0f6f91c

Please sign in to comment.