-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
database.go
81 lines (67 loc) · 1.84 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package dbresolver
import (
"context"
"time"
"gorm.io/gorm"
)
func (dr *DBResolver) SetConnMaxIdleTime(d time.Duration) *DBResolver {
dr.Call(func(connPool gorm.ConnPool) error {
if conn, ok := connPool.(interface{ SetConnMaxIdleTime(time.Duration) }); ok {
conn.SetConnMaxIdleTime(d)
} else {
dr.DB.Logger.Error(context.Background(), "SetConnMaxIdleTime not implemented for %#v, please use golang v1.15+", conn)
}
return nil
})
return dr
}
func (dr *DBResolver) SetConnMaxLifetime(d time.Duration) *DBResolver {
dr.Call(func(connPool gorm.ConnPool) error {
if conn, ok := connPool.(interface{ SetConnMaxLifetime(time.Duration) }); ok {
conn.SetConnMaxLifetime(d)
} else {
dr.DB.Logger.Error(context.Background(), "SetConnMaxLifetime not implemented for %#v", conn)
}
return nil
})
return dr
}
func (dr *DBResolver) SetMaxIdleConns(n int) *DBResolver {
dr.Call(func(connPool gorm.ConnPool) error {
if conn, ok := connPool.(interface{ SetMaxIdleConns(int) }); ok {
conn.SetMaxIdleConns(n)
} else {
dr.DB.Logger.Error(context.Background(), "SetMaxIdleConns not implemented for %#v", conn)
}
return nil
})
return dr
}
func (dr *DBResolver) SetMaxOpenConns(n int) *DBResolver {
dr.Call(func(connPool gorm.ConnPool) error {
if conn, ok := connPool.(interface{ SetMaxOpenConns(int) }); ok {
conn.SetMaxOpenConns(n)
} else {
dr.DB.Logger.Error(context.Background(), "SetMaxOpenConns not implemented for %#v", conn)
}
return nil
})
return dr
}
func (dr *DBResolver) Call(fc func(connPool gorm.ConnPool) error) error {
if dr.DB != nil {
for _, r := range dr.resolvers {
if err := r.call(fc); err != nil {
return err
}
}
if dr.global != nil {
if err := dr.global.call(fc); err != nil {
return err
}
}
} else {
dr.compileCallbacks = append(dr.compileCallbacks, fc)
}
return nil
}