Skip to content

Commit ece768a

Browse files
zeripathguillep2k
andauthored
Expose db.SetMaxOpenConns and allow non MySQL dbs to set conn pool params (#8528) (#8618)
* 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 bac9424 commit ece768a

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

custom/conf/app.ini.sample

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,12 @@ LOG_SQL = true
277277
DB_RETRIES = 10
278278
; Backoff time per DB retry (time.Duration)
279279
DB_RETRY_BACKOFF = 3s
280-
; Max idle database connections on connnection pool, default is 0
281-
MAX_IDLE_CONNS = 0
282-
; Database connection max life time, default is 3s
280+
; Max idle database connections on connnection pool, default is 2
281+
MAX_IDLE_CONNS = 2
282+
; Database connection max life time, default is 0 or 3s mysql (See #6804 & #7071 for reasoning)
283283
CONN_MAX_LIFETIME = 3s
284+
; Database maximum number of open connections, default is 0 meaning no maximum
285+
MAX_OPEN_CONNS = 0
284286

285287
[indexer]
286288
; Issue indexer type, currently support: bleve or db, default is bleve
@@ -823,6 +825,6 @@ TOKEN =
823825
QUEUE_TYPE = channel
824826
; Task queue length, available only when `QUEUE_TYPE` is `channel`.
825827
QUEUE_LENGTH = 1000
826-
; Task queue connction string, available only when `QUEUE_TYPE` is `redis`.
828+
; Task queue connection string, available only when `QUEUE_TYPE` is `redis`.
827829
; If there is a password of redis, use `addrs=127.0.0.1:6379 password=123 db=0`.
828-
QUEUE_CONN_STR = "addrs=127.0.0.1:6379 db=0"
830+
QUEUE_CONN_STR = "addrs=127.0.0.1:6379 db=0"

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
167167
- `LOG_SQL`: **true**: Log the executed SQL.
168168
- `DB_RETRIES`: **10**: How many ORM init / DB connect attempts allowed.
169169
- `DB_RETRY_BACKOFF`: **3s**: time.Duration to wait before trying another ORM init / DB connect attempt, if failure occured.
170-
- `MAX_IDLE_CONNS` **0**: Max idle database connections on connnection pool, default is 0
171-
- `CONN_MAX_LIFETIME` **3s**: Database connection max lifetime
170+
- `MAX_OPEN_CONNS` **0**: Database maximum open connections - default is 0, meaning there is no limit.
171+
- `MAX_IDLE_CONNS` **2**: Max idle database connections on connnection pool, default is 2 - this will be capped to `MAX_OPEN_CONNS`.
172+
- `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).
173+
174+
Please see #8540 & #8273 for further discussion of the appropriate values for `MAX_OPEN_CONNS`, `MAX_IDLE_CONNS` & `CONN_MAX_LIFETIME` and their
175+
relation to port exhaustion.
172176

173177
## Indexer (`indexer`)
174178

models/models.go

Lines changed: 3 additions & 5 deletions
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

Lines changed: 9 additions & 5 deletions
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)