-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_lockeroptions_optiongen.go
154 lines (135 loc) · 5.4 KB
/
gen_lockeroptions_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
// Code generated by optiongen. DO NOT EDIT.
// optiongen: github.com/timestee/optiongen
package redisson
import "time"
// LockerOptions should use newLockerOptions to initialize it
type LockerOptions struct {
// annotation@KeyPrefix(KeyPrefix is the prefix of redis key for locks. Default value is defaultKeyPrefix)
KeyPrefix string
// annotation@KeyValidity(KeyValidity is the validity duration of locks and will be extended periodically by the ExtendInterval. Default value is defaultKeyValidity)
KeyValidity time.Duration
// annotation@TryNextAfter(TryNextAfter is the timeout duration before trying the next redis key for locks. Default value is defaultTryNextAfter)
TryNextAfter time.Duration
// annotation@KeyMajority(KeyMajority is at least how many redis keys in a total of KeyMajority*2-1 should be acquired to be a valid lock. Default value is defaultKeyMajority)
KeyMajority int32
// annotation@NoLoopTracking(NoLoopTracking will use NOLOOP in the CLIENT TRACKING command to avoid unnecessary notifications and thus have better performance. This can only be enabled if all your redis nodes >= 7.0.5)
NoLoopTracking bool
// annotation@FallbackSETPX(Use SET PX instead of SET PXAT when acquiring locks to be compatible with Redis < 6.2)
FallbackSETPX bool
}
// newLockerOptions new LockerOptions
func newLockerOptions(opts ...LockerOption) *LockerOptions {
cc := newDefaultLockerOptions()
for _, opt := range opts {
opt(cc)
}
if watchDogLockerOptions != nil {
watchDogLockerOptions(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 *LockerOptions) ApplyOption(opts ...LockerOption) []LockerOption {
var previous []LockerOption
for _, opt := range opts {
previous = append(previous, opt(cc))
}
return previous
}
// LockerOption option func
type LockerOption func(cc *LockerOptions) LockerOption
// WithLockerOptionKeyPrefix option func for filed KeyPrefix
func WithLockerOptionKeyPrefix(v string) LockerOption {
return func(cc *LockerOptions) LockerOption {
previous := cc.KeyPrefix
cc.KeyPrefix = v
return WithLockerOptionKeyPrefix(previous)
}
}
// WithLockerOptionKeyValidity option func for filed KeyValidity
func WithLockerOptionKeyValidity(v time.Duration) LockerOption {
return func(cc *LockerOptions) LockerOption {
previous := cc.KeyValidity
cc.KeyValidity = v
return WithLockerOptionKeyValidity(previous)
}
}
// WithLockerOptionTryNextAfter option func for filed TryNextAfter
func WithLockerOptionTryNextAfter(v time.Duration) LockerOption {
return func(cc *LockerOptions) LockerOption {
previous := cc.TryNextAfter
cc.TryNextAfter = v
return WithLockerOptionTryNextAfter(previous)
}
}
// WithLockerOptionKeyMajority option func for filed KeyMajority
func WithLockerOptionKeyMajority(v int32) LockerOption {
return func(cc *LockerOptions) LockerOption {
previous := cc.KeyMajority
cc.KeyMajority = v
return WithLockerOptionKeyMajority(previous)
}
}
// WithLockerOptionNoLoopTracking option func for filed NoLoopTracking
func WithLockerOptionNoLoopTracking(v bool) LockerOption {
return func(cc *LockerOptions) LockerOption {
previous := cc.NoLoopTracking
cc.NoLoopTracking = v
return WithLockerOptionNoLoopTracking(previous)
}
}
// WithLockerOptionFallbackSETPX option func for filed FallbackSETPX
func WithLockerOptionFallbackSETPX(v bool) LockerOption {
return func(cc *LockerOptions) LockerOption {
previous := cc.FallbackSETPX
cc.FallbackSETPX = v
return WithLockerOptionFallbackSETPX(previous)
}
}
// InstallLockerOptionsWatchDog the installed func will called when newLockerOptions called
func InstallLockerOptionsWatchDog(dog func(cc *LockerOptions)) { watchDogLockerOptions = dog }
// watchDogLockerOptions global watch dog
var watchDogLockerOptions func(cc *LockerOptions)
// setLockerOptionsDefaultValue default LockerOptions value
func setLockerOptionsDefaultValue(cc *LockerOptions) {
for _, opt := range [...]LockerOption{
WithLockerOptionKeyPrefix(defaultKeyPrefix),
WithLockerOptionKeyValidity(defaultKeyValidity),
WithLockerOptionTryNextAfter(defaultTryNextAfter),
WithLockerOptionKeyMajority(defaultKeyMajority),
WithLockerOptionNoLoopTracking(false),
WithLockerOptionFallbackSETPX(false),
} {
opt(cc)
}
}
// newDefaultLockerOptions new default LockerOptions
func newDefaultLockerOptions() *LockerOptions {
cc := &LockerOptions{}
setLockerOptionsDefaultValue(cc)
return cc
}
// all getter func
func (cc *LockerOptions) GetKeyPrefix() string { return cc.KeyPrefix }
func (cc *LockerOptions) GetKeyValidity() time.Duration { return cc.KeyValidity }
func (cc *LockerOptions) GetTryNextAfter() time.Duration { return cc.TryNextAfter }
func (cc *LockerOptions) GetKeyMajority() int32 { return cc.KeyMajority }
func (cc *LockerOptions) GetNoLoopTracking() bool { return cc.NoLoopTracking }
func (cc *LockerOptions) GetFallbackSETPX() bool { return cc.FallbackSETPX }
// LockerOptionsVisitor visitor interface for LockerOptions
type LockerOptionsVisitor interface {
GetKeyPrefix() string
GetKeyValidity() time.Duration
GetTryNextAfter() time.Duration
GetKeyMajority() int32
GetNoLoopTracking() bool
GetFallbackSETPX() bool
}
// LockerOptionsInterface visitor + ApplyOption interface for LockerOptions
type LockerOptionsInterface interface {
LockerOptionsVisitor
ApplyOption(...LockerOption) []LockerOption
}