From 849de070644b6b2ce7832ca63794904fa876efe5 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Wed, 21 Feb 2024 12:17:16 +0000 Subject: [PATCH] feat(xorm): add max idle time setting for db connections (#2418) Add a new optional `CONN_MAX_IDLETIME`[^1] This allows to set the `SetConnMaxIdleTime` on `database/sql`. It's useful to allow to close more idle connections to reduce database connections, especially on postgresql. For me i would like to use it to set a higher max idle connections but they will all be closed after being idle. So also the last idle connection will be closed when there is no load on forgejo. I also use it with max connection lifetime, because currently `database/sql` doesn't detect a postgresql master change[^2] and i'll get `[E] can't update runner status: pq: cannot execute UPDATE in a read-only transaction`[^3] on forgejo until the connection is closed. [^1]: https://pkg.go.dev/database/sql#DB.SetConnMaxIdleTime [^2]: https://stackoverflow.com/questions/51858659/how-to-safely-discard-golang-database-sql-pooled-connections-for-example-when-t [^3]: https://matrix.to/#/!zpNKWqkiEOyljSMQDK:matrix.org/$_AJft_amsGn5hXGOYw75JoBJQnW3aKJEpb-Iw53L_TU?via=schinas.net&via=matrix.org&via=nitro.chat Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2418 Reviewed-by: Gusted Reviewed-by: Earl Warren Co-authored-by: Michael Kriese Co-committed-by: Michael Kriese --- models/db/engine.go | 1 + modules/setting/database.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/models/db/engine.go b/models/db/engine.go index f1162ebd6e..660ea1f5e3 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -146,6 +146,7 @@ func InitEngine(ctx context.Context) error { xormEngine.SetMaxOpenConns(setting.Database.MaxOpenConns) xormEngine.SetMaxIdleConns(setting.Database.MaxIdleConns) xormEngine.SetConnMaxLifetime(setting.Database.ConnMaxLifetime) + xormEngine.SetConnMaxIdleTime(setting.Database.ConnMaxIdleTime) xormEngine.SetDefaultContext(ctx) if setting.Database.SlowQueryThreshold > 0 { diff --git a/modules/setting/database.go b/modules/setting/database.go index c7bc92e673..47d79d0de9 100644 --- a/modules/setting/database.go +++ b/modules/setting/database.go @@ -42,6 +42,7 @@ var ( DBConnectBackoff time.Duration MaxIdleConns int MaxOpenConns int + ConnMaxIdleTime time.Duration ConnMaxLifetime time.Duration IterateBufferSize int AutoMigration bool @@ -81,6 +82,7 @@ func loadDBSetting(rootCfg ConfigProvider) { } else { Database.ConnMaxLifetime = sec.Key("CONN_MAX_LIFETIME").MustDuration(0) } + Database.ConnMaxIdleTime = sec.Key("CONN_MAX_IDLETIME").MustDuration(0) Database.MaxOpenConns = sec.Key("MAX_OPEN_CONNS").MustInt(0) Database.IterateBufferSize = sec.Key("ITERATE_BUFFER_SIZE").MustInt(50)