-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_options_optiongen.go
150 lines (128 loc) · 3.9 KB
/
gen_options_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
// Code generated by optiongen. DO NOT EDIT.
// optiongen: github.com/timestee/optiongen
package distributedlocker
import "time"
// Options should use NewOptions to initialize it
type Options struct {
RetryTimes int `usage:"acquire lock重试次数"`
MinRetryInterval time.Duration `usage:"最小重试时间,不能小于50ms"`
MaxRetryInterval time.Duration `usage:"最大重试时间,不能大于250ms"`
DriftFactor float64 `usage:"有效时间因子"`
TimeoutFactor float64 `usage:"超时时间因子"`
Expiration time.Duration `usage:"过期时间"`
Prefix string `usage:"key的前缀"`
AutoRenew bool `usage:"是否自动续期"`
}
// NewOptions new Options
func NewOptions(opts ...Option) *Options {
cc := newDefaultOptions()
for _, opt := range opts {
opt(cc)
}
if watchDogOptions != nil {
watchDogOptions(cc)
}
return cc
}
// ApplyOption apply multiple new option
func (cc *Options) ApplyOption(opts ...Option) {
for _, opt := range opts {
opt(cc)
}
}
// Option option func
type Option func(cc *Options)
// WithRetryTimes acquire lock重试次数
func WithRetryTimes(v int) Option {
return func(cc *Options) {
cc.RetryTimes = v
}
}
// WithMinRetryInterval 最小重试时间,不能小于50ms
func WithMinRetryInterval(v time.Duration) Option {
return func(cc *Options) {
cc.MinRetryInterval = v
}
}
// WithMaxRetryInterval 最大重试时间,不能大于250ms
func WithMaxRetryInterval(v time.Duration) Option {
return func(cc *Options) {
cc.MaxRetryInterval = v
}
}
// WithDriftFactor 有效时间因子
func WithDriftFactor(v float64) Option {
return func(cc *Options) {
cc.DriftFactor = v
}
}
// WithTimeoutFactor 超时时间因子
func WithTimeoutFactor(v float64) Option {
return func(cc *Options) {
cc.TimeoutFactor = v
}
}
// WithExpiration 过期时间
func WithExpiration(v time.Duration) Option {
return func(cc *Options) {
cc.Expiration = v
}
}
// WithPrefix key的前缀
func WithPrefix(v string) Option {
return func(cc *Options) {
cc.Prefix = v
}
}
// WithAutoRenew 是否自动续期
func WithAutoRenew(v bool) Option {
return func(cc *Options) {
cc.AutoRenew = v
}
}
// InstallOptionsWatchDog the installed func will called when NewOptions called
func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog }
// watchDogOptions global watch dog
var watchDogOptions func(cc *Options)
// newDefaultOptions new default Options
func newDefaultOptions() *Options {
cc := &Options{}
for _, opt := range [...]Option{
WithRetryTimes(32),
WithMinRetryInterval(minRetryInterval),
WithMaxRetryInterval(maxRetryInterval),
WithDriftFactor(0.01),
WithTimeoutFactor(0.05),
WithExpiration(minExpiration),
WithPrefix("__mx__"),
WithAutoRenew(true),
} {
opt(cc)
}
return cc
}
// all getter func
func (cc *Options) GetRetryTimes() int { return cc.RetryTimes }
func (cc *Options) GetMinRetryInterval() time.Duration { return cc.MinRetryInterval }
func (cc *Options) GetMaxRetryInterval() time.Duration { return cc.MaxRetryInterval }
func (cc *Options) GetDriftFactor() float64 { return cc.DriftFactor }
func (cc *Options) GetTimeoutFactor() float64 { return cc.TimeoutFactor }
func (cc *Options) GetExpiration() time.Duration { return cc.Expiration }
func (cc *Options) GetPrefix() string { return cc.Prefix }
func (cc *Options) GetAutoRenew() bool { return cc.AutoRenew }
// OptionsVisitor visitor interface for Options
type OptionsVisitor interface {
GetRetryTimes() int
GetMinRetryInterval() time.Duration
GetMaxRetryInterval() time.Duration
GetDriftFactor() float64
GetTimeoutFactor() float64
GetExpiration() time.Duration
GetPrefix() string
GetAutoRenew() bool
}
// OptionsInterface visitor + ApplyOption interface for Options
type OptionsInterface interface {
OptionsVisitor
ApplyOption(...Option)
}