From 0f6f91c7e6481b9ca845371c676b18e389453944 Mon Sep 17 00:00:00 2001 From: Pallab Pain Date: Thu, 30 Nov 2023 18:35:27 +0530 Subject: [PATCH] fix(db): sets max open and idle connections for postgres --- CHANGELOG.md | 3 ++- hscontrol/db.go | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c2976e9e1..67ec6c931d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) @@ -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) diff --git a/hscontrol/db.go b/hscontrol/db.go index 14df4b3bf1..337ef4fd0f 100644 --- a/hscontrol/db.go +++ b/hscontrol/db.go @@ -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") ) @@ -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 {