Skip to content

Commit

Permalink
feat: add renew_retry_delay parameter, remove retry func from options
Browse files Browse the repository at this point in the history
  • Loading branch information
祝黄清 committed Nov 22, 2022
1 parent 1c12497 commit 442e0c6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 97 deletions.
41 changes: 13 additions & 28 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/sandwich-go/boost/retry"
"github.com/sandwich-go/boost/xsync"
"github.com/sandwich-go/boost/z"
"github.com/sandwich-go/logbus/glog"
Expand Down Expand Up @@ -235,23 +236,6 @@ func nextQuantum(lastQuantum uint64, segmentTime z.MonoTimeDuration, segmentDura
return nq
}

func retry(fn func(attempt int) (retry bool, err error)) error {
var err error
var shouldContinue bool
attempt := 1
for {
shouldContinue, err = fn(attempt)
if err == nil {
break
}
attempt++
if !shouldContinue {
return fmt.Errorf("exceeded retry limit - %v", err)
}
}
return err
}

func (e *engine) preRenew() (quantum uint64, begin z.MonoTimeDuration) {
begin = z.MonoOffset()
quantum = nextQuantum(e.quantum, e.ts,
Expand All @@ -269,27 +253,28 @@ func (e *engine) postRenew(quantum uint64, begin z.MonoTimeDuration, err error)
func (e *engine) renewWithUnlock() {
defer e.renewMutex.Unlock()
quantum, begin := e.preRenew()
e.postRenew(quantum, begin, retry(func(attempt int) (bool, error) {
e.postRenew(quantum, begin, retry.Do(func(attempt uint) error {
defer func() {
if r := recover(); r != nil {
glog.Error(w("renew panic"), glog.Int("attempt", attempt), glog.String("domain", e.domain),
glog.Error(w("renew panic"), glog.Uint("attempt", attempt), glog.String("domain", e.domain),
glog.Any("recover", r))
}
}()
ctx, cancel := context.WithTimeout(context.Background(), e.builder.visitor.GetRenewTimeout())
defer cancel()
c, err := e.builder.driver.Renew(ctx, e.domain, quantum, e.builder.visitor.GetOffsetWhenAutoCreateDomain())
if err != nil {
if delay := e.builder.visitor.GetRenewRetryDelay()(attempt); delay != 0 {
time.Sleep(delay)
}
} else {
e.nextN = c
e.nextMax = c + quantum
e.nextQuantum = quantum
return err
}
return attempt < e.builder.visitor.GetRenewRetry(), err
}))
e.nextN = c
e.nextMax = c + quantum
e.nextQuantum = quantum
return nil
},
retry.WithLimit(e.builder.visitor.GetRenewRetry()),
retry.WithDelayType(func(n uint, _ error, _ *retry.Options) time.Duration {
return time.Duration(n) * e.builder.visitor.GetRenewRetryDelay()
})))
}

func (e *engine) nextOne() (uint64, error) {
Expand Down
41 changes: 15 additions & 26 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,23 @@ import (
"time"
)

type (
// RenewRetryDelayFunc renew失败重试函数
RenewRetryDelayFunc func(attempt int) time.Duration
)

var (
defaultRenewRetryDelayFunc = func(attempt int) time.Duration {
return time.Millisecond * time.Duration(10*attempt)
}
)

//go:generate optiongen --option_with_struct_name=false --new_func=NewConfig --xconf=true --empty_composite_nil=true --usage_tag_name=usage
func OptionsOptionDeclareWithDefault() interface{} {
return map[string]interface{}{
"Limitation": uint64(math.MaxUint64), // @MethodComment(id最大限制,超过该值则会报ErrReachIdLimitation错误)
"OffsetWhenAutoCreateDomain": uint64(30000000), // @MethodComment(当新建新的domain时,偏移多少开始自增,即预留值)
"RenewPercent": 20, // @MethodComment(renew百分比,当id达到百分比值时,会去server端或db拿新的id段)
"RenewRetryDelay": RenewRetryDelayFunc(defaultRenewRetryDelayFunc), // @MethodComment(renew失败重试函数,默认10ms重试一次)
"RenewTimeout": time.Duration(5 * time.Second), // @MethodComment(renew超时)
"RenewRetry": 99, // @MethodComment(renew重试次数)
"SegmentDuration": time.Duration(900 * time.Second), // @MethodComment(设定segment长度,renew号段尺寸调节的目的是使号段消耗稳定趋于SegmentDuration内。降低SegmentDuration,可以更迅速使缓存的号段达到设定的最大数值以提高吞吐能力)
"MinQuantum": uint64(30), // @MethodComment(根据renew请求频率自动伸缩的请求id缓存段,最小段长)
"MaxQuantum": uint64(3000), // @MethodComment(最大段长)
"InitialQuantum": uint64(30), // @MethodComment(初始化段长)
"EnableSlow": true, // @MethodComment(是否开启慢日志)
"SlowQuery": time.Duration(30 * time.Millisecond), // @MethodComment(慢日志最小时长,大于该时长将输出日志)
"EnableTimeSummary": false, // @MethodComment(是否开启Next/MustNext接口的time监控,否则为统计监控)
"Development": true, // @MethodComment(是否为开发模式)
"EnableMonitor": true, // @MethodComment(是否开启监控)
"Limitation": uint64(math.MaxUint64), // @MethodComment(id最大限制,超过该值则会报ErrReachIdLimitation错误)
"OffsetWhenAutoCreateDomain": uint64(30000000), // @MethodComment(当新建新的domain时,偏移多少开始自增,即预留值)
"RenewPercent": 20, // @MethodComment(renew百分比,当id达到百分比值时,会去server端或db拿新的id段)
"RenewTimeout": time.Duration(5 * time.Second), // @MethodComment(renew超时)
"RenewRetry": uint(99), // @MethodComment(renew重试次数)
"RenewRetryDelay": time.Duration(10 * time.Millisecond), // @MethodComment(renew重试延迟)
"SegmentDuration": time.Duration(900 * time.Second), // @MethodComment(设定segment长度,renew号段尺寸调节的目的是使号段消耗稳定趋于SegmentDuration内。降低SegmentDuration,可以更迅速使缓存的号段达到设定的最大数值以提高吞吐能力)
"MinQuantum": uint64(30), // @MethodComment(根据renew请求频率自动伸缩的请求id缓存段,最小段长)
"MaxQuantum": uint64(3000), // @MethodComment(最大段长)
"InitialQuantum": uint64(30), // @MethodComment(初始化段长)
"EnableSlow": true, // @MethodComment(是否开启慢日志)
"SlowQuery": time.Duration(30 * time.Millisecond), // @MethodComment(慢日志最小时长,大于该时长将输出日志)
"EnableTimeSummary": false, // @MethodComment(是否开启Next/MustNext接口的time监控,否则为统计监控)
"Development": true, // @MethodComment(是否为开发模式)
"EnableMonitor": true, // @MethodComment(是否开启监控)
}
}
86 changes: 43 additions & 43 deletions gen_options_optiongen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 442e0c6

Please sign in to comment.