-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg: add a new retry to support the error isRetryable or not (#1848)
- Loading branch information
Showing
6 changed files
with
330 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package retry | ||
|
||
import ( | ||
"math" | ||
) | ||
|
||
const ( | ||
// defaultBackoffBaseInMs is the initial duration, in Millisecond | ||
defaultBackoffBaseInMs = 10.0 | ||
// defaultBackoffCapInMs is the max amount of duration, in Millisecond | ||
defaultBackoffCapInMs = 100.0 | ||
defaultMaxTries = 3 | ||
) | ||
|
||
// Option ... | ||
type Option func(*retryOptions) | ||
|
||
// IsRetryable checks the error is safe or worth to retry, eg. "context.Canceled" better not retry | ||
type IsRetryable func(error) bool | ||
|
||
type retryOptions struct { | ||
maxTries int64 | ||
backoffBaseInMs float64 | ||
backoffCapInMs float64 | ||
isRetryable IsRetryable | ||
} | ||
|
||
func newRetryOptions() *retryOptions { | ||
return &retryOptions{ | ||
maxTries: defaultMaxTries, | ||
backoffBaseInMs: defaultBackoffBaseInMs, | ||
backoffCapInMs: defaultBackoffCapInMs, | ||
isRetryable: func(err error) bool { return true }, | ||
} | ||
} | ||
|
||
// WithBackoffBaseDelay configures the initial delay, if delayInMs <= 0 "defaultBackoffBaseInMs" will be used | ||
func WithBackoffBaseDelay(delayInMs int64) Option { | ||
return func(o *retryOptions) { | ||
if delayInMs > 0 { | ||
o.backoffBaseInMs = float64(delayInMs) | ||
} | ||
} | ||
} | ||
|
||
// WithBackoffMaxDelay configures the maximum delay, if delayInMs <= 0 "defaultBackoffCapInMs" will be used | ||
func WithBackoffMaxDelay(delayInMs int64) Option { | ||
return func(o *retryOptions) { | ||
if delayInMs > 0 { | ||
o.backoffCapInMs = float64(delayInMs) | ||
} | ||
} | ||
} | ||
|
||
// WithMaxTries configures maximum tries, if tries <= 0 "defaultMaxTries" will be used | ||
func WithMaxTries(tries int64) Option { | ||
return func(o *retryOptions) { | ||
if tries > 0 { | ||
o.maxTries = tries | ||
} | ||
} | ||
} | ||
|
||
// WithInfiniteTries configures to retry forever (math.MaxInt64 times) till success or got canceled | ||
func WithInfiniteTries() Option { | ||
return func(o *retryOptions) { | ||
o.maxTries = math.MaxInt64 | ||
} | ||
} | ||
|
||
// WithIsRetryableErr configures the error should retry or not, if not set, retry by default | ||
func WithIsRetryableErr(f IsRetryable) Option { | ||
return func(o *retryOptions) { | ||
if f != nil { | ||
o.isRetryable = f | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package retry | ||
|
||
import ( | ||
"context" | ||
"math" | ||
"math/rand" | ||
"time" | ||
|
||
"github.com/pingcap/errors" | ||
cerror "github.com/pingcap/ticdc/pkg/errors" | ||
) | ||
|
||
// Operation is the action need to retry | ||
type Operation func() error | ||
|
||
// Do execute the specified function at most maxTries times until it succeeds or got canceled | ||
func Do(ctx context.Context, operation Operation, opts ...Option) error { | ||
retryOption := setOptions(opts...) | ||
return run(ctx, operation, retryOption) | ||
} | ||
|
||
func setOptions(opts ...Option) *retryOptions { | ||
retryOption := newRetryOptions() | ||
for _, opt := range opts { | ||
opt(retryOption) | ||
} | ||
return retryOption | ||
} | ||
|
||
func run(ctx context.Context, op Operation, retryOption *retryOptions) error { | ||
select { | ||
case <-ctx.Done(): | ||
return errors.Trace(ctx.Err()) | ||
default: | ||
} | ||
|
||
var t *time.Timer | ||
try := 0 | ||
backOff := time.Duration(0) | ||
for { | ||
err := op() | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
if !retryOption.isRetryable(err) { | ||
return err | ||
} | ||
|
||
try++ | ||
if int64(try) >= retryOption.maxTries { | ||
return cerror.ErrReachMaxTry.Wrap(err).GenWithStackByArgs(retryOption.maxTries) | ||
} | ||
|
||
backOff = getBackoffInMs(retryOption.backoffBaseInMs, retryOption.backoffCapInMs, float64(try)) | ||
if t == nil { | ||
t = time.NewTimer(backOff) | ||
defer t.Stop() | ||
} else { | ||
t.Reset(backOff) | ||
} | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return errors.Trace(ctx.Err()) | ||
case <-t.C: | ||
} | ||
} | ||
} | ||
|
||
// getBackoffInMs returns the duration to wait before next try | ||
// See https://www.awsarchitectureblog.com/2015/03/backoff.html | ||
func getBackoffInMs(backoffBaseInMs, backoffCapInMs, try float64) time.Duration { | ||
temp := int64(math.Min(backoffCapInMs, backoffBaseInMs*math.Exp2(try)) / 2) | ||
if temp <= 0 { | ||
temp = 1 | ||
} | ||
sleep := temp + rand.Int63n(temp) | ||
backOff := math.Min(backoffCapInMs, float64(rand.Int63n(sleep*3))+backoffBaseInMs) | ||
return time.Duration(backOff) * time.Millisecond | ||
} |