github.com/sandwich-go/boost@v1.3.29/xpanic/gen_autorecoveroptions_optiongen.go (about) 1 // Code generated by optiongen. DO NOT EDIT. 2 // optiongen: github.com/timestee/optiongen 3 4 package xpanic 5 6 import ( 7 "fmt" 8 "time" 9 10 "github.com/sandwich-go/boost/internal/log" 11 ) 12 13 // AutoRecoverOptions should use NewAutoRecoverOptions to initialize it 14 type AutoRecoverOptions struct { 15 // annotation@DelayTime(comment="每次panic后重启delay的时间 Note: 这里应该可以直接对接到retry package,复用重试逻辑") 16 DelayTime time.Duration 17 // annotation@OnRecover(comment="如果指定了该函数,recover panic的时候,会执行该函数,默认输出error日志") 18 OnRecover OnRecover 19 } 20 21 // NewAutoRecoverOptions new AutoRecoverOptions 22 func NewAutoRecoverOptions(opts ...AutoRecoverOption) *AutoRecoverOptions { 23 cc := newDefaultAutoRecoverOptions() 24 for _, opt := range opts { 25 opt(cc) 26 } 27 if watchDogAutoRecoverOptions != nil { 28 watchDogAutoRecoverOptions(cc) 29 } 30 return cc 31 } 32 33 // ApplyOption apply multiple new option 34 func (cc *AutoRecoverOptions) ApplyOption(opts ...AutoRecoverOption) { 35 for _, opt := range opts { 36 opt(cc) 37 } 38 } 39 40 // AutoRecoverOption option func 41 type AutoRecoverOption func(cc *AutoRecoverOptions) 42 43 // WithAutoRecoverOptionDelayTime 每次panic后重启delay的时间 Note: 这里应该可以直接对接到retry package,复用重试逻辑 44 func WithAutoRecoverOptionDelayTime(v time.Duration) AutoRecoverOption { 45 return func(cc *AutoRecoverOptions) { 46 cc.DelayTime = v 47 } 48 } 49 50 // WithAutoRecoverOptionOnRecover 如果指定了该函数,recover panic的时候,会执行该函数,默认输出error日志 51 func WithAutoRecoverOptionOnRecover(v OnRecover) AutoRecoverOption { 52 return func(cc *AutoRecoverOptions) { 53 cc.OnRecover = v 54 } 55 } 56 57 // InstallAutoRecoverOptionsWatchDog the installed func will called when NewAutoRecoverOptions called 58 func InstallAutoRecoverOptionsWatchDog(dog func(cc *AutoRecoverOptions)) { 59 watchDogAutoRecoverOptions = dog 60 } 61 62 // watchDogAutoRecoverOptions global watch dog 63 var watchDogAutoRecoverOptions func(cc *AutoRecoverOptions) 64 65 // newDefaultAutoRecoverOptions new default AutoRecoverOptions 66 func newDefaultAutoRecoverOptions() *AutoRecoverOptions { 67 cc := &AutoRecoverOptions{} 68 69 for _, opt := range [...]AutoRecoverOption{ 70 WithAutoRecoverOptionDelayTime(0), 71 WithAutoRecoverOptionOnRecover(func(tag string, reason interface{}) { 72 log.Error(fmt.Sprintf("%s panic with err, reason: %v", tag, reason)) 73 }), 74 } { 75 opt(cc) 76 } 77 78 return cc 79 } 80 81 // all getter func 82 func (cc *AutoRecoverOptions) GetDelayTime() time.Duration { return cc.DelayTime } 83 func (cc *AutoRecoverOptions) GetOnRecover() OnRecover { return cc.OnRecover } 84 85 // AutoRecoverOptionsVisitor visitor interface for AutoRecoverOptions 86 type AutoRecoverOptionsVisitor interface { 87 GetDelayTime() time.Duration 88 GetOnRecover() OnRecover 89 } 90 91 // AutoRecoverOptionsInterface visitor + ApplyOption interface for AutoRecoverOptions 92 type AutoRecoverOptionsInterface interface { 93 AutoRecoverOptionsVisitor 94 ApplyOption(...AutoRecoverOption) 95 }