loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <gusted@noreply.codeberg.org>
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-committed-by: Michael Kriese <michael.kriese@visualon.de>

authored by

Michael Kriese
Michael Kriese
and committed by
Earl Warren
849de070 0533022d

+3
+1
models/db/engine.go
··· 146 146 xormEngine.SetMaxOpenConns(setting.Database.MaxOpenConns) 147 147 xormEngine.SetMaxIdleConns(setting.Database.MaxIdleConns) 148 148 xormEngine.SetConnMaxLifetime(setting.Database.ConnMaxLifetime) 149 + xormEngine.SetConnMaxIdleTime(setting.Database.ConnMaxIdleTime) 149 150 xormEngine.SetDefaultContext(ctx) 150 151 151 152 if setting.Database.SlowQueryThreshold > 0 {
+2
modules/setting/database.go
··· 42 42 DBConnectBackoff time.Duration 43 43 MaxIdleConns int 44 44 MaxOpenConns int 45 + ConnMaxIdleTime time.Duration 45 46 ConnMaxLifetime time.Duration 46 47 IterateBufferSize int 47 48 AutoMigration bool ··· 81 82 } else { 82 83 Database.ConnMaxLifetime = sec.Key("CONN_MAX_LIFETIME").MustDuration(0) 83 84 } 85 + Database.ConnMaxIdleTime = sec.Key("CONN_MAX_IDLETIME").MustDuration(0) 84 86 Database.MaxOpenConns = sec.Key("MAX_OPEN_CONNS").MustInt(0) 85 87 86 88 Database.IterateBufferSize = sec.Key("ITERATE_BUFFER_SIZE").MustInt(50)