...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package mysql
16
17 import (
18 "time"
19
20 "github.com/jmoiron/sqlx"
21 )
22
23
24 type Options interface {
25 applyConnMaxIdleTime(*sqlx.DB)
26 applyConnMaxLifetime(*sqlx.DB)
27 applyMaxIdleConns(*sqlx.DB)
28 applyMaxOpenConns(*sqlx.DB)
29 }
30
31
32 type noOpOptionImpl struct{}
33
34
35 func (noOpOptionImpl) applyConnMaxIdleTime(_ *sqlx.DB) {}
36
37
38 func (noOpOptionImpl) applyConnMaxLifetime(_ *sqlx.DB) {}
39
40
41 func (noOpOptionImpl) applyMaxOpenConns(_ *sqlx.DB) {}
42
43
44 func (noOpOptionImpl) applyMaxIdleConns(_ *sqlx.DB) {}
45
46
47 type RequestConnMaxIdleTime struct {
48 noOpOptionImpl
49 idleTime time.Duration
50 }
51
52
53 func (r RequestConnMaxIdleTime) applyConnMaxIdleTime(db *sqlx.DB) {
54 if db != nil {
55 db.SetConnMaxIdleTime(r.idleTime)
56 }
57 }
58
59
60 func WithConnMaxIdleTime(idleTime time.Duration) RequestConnMaxIdleTime {
61 return RequestConnMaxIdleTime{idleTime: idleTime}
62 }
63
64
65 type RequestConnMaxLifetime struct {
66 noOpOptionImpl
67 lifetime time.Duration
68 }
69
70
71 func (r RequestConnMaxLifetime) applyConnMaxLifetime(db *sqlx.DB) {
72 if db != nil {
73 db.SetConnMaxLifetime(r.lifetime)
74 }
75 }
76
77
78 func WithConnMaxLifetime(lifetime time.Duration) RequestConnMaxLifetime {
79 return RequestConnMaxLifetime{lifetime: lifetime}
80 }
81
82
83 type RequestMaxIdleConns struct {
84 noOpOptionImpl
85 idleConns int
86 }
87
88
89 func (r RequestMaxIdleConns) applyMaxIdleConns(db *sqlx.DB) {
90 if db != nil {
91 db.SetMaxIdleConns(r.idleConns)
92 }
93 }
94
95
96 func WithMaxIdleConns(idleConns int) RequestMaxIdleConns {
97 return RequestMaxIdleConns{idleConns: idleConns}
98 }
99
100
101 type RequestMaxOpenConns struct {
102 noOpOptionImpl
103 openConns int
104 }
105
106
107 func (r RequestMaxOpenConns) applyMaxOpenConns(db *sqlx.DB) {
108 if db != nil {
109 db.SetMaxOpenConns(r.openConns)
110 }
111 }
112
113
114 func WithMaxOpenConns(openConns int) RequestMaxOpenConns {
115 return RequestMaxOpenConns{openConns: openConns}
116 }
117
View as plain text