/
option.go
230 lines (207 loc) · 5.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package mysql
import (
"context"
"crypto/tls"
"time"
"github.com/vdaas/vald/internal/net"
"github.com/vdaas/vald/internal/net/tcp"
"github.com/vdaas/vald/internal/timeutil"
)
// Option represents the functional option for mySQLClient.
type Option func(*mySQLClient) error
var (
defaultOpts = []Option{
WithCharset("utf8mb4"),
WithTimezone("Local"),
WithInitialPingDuration("30ms"),
WithInitialPingTimeLimit("5m"),
// WithConnectionLifeTimeLimit("2m"),
// WithMaxOpenConns(40),
// WithMaxIdleConns(50),
}
)
// WithTimezone returns the option to set the timezone.
func WithTimezone(tz string) Option {
return func(m *mySQLClient) error {
if tz != "" {
m.timezone = tz
}
return nil
}
}
// WithCharset returns the option to set the charset.
func WithCharset(cs string) Option {
return func(m *mySQLClient) error {
if cs != "" {
m.charset = cs
}
return nil
}
}
// WithDB returns the option to set the db.
func WithDB(db string) Option {
return func(m *mySQLClient) error {
if db != "" {
m.db = db
}
return nil
}
}
// WithHost returns the option to set the host.
func WithHost(host string) Option {
return func(m *mySQLClient) error {
if host != "" {
m.host = host
}
return nil
}
}
// WithPort returns the option to set the port.
func WithPort(port int) Option {
return func(m *mySQLClient) error {
m.port = port
return nil
}
}
// WithUser returns the option to set the user.
func WithUser(user string) Option {
return func(m *mySQLClient) error {
if user != "" {
m.user = user
}
return nil
}
}
// WithPass returns the option to set the pass.
func WithPass(pass string) Option {
return func(m *mySQLClient) error {
if pass != "" {
m.pass = pass
}
return nil
}
}
// WithName returns the option to sst the name.
func WithName(name string) Option {
return func(m *mySQLClient) error {
if name != "" {
m.name = name
}
return nil
}
}
// WithInitialPingTimeLimit returns the option to set the initialPingTimeLimit.
func WithInitialPingTimeLimit(lim string) Option {
return func(m *mySQLClient) error {
if lim == "" {
return nil
}
pd, err := timeutil.Parse(lim)
if err != nil {
pd = time.Second * 30
}
m.initialPingTimeLimit = pd
return nil
}
}
// WithInitialPingDuration returns the option to set the initialPingDuration.
func WithInitialPingDuration(dur string) Option {
return func(m *mySQLClient) error {
if dur == "" {
return nil
}
pd, err := timeutil.Parse(dur)
if err != nil {
pd = time.Millisecond * 50
}
m.initialPingDuration = pd
return nil
}
}
// WithConnectionLifeTimeLimit returns the option to set the connMaxLifeTime.
func WithConnectionLifeTimeLimit(dur string) Option {
return func(m *mySQLClient) error {
if dur == "" {
return nil
}
pd, err := timeutil.Parse(dur)
if err != nil {
pd = time.Second * 30
}
m.connMaxLifeTime = pd
return nil
}
}
// WithMaxIdleConns returns the option to set the maxIdleConns.
// If conns is negative numner, no idle connections are retained.
// ref: https://golang.org/src/database/sql/sql.go?s=24983:25019#L879
func WithMaxIdleConns(conns int) Option {
return func(m *mySQLClient) error {
if conns != 0 {
m.maxIdleConns = conns
}
return nil
}
}
// WithMaxOpenConns returns the option to set the maxOpenConns.
// If conns is negative numner, no limit on the number of open connections.
// ref: https://golang.org/src/database/sql/sql.go?s=24983:25019#L923
func WithMaxOpenConns(conns int) Option {
return func(m *mySQLClient) error {
if conns != 0 {
m.maxOpenConns = conns
}
return nil
}
}
// WithTLSConfig returns the option to set the tlsConfig.
func WithTLSConfig(cfg *tls.Config) Option {
return func(m *mySQLClient) error {
if cfg != nil {
m.tlsConfig = cfg
}
return nil
}
}
// WithDialer returns the option to set the dialer.
func WithDialer(der tcp.Dialer) Option {
return func(m *mySQLClient) error {
if der != nil {
m.dialer = der
}
return nil
}
}
// WithDialerFunc returns the option to set the dialer function.
func WithDialerFunc(der func(ctx context.Context, addr, port string) (net.Conn, error)) Option {
return func(m *mySQLClient) error {
if der != nil {
m.dialerFunc = der
}
return nil
}
}
// WithEventReceiver returns the option to set the eventReceiver.
func WithEventReceiver(er EventReceiver) Option {
return func(m *mySQLClient) error {
if er != nil {
m.eventReceiver = er
}
return nil
}
}