From f23fd221e4f8c68eb7f3be3411f71ad93e23ac97 Mon Sep 17 00:00:00 2001 From: Otto Richter Date: Tue, 23 Apr 2024 00:41:52 +0200 Subject: [PATCH] Limit database max connections by default Our default of unlimited database connections is not sane, because every database has a limit, and our default should just follow this. Otherwise it will lead to issues every time a small instance gets a high traffic peak. Part of https://codeberg.org/forgejo/forgejo/issues/3381 The value of 100 is the lowest value from: - 100 Postgres https://www.postgresql.org/docs/current/runtime-config-connection.html#GUC-MAX-CONNECTIONS - 151 MySQL https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_max_connections - 151 MariaDB https://mariadb.com/docs/server/ref/mdb/system-variables/max_connections/ --- custom/conf/app.example.ini | 4 ++-- modules/setting/database.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 23b41e0853..4eee2cd1ff 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -407,8 +407,8 @@ USER = root ;; Database connection max life time, default is 0 or 3s mysql (See #6804 & #7071 for reasoning) ;CONN_MAX_LIFETIME = 3s ;; -;; Database maximum number of open connections, default is 0 meaning no maximum -;MAX_OPEN_CONNS = 0 +;; Database maximum number of open connections, default is 100 which is the lowest default from Postgres (MariaDB + MySQL default to 151). Ensure you only increase the value if you configured your database server accordingly. +;MAX_OPEN_CONNS = 100 ;; ;; Whether execute database models migrations automatically ;AUTO_MIGRATION = true diff --git a/modules/setting/database.go b/modules/setting/database.go index 6abef42cba..76fae27164 100644 --- a/modules/setting/database.go +++ b/modules/setting/database.go @@ -83,7 +83,7 @@ func loadDBSetting(rootCfg ConfigProvider) { 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.MaxOpenConns = sec.Key("MAX_OPEN_CONNS").MustInt(100) Database.IterateBufferSize = sec.Key("ITERATE_BUFFER_SIZE").MustInt(50) Database.LogSQL = sec.Key("LOG_SQL").MustBool(false)