-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_delayoptions_optiongen.go
131 lines (114 loc) · 4.09 KB
/
gen_delayoptions_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
// Code generated by optiongen. DO NOT EDIT.
// optiongen: github.com/timestee/optiongen
package redisson
import (
"fmt"
"time"
)
// DelayOptions should use newDelayOptions to initialize it
type DelayOptions struct {
// annotation@Prefix(延迟队列前缀)
Prefix string
// annotation@Timeout(业务处理超时时间,如果超过该时间未处理,则重试)
Timeout time.Duration
// annotation@RetryTimes(comment="重试次数,当业务处理超时,或业务处理返回错误,则重试")
RetryTimes int `usage:"重试次数,当业务处理超时,或业务处理返回错误,则重试"`
// annotation@HandleDeadLetter(comment="处理死信,当达到最大重试次数,则为死信")
HandleDeadLetter func(bs []byte) `usage:"处理死信,当达到最大重试次数,则为死信"`
}
// newDelayOptions new DelayOptions
func newDelayOptions(opts ...DelayOption) *DelayOptions {
cc := newDefaultDelayOptions()
for _, opt := range opts {
opt(cc)
}
if watchDogDelayOptions != nil {
watchDogDelayOptions(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 *DelayOptions) ApplyOption(opts ...DelayOption) []DelayOption {
var previous []DelayOption
for _, opt := range opts {
previous = append(previous, opt(cc))
}
return previous
}
// DelayOption option func
type DelayOption func(cc *DelayOptions) DelayOption
// WithDelayOptionPrefix option func for filed Prefix
func WithDelayOptionPrefix(v string) DelayOption {
return func(cc *DelayOptions) DelayOption {
previous := cc.Prefix
cc.Prefix = v
return WithDelayOptionPrefix(previous)
}
}
// WithDelayOptionTimeout option func for filed Timeout
func WithDelayOptionTimeout(v time.Duration) DelayOption {
return func(cc *DelayOptions) DelayOption {
previous := cc.Timeout
cc.Timeout = v
return WithDelayOptionTimeout(previous)
}
}
// WithDelayOptionRetryTimes 重试次数,当业务处理超时,或业务处理返回错误,则重试
func WithDelayOptionRetryTimes(v int) DelayOption {
return func(cc *DelayOptions) DelayOption {
previous := cc.RetryTimes
cc.RetryTimes = v
return WithDelayOptionRetryTimes(previous)
}
}
// WithDelayOptionHandleDeadLetter 处理死信,当达到最大重试次数,则为死信
func WithDelayOptionHandleDeadLetter(v func(bs []byte)) DelayOption {
return func(cc *DelayOptions) DelayOption {
previous := cc.HandleDeadLetter
cc.HandleDeadLetter = v
return WithDelayOptionHandleDeadLetter(previous)
}
}
// InstallDelayOptionsWatchDog the installed func will called when newDelayOptions called
func InstallDelayOptionsWatchDog(dog func(cc *DelayOptions)) { watchDogDelayOptions = dog }
// watchDogDelayOptions global watch dog
var watchDogDelayOptions func(cc *DelayOptions)
// setDelayOptionsDefaultValue default DelayOptions value
func setDelayOptionsDefaultValue(cc *DelayOptions) {
for _, opt := range [...]DelayOption{
WithDelayOptionPrefix(""),
WithDelayOptionTimeout(1 * time.Minute),
WithDelayOptionRetryTimes(3),
WithDelayOptionHandleDeadLetter(func(bs []byte) {
warning(fmt.Sprintf("got dead letter, %v", bs))
}),
} {
opt(cc)
}
}
// newDefaultDelayOptions new default DelayOptions
func newDefaultDelayOptions() *DelayOptions {
cc := &DelayOptions{}
setDelayOptionsDefaultValue(cc)
return cc
}
// all getter func
func (cc *DelayOptions) GetPrefix() string { return cc.Prefix }
func (cc *DelayOptions) GetTimeout() time.Duration { return cc.Timeout }
func (cc *DelayOptions) GetRetryTimes() int { return cc.RetryTimes }
func (cc *DelayOptions) GetHandleDeadLetter() func(bs []byte) { return cc.HandleDeadLetter }
// DelayOptionsVisitor visitor interface for DelayOptions
type DelayOptionsVisitor interface {
GetPrefix() string
GetTimeout() time.Duration
GetRetryTimes() int
GetHandleDeadLetter() func(bs []byte)
}
// DelayOptionsInterface visitor + ApplyOption interface for DelayOptions
type DelayOptionsInterface interface {
DelayOptionsVisitor
ApplyOption(...DelayOption) []DelayOption
}