github.com/sandwich-go/boost@v1.3.29/retry/gen_options_optiongen.go (about) 1 // Code generated by optiongen. DO NOT EDIT. 2 // optiongen: github.com/timestee/optiongen 3 4 package retry 5 6 import ( 7 "context" 8 "time" 9 ) 10 11 // Options should use NewOptions to initialize it 12 type Options struct { 13 // annotation@Limit(comment="最大尝试次数") 14 Limit uint 15 // annotation@Delay(comment="固定延迟") 16 Delay time.Duration 17 // annotation@MaxJitter(comment="延迟最大抖动") 18 MaxJitter time.Duration 19 // annotation@OnRetry(comment="每次重试会先调用此方法") 20 OnRetry func(n uint, err error) /*do nothing now*/ 21 // annotation@RetryIf(comment="何种error进行重试") 22 RetryIf func(err error) bool 23 // annotation@DelayType(comment="何种error进行重试") 24 DelayType DelayTypeFunc 25 // annotation@LastErrorOnly(comment="是否只返回最后遇到的error") 26 LastErrorOnly bool 27 // annotation@Context(comment="context,可以设定超时等") 28 Context context.Context 29 // annotation@MaxDelay(comment="最大延迟时间") 30 MaxDelay time.Duration 31 MaxBackOffNInner uint 32 } 33 34 // NewOptions new Options 35 func NewOptions(opts ...Option) *Options { 36 cc := newDefaultOptions() 37 for _, opt := range opts { 38 opt(cc) 39 } 40 if watchDogOptions != nil { 41 watchDogOptions(cc) 42 } 43 return cc 44 } 45 46 // ApplyOption apply multiple new option 47 func (cc *Options) ApplyOption(opts ...Option) { 48 for _, opt := range opts { 49 opt(cc) 50 } 51 } 52 53 // Option option func 54 type Option func(cc *Options) 55 56 // WithLimit 最大尝试次数 57 func WithLimit(v uint) Option { 58 return func(cc *Options) { 59 cc.Limit = v 60 } 61 } 62 63 // WithDelay 固定延迟 64 func WithDelay(v time.Duration) Option { 65 return func(cc *Options) { 66 cc.Delay = v 67 } 68 } 69 70 // WithMaxJitter 延迟最大抖动 71 func WithMaxJitter(v time.Duration) Option { 72 return func(cc *Options) { 73 cc.MaxJitter = v 74 } 75 } 76 77 // WithOnRetry 每次重试会先调用此方法 78 func WithOnRetry(v func(n uint, err error)) Option { 79 return func(cc *Options) { 80 cc.OnRetry = v 81 } 82 } 83 84 // WithRetryIf 何种error进行重试 85 func WithRetryIf(v func(err error) bool) Option { 86 return func(cc *Options) { 87 cc.RetryIf = v 88 } 89 } 90 91 // WithDelayType 何种error进行重试 92 func WithDelayType(v DelayTypeFunc) Option { 93 return func(cc *Options) { 94 cc.DelayType = v 95 } 96 } 97 98 // WithLastErrorOnly 是否只返回最后遇到的error 99 func WithLastErrorOnly(v bool) Option { 100 return func(cc *Options) { 101 cc.LastErrorOnly = v 102 } 103 } 104 105 // WithContext context,可以设定超时等 106 func WithContext(v context.Context) Option { 107 return func(cc *Options) { 108 cc.Context = v 109 } 110 } 111 112 // WithMaxDelay 最大延迟时间 113 func WithMaxDelay(v time.Duration) Option { 114 return func(cc *Options) { 115 cc.MaxDelay = v 116 } 117 } 118 119 // InstallOptionsWatchDog the installed func will called when NewOptions called 120 func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog } 121 122 // watchDogOptions global watch dog 123 var watchDogOptions func(cc *Options) 124 125 // newDefaultOptions new default Options 126 func newDefaultOptions() *Options { 127 cc := &Options{ 128 MaxBackOffNInner: 0, 129 } 130 131 for _, opt := range [...]Option{ 132 WithLimit(10), 133 WithDelay(100 * time.Millisecond), 134 WithMaxJitter(100 * time.Millisecond), 135 WithOnRetry(func(n uint, err error) { 136 }), 137 WithRetryIf(IsRecoverable), 138 WithDelayType(CombineDelay(BackOffDelay, RandomDelay)), 139 WithLastErrorOnly(false), 140 WithContext(context.Background()), 141 WithMaxDelay(0), 142 } { 143 opt(cc) 144 } 145 146 return cc 147 } 148 149 // all getter func 150 func (cc *Options) GetLimit() uint { return cc.Limit } 151 func (cc *Options) GetDelay() time.Duration { return cc.Delay } 152 func (cc *Options) GetMaxJitter() time.Duration { return cc.MaxJitter } 153 func (cc *Options) GetOnRetry() func(n uint, err error) { return cc.OnRetry } 154 func (cc *Options) GetRetryIf() func(err error) bool { return cc.RetryIf } 155 func (cc *Options) GetDelayType() DelayTypeFunc { return cc.DelayType } 156 func (cc *Options) GetLastErrorOnly() bool { return cc.LastErrorOnly } 157 func (cc *Options) GetContext() context.Context { return cc.Context } 158 func (cc *Options) GetMaxDelay() time.Duration { return cc.MaxDelay } 159 func (cc *Options) GetMaxBackOffNInner() uint { return cc.MaxBackOffNInner } 160 161 // OptionsVisitor visitor interface for Options 162 type OptionsVisitor interface { 163 GetLimit() uint 164 GetDelay() time.Duration 165 GetMaxJitter() time.Duration 166 GetOnRetry() func(n uint, err error) 167 GetRetryIf() func(err error) bool 168 GetDelayType() DelayTypeFunc 169 GetLastErrorOnly() bool 170 GetContext() context.Context 171 GetMaxDelay() time.Duration 172 GetMaxBackOffNInner() uint 173 } 174 175 // OptionsInterface visitor + ApplyOption interface for Options 176 type OptionsInterface interface { 177 OptionsVisitor 178 ApplyOption(...Option) 179 }