Proposal for v5 API: retry.New(...).Do(func) #148
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
API redesign: method-based retry operations
Migration is simple:
retry.Do(func, opts...)→retry.New(opts...).Do(func)(simple find & replace)This change improves performance (config reuse reduces memory allocations), simplifies the API, and provides a cleaner interface
Renamed
Configtype toRetrierRenamed
NewConfig()toNew()Changed from package-level functions to methods:
retry.Do(func, config)→retry.New(opts...).Do(func)DelayTypeFuncsignature changed:func(n uint, err error, config *Config)→func(n uint, err error, r *Retrier)Added benchmarks
minor stability fix on handling "Delay(0)" or negative delay cases
The Config is pre-computed and reusable, allowing for zero allocations on happy paths and but also lower allocation on retries.
Performance improvements (baseline: v4 retry.Do(): 129ns, 4 allocs):
BenchmarkDo_ImmediateSuccess-24 69677844 18.08 ns/op 0 B/op 0 allocs/op
BenchmarkDoWithData_ImmediateSuccess-24 73470648 16.41 ns/op 0 B/op 0 allocs/op
BenchmarkDo_OneRetry-24 49 49192156 ns/op 300 B/op 6 allocs/op