Skip to content

Commit d8161ee

Browse files
zeripathguillep2k
andauthored
Expose db.SetMaxOpenConns and allow non MySQL dbs to set conn pool params (#8528)
* Expose db.SetMaxOpenConns and allow other dbs to set their connection params * Add note about port exhaustion Co-Authored-By: guillep2k <[email protected]>
1 parent 73f8069 commit d8161ee

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

custom/conf/app.ini.sample

+6-4
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,12 @@ LOG_SQL = true
317317
DB_RETRIES = 10
318318
; Backoff time per DB retry (time.Duration)
319319
DB_RETRY_BACKOFF = 3s
320-
; Max idle database connections on connnection pool, default is 0
321-
MAX_IDLE_CONNS = 0
322-
; Database connection max life time, default is 3s
320+
; Max idle database connections on connnection pool, default is 2
321+
MAX_IDLE_CONNS = 2
322+
; Database connection max life time, default is 0 or 3s mysql (See #6804 & #7071 for reasoning)
323323
CONN_MAX_LIFETIME = 3s
324+
; Database maximum number of open connections, default is 0 meaning no maximum
325+
MAX_OPEN_CONNS = 0
324326

325327
[indexer]
326328
; Issue indexer type, currently support: bleve or db, default is bleve
@@ -870,6 +872,6 @@ TOKEN =
870872
QUEUE_TYPE = channel
871873
; Task queue length, available only when `QUEUE_TYPE` is `channel`.
872874
QUEUE_LENGTH = 1000
873-
; Task queue connction string, available only when `QUEUE_TYPE` is `redis`.
875+
; Task queue connection string, available only when `QUEUE_TYPE` is `redis`.
874876
; If there is a password of redis, use `addrs=127.0.0.1:6379 password=123 db=0`.
875877
QUEUE_CONN_STR = "addrs=127.0.0.1:6379 db=0"

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,12 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
192192
- `LOG_SQL`: **true**: Log the executed SQL.
193193
- `DB_RETRIES`: **10**: How many ORM init / DB connect attempts allowed.
194194
- `DB_RETRY_BACKOFF`: **3s**: time.Duration to wait before trying another ORM init / DB connect attempt, if failure occured.
195-
- `MAX_IDLE_CONNS` **0**: Max idle database connections on connnection pool, default is 0
196-
- `CONN_MAX_LIFETIME` **3s**: Database connection max lifetime
195+
- `MAX_OPEN_CONNS` **0**: Database maximum open connections - default is 0, meaning there is no limit.
196+
- `MAX_IDLE_CONNS` **2**: Max idle database connections on connnection pool, default is 2 - this will be capped to `MAX_OPEN_CONNS`.
197+
- `CONN_MAX_LIFETIME` **0 or 3s**: Sets the maximum amount of time a DB connection may be reused - default is 0, meaning there is no limit (except on MySQL where it is 3s - see #6804 & #7071).
198+
199+
Please see #8540 & #8273 for further discussion of the appropriate values for `MAX_OPEN_CONNS`, `MAX_IDLE_CONNS` & `CONN_MAX_LIFETIME` and their
200+
relation to port exhaustion.
197201

198202
## Indexer (`indexer`)
199203

models/models.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,9 @@ func SetEngine() (err error) {
157157
// so use log file to instead print to stdout.
158158
x.SetLogger(NewXORMLogger(setting.Database.LogSQL))
159159
x.ShowSQL(setting.Database.LogSQL)
160-
if setting.Database.UseMySQL {
161-
x.SetMaxIdleConns(setting.Database.MaxIdleConns)
162-
x.SetConnMaxLifetime(setting.Database.ConnMaxLifetime)
163-
}
164-
160+
x.SetMaxOpenConns(setting.Database.MaxOpenConns)
161+
x.SetMaxIdleConns(setting.Database.MaxIdleConns)
162+
x.SetConnMaxLifetime(setting.Database.ConnMaxLifetime)
165163
return nil
166164
}
167165

modules/setting/database.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ var (
4242
DBConnectRetries int
4343
DBConnectBackoff time.Duration
4444
MaxIdleConns int
45+
MaxOpenConns int
4546
ConnMaxLifetime time.Duration
4647
IterateBufferSize int
4748
}{
48-
Timeout: 500,
49-
MaxIdleConns: 0,
50-
ConnMaxLifetime: 3 * time.Second,
49+
Timeout: 500,
5150
}
5251
)
5352

@@ -80,8 +79,13 @@ func InitDBConfig() {
8079
Database.Charset = sec.Key("CHARSET").In("utf8", []string{"utf8", "utf8mb4"})
8180
Database.Path = sec.Key("PATH").MustString(filepath.Join(AppDataPath, "gitea.db"))
8281
Database.Timeout = sec.Key("SQLITE_TIMEOUT").MustInt(500)
83-
Database.MaxIdleConns = sec.Key("MAX_IDLE_CONNS").MustInt(0)
84-
Database.ConnMaxLifetime = sec.Key("CONN_MAX_LIFE_TIME").MustDuration(3 * time.Second)
82+
Database.MaxIdleConns = sec.Key("MAX_IDLE_CONNS").MustInt(2)
83+
if Database.UseMySQL {
84+
Database.ConnMaxLifetime = sec.Key("CONN_MAX_LIFE_TIME").MustDuration(3 * time.Second)
85+
} else {
86+
Database.ConnMaxLifetime = sec.Key("CONN_MAX_LIFE_TIME").MustDuration(0)
87+
}
88+
Database.MaxOpenConns = sec.Key("MAX_OPEN_CONNS").MustInt(0)
8589

8690
Database.IterateBufferSize = sec.Key("ITERATE_BUFFER_SIZE").MustInt(50)
8791
Database.LogSQL = sec.Key("LOG_SQL").MustBool(true)

0 commit comments

Comments
 (0)