File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package dbo
22
33import (
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 ) {
Original file line number Diff line number Diff line change @@ -20,17 +20,21 @@ const (
2020)
2121
2222type 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
3640func (c * Options ) getConfig () * gorm.Config {
You can’t perform that action at this time.
0 commit comments