-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_conf_optiongen.go
341 lines (304 loc) · 10.5 KB
/
gen_conf_optiongen.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Code generated by optiongen. DO NOT EDIT.
// optiongen: github.com/timestee/optiongen
package redisson
import (
"sync/atomic"
"time"
"unsafe"
)
// Conf should use NewConf to initialize it
type Conf struct {
Net string `xconf:"net" usage:"网络类型,tcp/unix"`
AlwaysRESP2 bool `xconf:"always_resp2" usage:"always uses RESP2, otherwise it will try using RESP3 first"`
Name string `xconf:"name" usage:"Redis客户端名字"`
MasterName string `xconf:"master_name" usage:"Redis Sentinel模式下,master名字"`
EnableMonitor bool `xconf:"enable_monitor" usage:"是否开启监控"`
Addrs []string `xconf:"addrs" usage:"Redis地址列表"`
DB int `xconf:"db" usage:"Redis实例数据库编号,集群下只能用0"`
Username string `xconf:"username" usage:"Redis用户名"`
Password string `xconf:"password" usage:"Redis用户密码"`
WriteTimeout time.Duration `xconf:"write_timeout" usage:"Redis连接写入的超时时长"`
ConnPoolSize int `xconf:"conn_pool_size" usage:"RedisBlock连接池,默认1000"`
EnableCache bool `xconf:"enable_cache" usage:"是否开启客户端缓存"`
CacheSizeEachConn int `xconf:"cache_size_each_conn" usage:"开启客户端缓存时,单个连接缓存大小,默认128 MiB"`
RingScaleEachConn int `xconf:"ring_scale_each_conn" usage:"单个连接ring buffer大小,默认2 ^ RingScaleEachConn, RingScaleEachConn默认情况下为10"`
Development bool `xconf:"development" usage:"是否为开发模式,开发模式下,使用部分接口会有警告日志输出,会校验多key是否为同一hash槽,会校验部分接口是否满足版本要求"`
T Tester `xconf:"t" usage:"如果设置该值,则启动mock"`
ForceSingleClient bool `xconf:"force_single_client" usage:"ForceSingleClient force the usage of a single client connection, without letting the lib guessing"`
}
// NewConf new Conf
func NewConf(opts ...ConfOption) *Conf {
cc := newDefaultConf()
for _, opt := range opts {
opt(cc)
}
if watchDogConf != nil {
watchDogConf(cc)
}
return cc
}
// ApplyOption apply multiple new option and return the old ones
// sample:
// old := cc.ApplyOption(WithTimeout(time.Second))
// defer cc.ApplyOption(old...)
func (cc *Conf) ApplyOption(opts ...ConfOption) []ConfOption {
var previous []ConfOption
for _, opt := range opts {
previous = append(previous, opt(cc))
}
return previous
}
// ConfOption option func
type ConfOption func(cc *Conf) ConfOption
// WithNet 网络类型,tcp/unix
func WithNet(v string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.Net
cc.Net = v
return WithNet(previous)
}
}
// WithAlwaysRESP2 always uses RESP2, otherwise it will try using RESP3 first
func WithAlwaysRESP2(v bool) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.AlwaysRESP2
cc.AlwaysRESP2 = v
return WithAlwaysRESP2(previous)
}
}
// WithName Redis客户端名字
func WithName(v string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.Name
cc.Name = v
return WithName(previous)
}
}
// WithMasterName Redis Sentinel模式下,master名字
func WithMasterName(v string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.MasterName
cc.MasterName = v
return WithMasterName(previous)
}
}
// WithEnableMonitor 是否开启监控
func WithEnableMonitor(v bool) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.EnableMonitor
cc.EnableMonitor = v
return WithEnableMonitor(previous)
}
}
// WithAddrs Redis地址列表
func WithAddrs(v ...string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.Addrs
cc.Addrs = v
return WithAddrs(previous...)
}
}
// AppendAddrs Redis地址列表
func AppendAddrs(v ...string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.Addrs
cc.Addrs = append(cc.Addrs, v...)
return WithAddrs(previous...)
}
}
// WithDB Redis实例数据库编号,集群下只能用0
func WithDB(v int) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.DB
cc.DB = v
return WithDB(previous)
}
}
// WithUsername Redis用户名
func WithUsername(v string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.Username
cc.Username = v
return WithUsername(previous)
}
}
// WithPassword Redis用户密码
func WithPassword(v string) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.Password
cc.Password = v
return WithPassword(previous)
}
}
// WithWriteTimeout Redis连接写入的超时时长
func WithWriteTimeout(v time.Duration) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.WriteTimeout
cc.WriteTimeout = v
return WithWriteTimeout(previous)
}
}
// WithConnPoolSize RedisBlock连接池,默认1000
func WithConnPoolSize(v int) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.ConnPoolSize
cc.ConnPoolSize = v
return WithConnPoolSize(previous)
}
}
// WithEnableCache 是否开启客户端缓存
func WithEnableCache(v bool) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.EnableCache
cc.EnableCache = v
return WithEnableCache(previous)
}
}
// WithCacheSizeEachConn 开启客户端缓存时,单个连接缓存大小,默认128 MiB
func WithCacheSizeEachConn(v int) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.CacheSizeEachConn
cc.CacheSizeEachConn = v
return WithCacheSizeEachConn(previous)
}
}
// WithRingScaleEachConn 单个连接ring buffer大小,默认2 ^ RingScaleEachConn, RingScaleEachConn默认情况下为10
func WithRingScaleEachConn(v int) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.RingScaleEachConn
cc.RingScaleEachConn = v
return WithRingScaleEachConn(previous)
}
}
// WithDevelopment 是否为开发模式,开发模式下,使用部分接口会有警告日志输出,会校验多key是否为同一hash槽,会校验部分接口是否满足版本要求
func WithDevelopment(v bool) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.Development
cc.Development = v
return WithDevelopment(previous)
}
}
// WithT 如果设置该值,则启动mock
func WithT(v Tester) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.T
cc.T = v
return WithT(previous)
}
}
// WithForceSingleClient ForceSingleClient force the usage of a single client connection, without letting the lib guessing
func WithForceSingleClient(v bool) ConfOption {
return func(cc *Conf) ConfOption {
previous := cc.ForceSingleClient
cc.ForceSingleClient = v
return WithForceSingleClient(previous)
}
}
// InstallConfWatchDog the installed func will called when NewConf called
func InstallConfWatchDog(dog func(cc *Conf)) { watchDogConf = dog }
// watchDogConf global watch dog
var watchDogConf func(cc *Conf)
// setConfDefaultValue default Conf value
func setConfDefaultValue(cc *Conf) {
for _, opt := range [...]ConfOption{
WithNet("tcp"),
WithAlwaysRESP2(false),
WithName(""),
WithMasterName(""),
WithEnableMonitor(true),
WithAddrs([]string{"127.0.0.1:6379"}...),
WithDB(0),
WithUsername(""),
WithPassword(""),
WithWriteTimeout(10 * time.Second),
WithConnPoolSize(0),
WithEnableCache(true),
WithCacheSizeEachConn(0),
WithRingScaleEachConn(0),
WithDevelopment(true),
WithT(nil),
WithForceSingleClient(false),
} {
opt(cc)
}
}
// newDefaultConf new default Conf
func newDefaultConf() *Conf {
cc := &Conf{}
setConfDefaultValue(cc)
return cc
}
// AtomicSetFunc used for XConf
func (cc *Conf) AtomicSetFunc() func(interface{}) { return AtomicConfSet }
// atomicConf global *Conf holder
var atomicConf unsafe.Pointer
// onAtomicConfSet global call back when AtomicConfSet called by XConf.
// use ConfInterface.ApplyOption to modify the updated cc
// if passed in cc not valid, then return false, cc will not set to atomicConf
var onAtomicConfSet func(cc ConfInterface) bool
// InstallCallbackOnAtomicConfSet install callback
func InstallCallbackOnAtomicConfSet(callback func(cc ConfInterface) bool) { onAtomicConfSet = callback }
// AtomicConfSet atomic setter for *Conf
func AtomicConfSet(update interface{}) {
cc := update.(*Conf)
if onAtomicConfSet != nil && !onAtomicConfSet(cc) {
return
}
atomic.StorePointer(&atomicConf, (unsafe.Pointer)(cc))
}
// AtomicConf return atomic *ConfVisitor
func AtomicConf() ConfVisitor {
current := (*Conf)(atomic.LoadPointer(&atomicConf))
if current == nil {
defaultOne := newDefaultConf()
if watchDogConf != nil {
watchDogConf(defaultOne)
}
atomic.CompareAndSwapPointer(&atomicConf, nil, (unsafe.Pointer)(defaultOne))
return (*Conf)(atomic.LoadPointer(&atomicConf))
}
return current
}
// all getter func
func (cc *Conf) GetNet() string { return cc.Net }
func (cc *Conf) GetAlwaysRESP2() bool { return cc.AlwaysRESP2 }
func (cc *Conf) GetName() string { return cc.Name }
func (cc *Conf) GetMasterName() string { return cc.MasterName }
func (cc *Conf) GetEnableMonitor() bool { return cc.EnableMonitor }
func (cc *Conf) GetAddrs() []string { return cc.Addrs }
func (cc *Conf) GetDB() int { return cc.DB }
func (cc *Conf) GetUsername() string { return cc.Username }
func (cc *Conf) GetPassword() string { return cc.Password }
func (cc *Conf) GetWriteTimeout() time.Duration { return cc.WriteTimeout }
func (cc *Conf) GetConnPoolSize() int { return cc.ConnPoolSize }
func (cc *Conf) GetEnableCache() bool { return cc.EnableCache }
func (cc *Conf) GetCacheSizeEachConn() int { return cc.CacheSizeEachConn }
func (cc *Conf) GetRingScaleEachConn() int { return cc.RingScaleEachConn }
func (cc *Conf) GetDevelopment() bool { return cc.Development }
func (cc *Conf) GetT() Tester { return cc.T }
func (cc *Conf) GetForceSingleClient() bool { return cc.ForceSingleClient }
// ConfVisitor visitor interface for Conf
type ConfVisitor interface {
GetNet() string
GetAlwaysRESP2() bool
GetName() string
GetMasterName() string
GetEnableMonitor() bool
GetAddrs() []string
GetDB() int
GetUsername() string
GetPassword() string
GetWriteTimeout() time.Duration
GetConnPoolSize() int
GetEnableCache() bool
GetCacheSizeEachConn() int
GetRingScaleEachConn() int
GetDevelopment() bool
GetT() Tester
GetForceSingleClient() bool
}
// ConfInterface visitor + ApplyOption interface for Conf
type ConfInterface interface {
ConfVisitor
ApplyOption(...ConfOption) []ConfOption
}