Skip to content

Commit

Permalink
Optimize note information and some code
Browse files Browse the repository at this point in the history
  • Loading branch information
shengyanli1982 committed Dec 25, 2023
1 parent 64083c1 commit 267302e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 83 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ Gopkg.lock

# cover
coverage.txt

.vscode/
.idea/
.DS_Store
72 changes: 36 additions & 36 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"time"
)

// Function signature of retry if function
// RetryIfFunc defines the function signature for the retry condition.
type RetryIfFunc func(error) bool

// Function signature of OnRetry function
// OnRetryFunc defines the function signature for the on retry callback.
// n = count of attempts
type OnRetryFunc func(n uint, err error)

Expand Down Expand Up @@ -44,58 +44,58 @@ type Option func(*Config)

func emptyOption(c *Config) {}

// return the direct last error that came from the retried function
// default is false (return wrapped errors with everything)
// LastErrorOnly sets whether to return only the direct last error that came from the retried function.
// The default value is false (return wrapped errors with everything).
func LastErrorOnly(lastErrorOnly bool) Option {
return func(c *Config) {
c.lastErrorOnly = lastErrorOnly
}
}

// Attempts set count of retry. Setting to 0 will retry until the retried function succeeds.
// default is 10
// Attempts sets the count of retry attempts. Setting it to 0 will retry until the retried function succeeds.
// The default value is 10.
func Attempts(attempts uint) Option {
return func(c *Config) {
c.attempts = attempts
}
}

// AttemptsForError sets count of retry in case execution results in given `err`
// Retries for the given `err` are also counted against total retries.
// The retry will stop if any of given retries is exhausted.
// AttemptsForError sets the count of retry attempts in case the execution results in the given `err`.
// Retries for the given `err` are also counted against the total retries.
// The retry will stop if any of the given retries is exhausted.
//
// added in 4.3.0
// Added in 4.3.0.
func AttemptsForError(attempts uint, err error) Option {
return func(c *Config) {
c.attemptsForError[err] = attempts
}
}

// Delay set delay between retry
// default is 100ms
// Delay sets the delay between retries.
// The default value is 100ms.
func Delay(delay time.Duration) Option {
return func(c *Config) {
c.delay = delay
}
}

// MaxDelay set maximum delay between retry
// does not apply by default
// MaxDelay sets the maximum delay between retries.
// It does not apply by default.
func MaxDelay(maxDelay time.Duration) Option {
return func(c *Config) {
c.maxDelay = maxDelay
}
}

// MaxJitter sets the maximum random Jitter between retries for RandomDelay
// MaxJitter sets the maximum random jitter between retries for RandomDelay.
func MaxJitter(maxJitter time.Duration) Option {
return func(c *Config) {
c.maxJitter = maxJitter
}
}

// DelayType set type of the delay between retries
// default is BackOff
// DelayType sets the type of delay between retries.
// The default value is BackOff.
func DelayType(delayType DelayTypeFunc) Option {
if delayType == nil {
return emptyOption
Expand All @@ -105,7 +105,7 @@ func DelayType(delayType DelayTypeFunc) Option {
}
}

// BackOffDelay is a DelayType which increases delay between consecutive retries
// BackOffDelay is a DelayType which increases the delay between consecutive retries.
func BackOffDelay(n uint, _ error, config *Config) time.Duration {
// 1 << 63 would overflow signed int64 (time.Duration), thus 62.
const max uint = 62
Expand All @@ -125,17 +125,17 @@ func BackOffDelay(n uint, _ error, config *Config) time.Duration {
return config.delay << n
}

// FixedDelay is a DelayType which keeps delay the same through all iterations
// FixedDelay is a DelayType which keeps the delay the same through all iterations.
func FixedDelay(_ uint, _ error, config *Config) time.Duration {
return config.delay
}

// RandomDelay is a DelayType which picks a random delay up to config.maxJitter
// RandomDelay is a DelayType which picks a random delay up to config.maxJitter.
func RandomDelay(_ uint, _ error, config *Config) time.Duration {
return time.Duration(rand.Int63n(int64(config.maxJitter)))
}

// CombineDelay is a DelayType the combines all of the specified delays into a new DelayTypeFunc
// CombineDelay is a DelayType that combines all of the specified delays into a new DelayTypeFunc.
func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc {
const maxInt64 = uint64(math.MaxInt64)

Expand All @@ -152,9 +152,9 @@ func CombineDelay(delays ...DelayTypeFunc) DelayTypeFunc {
}
}

// OnRetry function callback are called each retry
// OnRetry sets the callback function that is called on each retry.
//
// log each retry example:
// Example of logging each retry:
//
// retry.Do(
// func() error {
Expand All @@ -174,9 +174,9 @@ func OnRetry(onRetry OnRetryFunc) Option {
}

// RetryIf controls whether a retry should be attempted after an error
// (assuming there are any retry attempts remaining)
// (assuming there are any retry attempts remaining).
//
// skip retry if special error example:
// Example of skipping retry for a special error:
//
// retry.Do(
// func() error {
Expand All @@ -190,8 +190,8 @@ func OnRetry(onRetry OnRetryFunc) Option {
// })
// )
//
// By default RetryIf stops execution if the error is wrapped using `retry.Unrecoverable`,
// so above example may also be shortened to:
// By default, RetryIf stops execution if the error is wrapped using `retry.Unrecoverable`.
// So the above example may also be shortened to:
//
// retry.Do(
// func() error {
Expand All @@ -207,10 +207,10 @@ func RetryIf(retryIf RetryIfFunc) Option {
}
}

// Context allow to set context of retry
// default are Background context
// Context allows setting the context of the retry.
// The default context is the Background context.
//
// example of immediately cancellation (maybe it isn't the best example, but it describes behavior enough; I hope)
// Example of immediate cancellation (maybe it isn't the best example, but it describes the behavior enough; I hope):
//
// ctx, cancel := context.WithCancel(context.Background())
// cancel()
Expand All @@ -228,12 +228,12 @@ func Context(ctx context.Context) Option {
}

// WithTimer provides a way to swap out timer module implementations.
// This primarily is useful for mocking/testing, where you may not want to explicitly wait for a set duration
// This is primarily useful for mocking/testing, where you may not want to explicitly wait for a set duration
// for retries.
//
// example of augmenting time.After with a print statement
// Example of augmenting time.After with a print statement:
//
// type struct MyTimer {}
// type MyTimer struct {}
//
// func (t *MyTimer) After(d time.Duration) <- chan time.Time {
// fmt.Print("Timer called!")
Expand All @@ -251,10 +251,10 @@ func WithTimer(t Timer) Option {
}

// WrapContextErrorWithLastError allows the context error to be returned wrapped with the last error that the
// retried function returned. This is only applicable when Attempts is set to 0 to retry indefinitly and when
// using a context to cancel / timeout
// retried function returned. This is only applicable when Attempts is set to 0 to retry indefinitely and when
// using a context to cancel / timeout.
//
// default is false
// The default value is false.
//
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()
Expand Down

0 comments on commit 267302e

Please sign in to comment.