Skip to content

Commit e78c479

Browse files
Add MaxConns and Idle configurations to dbo
1 parent 9cb0e2b commit e78c479

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

dbo/connect.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dbo
22

33
import (
4+
"time"
5+
46
"gorm.io/gorm"
57
)
68

@@ -18,6 +20,30 @@ func Connect(opts *Options) (i Instance, err error) {
1820
return
1921
}
2022

23+
sqldb, err := db.DB()
24+
25+
if err != nil {
26+
return
27+
}
28+
29+
if opts.MaxOpenConns > 0 {
30+
sqldb.SetMaxOpenConns(opts.MaxOpenConns)
31+
}
32+
33+
if opts.MaxIdleConns > 0 {
34+
sqldb.SetMaxIdleConns(opts.MaxIdleConns)
35+
}
36+
37+
if opts.ConnMaxLifetime > 0 {
38+
duration := time.Millisecond * time.Duration(opts.ConnMaxLifetime)
39+
sqldb.SetConnMaxLifetime(duration)
40+
}
41+
42+
if opts.ConnMaxIdleTime > 0 {
43+
duration := time.Millisecond * time.Duration(opts.ConnMaxIdleTime)
44+
sqldb.SetConnMaxIdleTime(duration)
45+
}
46+
2147
db.Use(NewPlugin())
2248

2349
switch dbDriver(opts) {

dbo/options.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ const (
2020
)
2121

2222
type Options struct {
23-
Driver DRIVER
24-
Host string
25-
Port string
26-
Username string
27-
Password string
28-
DBName string
29-
Charset string
30-
Collation string
31-
DSN string
32-
Engine ENGINE
33-
Config *gorm.Config
23+
Driver DRIVER
24+
Host string
25+
Port string
26+
Username string
27+
Password string
28+
DBName string
29+
Charset string
30+
Collation string
31+
DSN string
32+
Engine ENGINE
33+
Config *gorm.Config
34+
MaxOpenConns int
35+
MaxIdleConns int
36+
ConnMaxLifetime int // Maximum lifetime for a connection (in milliseconds)
37+
ConnMaxIdleTime int // Maximum idle time for a connection (in milliseconds)
3438
}
3539

3640
func (c *Options) getConfig() *gorm.Config {

0 commit comments

Comments
 (0)