-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
57 lines (47 loc) · 1.02 KB
/
option.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
package frodao
import (
"database/sql"
"time"
)
type optionData struct {
connMaxIdleTime time.Duration
connMaxLifetime time.Duration
maxIdleConns int
maxOpenConns int
}
type Option func(o *optionData)
func ConnMaxIdleTime(value time.Duration) Option {
return func(o *optionData) {
o.connMaxIdleTime = value
}
}
func ConnMaxLifetime(value time.Duration) Option {
return func(o *optionData) {
o.connMaxLifetime = value
}
}
func MaxIdleConns(value int) Option {
return func(o *optionData) {
o.maxIdleConns = value
}
}
func MaxOpenConns(value int) Option {
return func(o *optionData) {
o.maxOpenConns = value
}
}
func SetOptions(dbSession *sql.DB, options []Option) {
o := &optionData{
connMaxIdleTime: 0,
connMaxLifetime: 0,
maxIdleConns: 2,
maxOpenConns: 0,
}
for _, opt := range options {
opt(o)
}
dbSession.SetConnMaxIdleTime(o.connMaxIdleTime)
dbSession.SetConnMaxLifetime(o.connMaxLifetime)
dbSession.SetMaxIdleConns(o.maxIdleConns)
dbSession.SetMaxOpenConns(o.maxOpenConns)
}