├── .github ├── CONTRIBUTING.md └── workflows │ └── test.yml ├── LICENSE ├── README.md ├── backoff.go ├── backoff_constant.go ├── backoff_constant_test.go ├── backoff_exponential.go ├── backoff_exponential_test.go ├── backoff_fibonacci.go ├── backoff_fibonacci_test.go ├── backoff_test.go ├── go.mod ├── rand.go ├── retry.go └── retry_test.go /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | We'd love to accept your patches and contributions to this project. There are 4 | just a few small guidelines you need to follow. 5 | 6 | 7 | ## Code reviews 8 | 9 | All submissions, including submissions by project members, require review. We 10 | use GitHub pull requests for this purpose. Consult 11 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more 12 | information on using pull requests. 13 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: 'Test' 2 | 3 | on: 4 | push: 5 | branches: 6 | - 'main' 7 | tags: 8 | - '*' 9 | pull_request: 10 | branches: 11 | - 'main' 12 | workflow_dispatch: 13 | 14 | concurrency: 15 | group: '${{ github.workflow }}-${{ github.head_ref || github.ref }}' 16 | cancel-in-progress: true 17 | 18 | jobs: 19 | test: 20 | runs-on: 'ubuntu-latest' 21 | 22 | steps: 23 | - uses: 'actions/checkout@v4' 24 | 25 | - uses: actions/setup-go@v5 26 | with: 27 | go-version-file: 'go.mod' 28 | 29 | - name: 'Test' 30 | run: |- 31 | go test \ 32 | -count=1 \ 33 | -race \ 34 | -short \ 35 | -timeout=5m \ 36 | ./... 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Retry 2 | 3 | [![GoDoc](https://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](https://pkg.go.dev/mod/github.com/sethvargo/go-retry) 4 | 5 | Retry is a Go library for facilitating retry logic and backoff. It's highly 6 | extensible with full control over how and when retries occur. You can also write 7 | your own custom backoff functions by implementing the Backoff interface. 8 | 9 | ## Features 10 | 11 | - **Extensible** - Inspired by Go's built-in HTTP package, this Go backoff and 12 | retry library is extensible via middleware. You can write custom backoff 13 | functions or use a provided filter. 14 | 15 | - **Independent** - No external dependencies besides the Go standard library, 16 | meaning it won't bloat your project. 17 | 18 | - **Concurrent** - Unless otherwise specified, everything is safe for concurrent 19 | use. 20 | 21 | - **Context-aware** - Use native Go contexts to control cancellation. 22 | 23 | ## Usage 24 | 25 | Here is an example use for connecting to a database using Go's `database/sql` 26 | package: 27 | 28 | ```golang 29 | package main 30 | 31 | import ( 32 | "context" 33 | "database/sql" 34 | "log" 35 | "time" 36 | 37 | "github.com/sethvargo/go-retry" 38 | ) 39 | 40 | func main() { 41 | db, err := sql.Open("mysql", "...") 42 | if err != nil { 43 | log.Fatal(err) 44 | } 45 | 46 | ctx := context.Background() 47 | if err := retry.Fibonacci(ctx, 1*time.Second, func(ctx context.Context) error { 48 | if err := db.PingContext(ctx); err != nil { 49 | // This marks the error as retryable 50 | return retry.RetryableError(err) 51 | } 52 | return nil 53 | }); err != nil { 54 | log.Fatal(err) 55 | } 56 | } 57 | ``` 58 | 59 | ## Backoffs 60 | 61 | In addition to your own custom algorithms, there are built-in algorithms for 62 | backoff in the library. 63 | 64 | ### Constant 65 | 66 | A very rudimentary backoff, just returns a constant value. Here is an example: 67 | 68 | ```text 69 | 1s -> 1s -> 1s -> 1s -> 1s -> 1s 70 | ``` 71 | 72 | Usage: 73 | 74 | ```golang 75 | NewConstant(1 * time.Second) 76 | ``` 77 | 78 | ### Exponential 79 | 80 | Arguably the most common backoff, the next value is double the previous value. 81 | Here is an example: 82 | 83 | ```text 84 | 1s -> 2s -> 4s -> 8s -> 16s -> 32s -> 64s 85 | ``` 86 | 87 | Usage: 88 | 89 | ```golang 90 | NewExponential(1 * time.Second) 91 | ``` 92 | 93 | ### Fibonacci 94 | 95 | The Fibonacci backoff uses the Fibonacci sequence to calculate the backoff. The 96 | next value is the sum of the current value and the previous value. This means 97 | retires happen quickly at first, but then gradually take slower, ideal for 98 | network-type issues. Here is an example: 99 | 100 | ```text 101 | 1s -> 1s -> 2s -> 3s -> 5s -> 8s -> 13s 102 | ``` 103 | 104 | Usage: 105 | 106 | ```golang 107 | NewFibonacci(1 * time.Second) 108 | ``` 109 | 110 | ## Modifiers (Middleware) 111 | 112 | The built-in backoff algorithms never terminate and have no caps or limits - you 113 | control their behavior with middleware. There's built-in middleware, but you can 114 | also write custom middleware. 115 | 116 | ### Jitter 117 | 118 | To reduce the changes of a thundering herd, add random jitter to the returned 119 | value. 120 | 121 | ```golang 122 | b := NewFibonacci(1 * time.Second) 123 | 124 | // Return the next value, +/- 500ms 125 | b = WithJitter(500*time.Millisecond, b) 126 | 127 | // Return the next value, +/- 5% of the result 128 | b = WithJitterPercent(5, b) 129 | ``` 130 | 131 | ### MaxRetries 132 | 133 | To terminate a retry, specify the maximum number of _retries_. Note this 134 | is _retries_, not _attempts_. Attempts is retries + 1. 135 | 136 | ```golang 137 | b := NewFibonacci(1 * time.Second) 138 | 139 | // Stop after 4 retries, when the 5th attempt has failed. In this example, the worst case elapsed 140 | // time would be 1s + 1s + 2s + 3s = 7s. 141 | b = WithMaxRetries(4, b) 142 | ``` 143 | 144 | ### CappedDuration 145 | 146 | To ensure an individual calculated duration never exceeds a value, use a cap: 147 | 148 | ```golang 149 | b := NewFibonacci(1 * time.Second) 150 | 151 | // Ensure the maximum value is 2s. In this example, the sleep values would be 152 | // 1s, 1s, 2s, 2s, 2s, 2s... 153 | b = WithCappedDuration(2 * time.Second, b) 154 | ``` 155 | 156 | ### WithMaxDuration 157 | 158 | For a best-effort limit on the total execution time, specify a max duration: 159 | 160 | ```golang 161 | b := NewFibonacci(1 * time.Second) 162 | 163 | // Ensure the maximum total retry time is 5s. 164 | b = WithMaxDuration(5 * time.Second, b) 165 | ``` 166 | 167 | ## Benchmarks 168 | 169 | Here are benchmarks against some other popular Go backoff and retry libraries. 170 | You can run these benchmarks yourself via the `benchmark/` folder. Commas and 171 | spacing fixed for clarity. 172 | 173 | ```text 174 | Benchmark/cenkalti-7 13,052,668 87.3 ns/op 175 | Benchmark/lestrrat-7 902,044 1,355 ns/op 176 | Benchmark/sethvargo-7 203,914,245 5.73 ns/op 177 | ``` 178 | 179 | ## Notes and Caveats 180 | 181 | - Randomization uses `math/rand` seeded with the Unix timestamp instead of 182 | `crypto/rand`. 183 | - Ordering of addition of multiple modifiers will make a difference. 184 | For example; ensure you add `CappedDuration` before `WithMaxDuration`, otherwise it may early out too early. 185 | Another example is you could add `Jitter` before or after capping depending on your desired outcome. 186 | -------------------------------------------------------------------------------- /backoff.go: -------------------------------------------------------------------------------- 1 | package retry 2 | 3 | import ( 4 | "sync" 5 | "time" 6 | ) 7 | 8 | // Backoff is an interface that backs off. 9 | type Backoff interface { 10 | // Next returns the time duration to wait and whether to stop. 11 | Next() (next time.Duration, stop bool) 12 | } 13 | 14 | var _ Backoff = (BackoffFunc)(nil) 15 | 16 | // BackoffFunc is a backoff expressed as a function. 17 | type BackoffFunc func() (time.Duration, bool) 18 | 19 | // Next implements Backoff. 20 | func (b BackoffFunc) Next() (time.Duration, bool) { 21 | return b() 22 | } 23 | 24 | // WithJitter wraps a backoff function and adds the specified jitter. j can be 25 | // interpreted as "+/- j". For example, if j were 5 seconds and the backoff 26 | // returned 20s, the value could be between 15 and 25 seconds. The value can 27 | // never be less than 0. 28 | func WithJitter(j time.Duration, next Backoff) Backoff { 29 | r := newLockedRandom(time.Now().UnixNano()) 30 | 31 | return BackoffFunc(func() (time.Duration, bool) { 32 | val, stop := next.Next() 33 | if stop { 34 | return 0, true 35 | } 36 | 37 | diff := time.Duration(r.Int63n(int64(j)*2) - int64(j)) 38 | val = val + diff 39 | if val < 0 { 40 | val = 0 41 | } 42 | return val, false 43 | }) 44 | } 45 | 46 | // WithJitterPercent wraps a backoff function and adds the specified jitter 47 | // percentage. j can be interpreted as "+/- j%". For example, if j were 5 and 48 | // the backoff returned 20s, the value could be between 19 and 21 seconds. The 49 | // value can never be less than 0 or greater than 100. 50 | func WithJitterPercent(j uint64, next Backoff) Backoff { 51 | r := newLockedRandom(time.Now().UnixNano()) 52 | 53 | return BackoffFunc(func() (time.Duration, bool) { 54 | val, stop := next.Next() 55 | if stop { 56 | return 0, true 57 | } 58 | 59 | // Get a value between -j and j, the convert to a percentage 60 | top := r.Int63n(int64(j)*2) - int64(j) 61 | pct := 1 - float64(top)/100.0 62 | 63 | val = time.Duration(float64(val) * pct) 64 | if val < 0 { 65 | val = 0 66 | } 67 | return val, false 68 | }) 69 | } 70 | 71 | // WithMaxRetries executes the backoff function up until the maximum attempts. 72 | func WithMaxRetries(max uint64, next Backoff) Backoff { 73 | var l sync.Mutex 74 | var attempt uint64 75 | 76 | return BackoffFunc(func() (time.Duration, bool) { 77 | l.Lock() 78 | defer l.Unlock() 79 | 80 | if attempt >= max { 81 | return 0, true 82 | } 83 | attempt++ 84 | 85 | val, stop := next.Next() 86 | if stop { 87 | return 0, true 88 | } 89 | 90 | return val, false 91 | }) 92 | } 93 | 94 | // WithCappedDuration sets a maximum on the duration returned from the next 95 | // backoff. This is NOT a total backoff time, but rather a cap on the maximum 96 | // value a backoff can return. Without another middleware, the backoff will 97 | // continue infinitely. 98 | func WithCappedDuration(cap time.Duration, next Backoff) Backoff { 99 | return BackoffFunc(func() (time.Duration, bool) { 100 | val, stop := next.Next() 101 | if stop { 102 | return 0, true 103 | } 104 | 105 | if val <= 0 || val > cap { 106 | val = cap 107 | } 108 | return val, false 109 | }) 110 | } 111 | 112 | // WithMaxDuration sets a maximum on the total amount of time a backoff should 113 | // execute. It's best-effort, and should not be used to guarantee an exact 114 | // amount of time. 115 | func WithMaxDuration(timeout time.Duration, next Backoff) Backoff { 116 | start := time.Now() 117 | 118 | return BackoffFunc(func() (time.Duration, bool) { 119 | diff := timeout - time.Since(start) 120 | if diff <= 0 { 121 | return 0, true 122 | } 123 | 124 | val, stop := next.Next() 125 | if stop { 126 | return 0, true 127 | } 128 | 129 | if val <= 0 || val > diff { 130 | val = diff 131 | } 132 | return val, false 133 | }) 134 | } 135 | -------------------------------------------------------------------------------- /backoff_constant.go: -------------------------------------------------------------------------------- 1 | package retry 2 | 3 | import ( 4 | "context" 5 | "time" 6 | ) 7 | 8 | // Constant is a wrapper around Retry that uses a constant backoff. It panics if 9 | // the given base is less than zero. 10 | func Constant(ctx context.Context, t time.Duration, f RetryFunc) error { 11 | return Do(ctx, NewConstant(t), f) 12 | } 13 | 14 | // NewConstant creates a new constant backoff using the value t. The wait time 15 | // is the provided constant value. It panics if the given base is less than 16 | // zero. 17 | func NewConstant(t time.Duration) Backoff { 18 | if t <= 0 { 19 | panic("t must be greater than 0") 20 | } 21 | 22 | return BackoffFunc(func() (time.Duration, bool) { 23 | return t, false 24 | }) 25 | } 26 | -------------------------------------------------------------------------------- /backoff_constant_test.go: -------------------------------------------------------------------------------- 1 | package retry_test 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | "sort" 7 | "testing" 8 | "time" 9 | 10 | "github.com/sethvargo/go-retry" 11 | ) 12 | 13 | func TestConstantBackoff(t *testing.T) { 14 | t.Parallel() 15 | 16 | cases := []struct { 17 | name string 18 | base time.Duration 19 | tries int 20 | exp []time.Duration 21 | }{ 22 | { 23 | name: "single", 24 | base: 1 * time.Nanosecond, 25 | tries: 1, 26 | exp: []time.Duration{ 27 | 1 * time.Nanosecond, 28 | }, 29 | }, 30 | { 31 | name: "max", 32 | base: 10 * time.Millisecond, 33 | tries: 5, 34 | exp: []time.Duration{ 35 | 10 * time.Millisecond, 36 | 10 * time.Millisecond, 37 | 10 * time.Millisecond, 38 | 10 * time.Millisecond, 39 | 10 * time.Millisecond, 40 | }, 41 | }, 42 | { 43 | name: "many", 44 | base: 1 * time.Nanosecond, 45 | tries: 14, 46 | exp: []time.Duration{ 47 | 1 * time.Nanosecond, 48 | 1 * time.Nanosecond, 49 | 1 * time.Nanosecond, 50 | 1 * time.Nanosecond, 51 | 1 * time.Nanosecond, 52 | 1 * time.Nanosecond, 53 | 1 * time.Nanosecond, 54 | 1 * time.Nanosecond, 55 | 1 * time.Nanosecond, 56 | 1 * time.Nanosecond, 57 | 1 * time.Nanosecond, 58 | 1 * time.Nanosecond, 59 | 1 * time.Nanosecond, 60 | 1 * time.Nanosecond, 61 | }, 62 | }, 63 | } 64 | 65 | for _, tc := range cases { 66 | tc := tc 67 | 68 | t.Run(tc.name, func(t *testing.T) { 69 | t.Parallel() 70 | 71 | b := retry.NewConstant(tc.base) 72 | 73 | resultsCh := make(chan time.Duration, tc.tries) 74 | for i := 0; i < tc.tries; i++ { 75 | go func() { 76 | r, _ := b.Next() 77 | resultsCh <- r 78 | }() 79 | } 80 | 81 | results := make([]time.Duration, tc.tries) 82 | for i := 0; i < tc.tries; i++ { 83 | select { 84 | case val := <-resultsCh: 85 | results[i] = val 86 | case <-time.After(5 * time.Second): 87 | t.Fatal("timeout") 88 | } 89 | } 90 | sort.Slice(results, func(i, j int) bool { 91 | return results[i] < results[j] 92 | }) 93 | 94 | if !reflect.DeepEqual(results, tc.exp) { 95 | t.Errorf("expected \n\n%v\n\n to be \n\n%v\n\n", results, tc.exp) 96 | } 97 | }) 98 | } 99 | } 100 | 101 | func ExampleNewConstant() { 102 | b := retry.NewConstant(1 * time.Second) 103 | 104 | for i := 0; i < 5; i++ { 105 | val, _ := b.Next() 106 | fmt.Printf("%v\n", val) 107 | } 108 | // Output: 109 | // 1s 110 | // 1s 111 | // 1s 112 | // 1s 113 | // 1s 114 | } 115 | -------------------------------------------------------------------------------- /backoff_exponential.go: -------------------------------------------------------------------------------- 1 | package retry 2 | 3 | import ( 4 | "context" 5 | "math" 6 | "sync/atomic" 7 | "time" 8 | ) 9 | 10 | type exponentialBackoff struct { 11 | base time.Duration 12 | attempt uint64 13 | } 14 | 15 | // Exponential is a wrapper around Retry that uses an exponential backoff. See 16 | // NewExponential. 17 | func Exponential(ctx context.Context, base time.Duration, f RetryFunc) error { 18 | return Do(ctx, NewExponential(base), f) 19 | } 20 | 21 | // NewExponential creates a new exponential backoff using the starting value of 22 | // base and doubling on each failure (1, 2, 4, 8, 16, 32, 64...), up to max. 23 | // 24 | // Once it overflows, the function constantly returns the maximum time.Duration 25 | // for a 64-bit integer. 26 | // 27 | // It panics if the given base is less than zero. 28 | func NewExponential(base time.Duration) Backoff { 29 | if base <= 0 { 30 | panic("base must be greater than 0") 31 | } 32 | 33 | return &exponentialBackoff{ 34 | base: base, 35 | } 36 | } 37 | 38 | // Next implements Backoff. It is safe for concurrent use. 39 | func (b *exponentialBackoff) Next() (time.Duration, bool) { 40 | next := b.base << (atomic.AddUint64(&b.attempt, 1) - 1) 41 | if next <= 0 { 42 | atomic.AddUint64(&b.attempt, ^uint64(0)) 43 | next = math.MaxInt64 44 | } 45 | 46 | return next, false 47 | } 48 | -------------------------------------------------------------------------------- /backoff_exponential_test.go: -------------------------------------------------------------------------------- 1 | package retry_test 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "reflect" 7 | "sort" 8 | "testing" 9 | "time" 10 | 11 | "github.com/sethvargo/go-retry" 12 | ) 13 | 14 | func TestExponentialBackoff(t *testing.T) { 15 | t.Parallel() 16 | 17 | cases := []struct { 18 | name string 19 | base time.Duration 20 | tries int 21 | exp []time.Duration 22 | }{ 23 | { 24 | name: "single", 25 | base: 1 * time.Nanosecond, 26 | tries: 1, 27 | exp: []time.Duration{ 28 | 1 * time.Nanosecond, 29 | }, 30 | }, 31 | { 32 | name: "many", 33 | base: 1 * time.Nanosecond, 34 | tries: 14, 35 | exp: []time.Duration{ 36 | 1 * time.Nanosecond, 37 | 2 * time.Nanosecond, 38 | 4 * time.Nanosecond, 39 | 8 * time.Nanosecond, 40 | 16 * time.Nanosecond, 41 | 32 * time.Nanosecond, 42 | 64 * time.Nanosecond, 43 | 128 * time.Nanosecond, 44 | 256 * time.Nanosecond, 45 | 512 * time.Nanosecond, 46 | 1024 * time.Nanosecond, 47 | 2048 * time.Nanosecond, 48 | 4096 * time.Nanosecond, 49 | 8192 * time.Nanosecond, 50 | }, 51 | }, 52 | { 53 | name: "overflow", 54 | base: 100_000 * time.Hour, 55 | tries: 10, 56 | exp: []time.Duration{ 57 | 100_000 * time.Hour, 58 | 200_000 * time.Hour, 59 | 400_000 * time.Hour, 60 | 800_000 * time.Hour, 61 | 1_600_000 * time.Hour, 62 | math.MaxInt64, 63 | math.MaxInt64, 64 | math.MaxInt64, 65 | math.MaxInt64, 66 | math.MaxInt64, 67 | }, 68 | }, 69 | } 70 | 71 | for _, tc := range cases { 72 | tc := tc 73 | 74 | t.Run(tc.name, func(t *testing.T) { 75 | t.Parallel() 76 | 77 | b := retry.NewExponential(tc.base) 78 | 79 | resultsCh := make(chan time.Duration, tc.tries) 80 | for i := 0; i < tc.tries; i++ { 81 | go func() { 82 | r, _ := b.Next() 83 | resultsCh <- r 84 | }() 85 | } 86 | 87 | results := make([]time.Duration, tc.tries) 88 | for i := 0; i < tc.tries; i++ { 89 | select { 90 | case val := <-resultsCh: 91 | results[i] = val 92 | case <-time.After(5 * time.Second): 93 | t.Fatal("timeout") 94 | } 95 | } 96 | sort.Slice(results, func(i, j int) bool { 97 | return results[i] < results[j] 98 | }) 99 | 100 | if !reflect.DeepEqual(results, tc.exp) { 101 | t.Errorf("expected \n\n%v\n\n to be \n\n%v\n\n", results, tc.exp) 102 | } 103 | }) 104 | } 105 | } 106 | 107 | func ExampleNewExponential() { 108 | b := retry.NewExponential(1 * time.Second) 109 | 110 | for i := 0; i < 5; i++ { 111 | val, _ := b.Next() 112 | fmt.Printf("%v\n", val) 113 | } 114 | // Output: 115 | // 1s 116 | // 2s 117 | // 4s 118 | // 8s 119 | // 16s 120 | } 121 | -------------------------------------------------------------------------------- /backoff_fibonacci.go: -------------------------------------------------------------------------------- 1 | package retry 2 | 3 | import ( 4 | "context" 5 | "math" 6 | "sync/atomic" 7 | "time" 8 | "unsafe" 9 | ) 10 | 11 | type state [2]time.Duration 12 | 13 | type fibonacciBackoff struct { 14 | state unsafe.Pointer 15 | } 16 | 17 | // Fibonacci is a wrapper around Retry that uses a Fibonacci backoff. See 18 | // NewFibonacci. 19 | func Fibonacci(ctx context.Context, base time.Duration, f RetryFunc) error { 20 | return Do(ctx, NewFibonacci(base), f) 21 | } 22 | 23 | // NewFibonacci creates a new Fibonacci backoff using the starting value of 24 | // base. The wait time is the sum of the previous two wait times on each failed 25 | // attempt (1, 1, 2, 3, 5, 8, 13...). 26 | // 27 | // Once it overflows, the function constantly returns the maximum time.Duration 28 | // for a 64-bit integer. 29 | // 30 | // It panics if the given base is less than zero. 31 | func NewFibonacci(base time.Duration) Backoff { 32 | if base <= 0 { 33 | panic("base must be greater than 0") 34 | } 35 | 36 | return &fibonacciBackoff{ 37 | state: unsafe.Pointer(&state{0, base}), 38 | } 39 | } 40 | 41 | // Next implements Backoff. It is safe for concurrent use. 42 | func (b *fibonacciBackoff) Next() (time.Duration, bool) { 43 | for { 44 | curr := atomic.LoadPointer(&b.state) 45 | currState := (*state)(curr) 46 | next := currState[0] + currState[1] 47 | 48 | if next <= 0 { 49 | return math.MaxInt64, false 50 | } 51 | 52 | if atomic.CompareAndSwapPointer(&b.state, curr, unsafe.Pointer(&state{currState[1], next})) { 53 | return next, false 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /backoff_fibonacci_test.go: -------------------------------------------------------------------------------- 1 | package retry_test 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | "reflect" 7 | "sort" 8 | "testing" 9 | "time" 10 | 11 | "github.com/sethvargo/go-retry" 12 | ) 13 | 14 | func TestFibonacciBackoff(t *testing.T) { 15 | t.Parallel() 16 | 17 | cases := []struct { 18 | name string 19 | base time.Duration 20 | tries int 21 | exp []time.Duration 22 | }{ 23 | { 24 | name: "single", 25 | base: 1 * time.Nanosecond, 26 | tries: 1, 27 | exp: []time.Duration{ 28 | 1 * time.Nanosecond, 29 | }, 30 | }, 31 | { 32 | name: "max", 33 | base: 10 * time.Millisecond, 34 | tries: 5, 35 | exp: []time.Duration{ 36 | 10 * time.Millisecond, 37 | 20 * time.Millisecond, 38 | 30 * time.Millisecond, 39 | 50 * time.Millisecond, 40 | 80 * time.Millisecond, 41 | }, 42 | }, 43 | { 44 | name: "many", 45 | base: 1 * time.Nanosecond, 46 | tries: 14, 47 | exp: []time.Duration{ 48 | 1 * time.Nanosecond, 49 | 2 * time.Nanosecond, 50 | 3 * time.Nanosecond, 51 | 5 * time.Nanosecond, 52 | 8 * time.Nanosecond, 53 | 13 * time.Nanosecond, 54 | 21 * time.Nanosecond, 55 | 34 * time.Nanosecond, 56 | 55 * time.Nanosecond, 57 | 89 * time.Nanosecond, 58 | 144 * time.Nanosecond, 59 | 233 * time.Nanosecond, 60 | 377 * time.Nanosecond, 61 | 610 * time.Nanosecond, 62 | }, 63 | }, 64 | { 65 | name: "overflow", 66 | base: 100_000 * time.Hour, 67 | tries: 10, 68 | exp: []time.Duration{ 69 | 100_000 * time.Hour, 70 | 200_000 * time.Hour, 71 | 300_000 * time.Hour, 72 | 500_000 * time.Hour, 73 | 800_000 * time.Hour, 74 | 1_300_000 * time.Hour, 75 | 2_100_000 * time.Hour, 76 | math.MaxInt64, 77 | math.MaxInt64, 78 | math.MaxInt64, 79 | }, 80 | }, 81 | } 82 | 83 | for _, tc := range cases { 84 | tc := tc 85 | 86 | t.Run(tc.name, func(t *testing.T) { 87 | t.Parallel() 88 | 89 | b := retry.NewFibonacci(tc.base) 90 | 91 | resultsCh := make(chan time.Duration, tc.tries) 92 | for i := 0; i < tc.tries; i++ { 93 | go func() { 94 | r, _ := b.Next() 95 | resultsCh <- r 96 | }() 97 | } 98 | 99 | results := make([]time.Duration, tc.tries) 100 | for i := 0; i < tc.tries; i++ { 101 | select { 102 | case val := <-resultsCh: 103 | results[i] = val 104 | case <-time.After(5 * time.Second): 105 | t.Fatal("timeout") 106 | } 107 | } 108 | sort.Slice(results, func(i, j int) bool { 109 | return results[i] < results[j] 110 | }) 111 | 112 | if !reflect.DeepEqual(results, tc.exp) { 113 | t.Errorf("expected \n\n%v\n\n to be \n\n%v\n\n", results, tc.exp) 114 | } 115 | }) 116 | } 117 | } 118 | 119 | func ExampleNewFibonacci() { 120 | b := retry.NewFibonacci(1 * time.Second) 121 | 122 | for i := 0; i < 5; i++ { 123 | val, _ := b.Next() 124 | fmt.Printf("%v\n", val) 125 | } 126 | // Output: 127 | // 1s 128 | // 2s 129 | // 3s 130 | // 5s 131 | // 8s 132 | } 133 | -------------------------------------------------------------------------------- /backoff_test.go: -------------------------------------------------------------------------------- 1 | package retry_test 2 | 3 | import ( 4 | "context" 5 | "testing" 6 | "time" 7 | 8 | "github.com/sethvargo/go-retry" 9 | ) 10 | 11 | func ExampleBackoffFunc() { 12 | ctx := context.Background() 13 | 14 | // Example backoff middleware that adds the provided duration t to the result. 15 | withShift := func(t time.Duration, next retry.Backoff) retry.BackoffFunc { 16 | return func() (time.Duration, bool) { 17 | val, stop := next.Next() 18 | if stop { 19 | return 0, true 20 | } 21 | return val + t, false 22 | } 23 | } 24 | 25 | // Middlewrap wrap another backoff: 26 | b := retry.NewFibonacci(1 * time.Second) 27 | b = withShift(5*time.Second, b) 28 | 29 | if err := retry.Do(ctx, b, func(ctx context.Context) error { 30 | // Actual retry logic here 31 | return nil 32 | }); err != nil { 33 | // handle error 34 | } 35 | } 36 | 37 | func TestWithJitter(t *testing.T) { 38 | t.Parallel() 39 | 40 | for i := 0; i < 100_000; i++ { 41 | b := retry.WithJitter(250*time.Millisecond, retry.BackoffFunc(func() (time.Duration, bool) { 42 | return 1 * time.Second, false 43 | })) 44 | val, stop := b.Next() 45 | if stop { 46 | t.Errorf("should not stop") 47 | } 48 | 49 | if min, max := 750*time.Millisecond, 1250*time.Millisecond; val < min || val > max { 50 | t.Errorf("expected %v to be between %v and %v", val, min, max) 51 | } 52 | } 53 | } 54 | 55 | func ExampleWithJitter() { 56 | ctx := context.Background() 57 | 58 | b := retry.NewFibonacci(1 * time.Second) 59 | b = retry.WithJitter(1*time.Second, b) 60 | 61 | if err := retry.Do(ctx, b, func(_ context.Context) error { 62 | // TODO: logic here 63 | return nil 64 | }); err != nil { 65 | // handle error 66 | } 67 | } 68 | 69 | func TestWithJitterPercent(t *testing.T) { 70 | t.Parallel() 71 | 72 | for i := 0; i < 100_000; i++ { 73 | b := retry.WithJitterPercent(5, retry.BackoffFunc(func() (time.Duration, bool) { 74 | return 1 * time.Second, false 75 | })) 76 | val, stop := b.Next() 77 | if stop { 78 | t.Errorf("should not stop") 79 | } 80 | 81 | if min, max := 950*time.Millisecond, 1050*time.Millisecond; val < min || val > max { 82 | t.Errorf("expected %v to be between %v and %v", val, min, max) 83 | } 84 | } 85 | } 86 | 87 | func ExampleWithJitterPercent() { 88 | ctx := context.Background() 89 | 90 | b := retry.NewFibonacci(1 * time.Second) 91 | b = retry.WithJitterPercent(5, b) 92 | 93 | if err := retry.Do(ctx, b, func(_ context.Context) error { 94 | // TODO: logic here 95 | return nil 96 | }); err != nil { 97 | // handle error 98 | } 99 | } 100 | 101 | func TestWithMaxRetries(t *testing.T) { 102 | t.Parallel() 103 | 104 | b := retry.WithMaxRetries(3, retry.BackoffFunc(func() (time.Duration, bool) { 105 | return 1 * time.Second, false 106 | })) 107 | 108 | // First 3 attempts succeed 109 | for i := 0; i < 3; i++ { 110 | val, stop := b.Next() 111 | if stop { 112 | t.Errorf("should not stop") 113 | } 114 | if val != 1*time.Second { 115 | t.Errorf("expected %v to be %v", val, 1*time.Second) 116 | } 117 | } 118 | 119 | // Now we stop 120 | val, stop := b.Next() 121 | if !stop { 122 | t.Errorf("should stop") 123 | } 124 | if val != 0 { 125 | t.Errorf("expected %v to be %v", val, 0) 126 | } 127 | } 128 | 129 | func ExampleWithMaxRetries() { 130 | ctx := context.Background() 131 | 132 | b := retry.NewFibonacci(1 * time.Second) 133 | b = retry.WithMaxRetries(3, b) 134 | 135 | if err := retry.Do(ctx, b, func(_ context.Context) error { 136 | // TODO: logic here 137 | return nil 138 | }); err != nil { 139 | // handle error 140 | } 141 | } 142 | 143 | func TestWithCappedDuration(t *testing.T) { 144 | t.Parallel() 145 | 146 | b := retry.WithCappedDuration(3*time.Second, retry.BackoffFunc(func() (time.Duration, bool) { 147 | return 5 * time.Second, false 148 | })) 149 | 150 | val, stop := b.Next() 151 | if stop { 152 | t.Errorf("should not stop") 153 | } 154 | if val != 3*time.Second { 155 | t.Errorf("expected %v to be %v", val, 3*time.Second) 156 | } 157 | } 158 | 159 | func ExampleWithCappedDuration() { 160 | ctx := context.Background() 161 | 162 | b := retry.NewFibonacci(1 * time.Second) 163 | b = retry.WithCappedDuration(3*time.Second, b) 164 | 165 | if err := retry.Do(ctx, b, func(_ context.Context) error { 166 | // TODO: logic here 167 | return nil 168 | }); err != nil { 169 | // handle error 170 | } 171 | } 172 | 173 | func TestWithMaxDuration(t *testing.T) { 174 | t.Parallel() 175 | 176 | b := retry.WithMaxDuration(250*time.Millisecond, retry.BackoffFunc(func() (time.Duration, bool) { 177 | return 1 * time.Second, false 178 | })) 179 | 180 | // Take once, within timeout. 181 | val, stop := b.Next() 182 | if stop { 183 | t.Error("should not stop") 184 | } 185 | 186 | if val > 250*time.Millisecond { 187 | t.Errorf("expected %v to be less than %v", val, 250*time.Millisecond) 188 | } 189 | 190 | time.Sleep(200 * time.Millisecond) 191 | 192 | // Take again, remainder contines 193 | val, stop = b.Next() 194 | if stop { 195 | t.Error("should not stop") 196 | } 197 | 198 | if val > 50*time.Millisecond { 199 | t.Errorf("expected %v to be less than %v", val, 50*time.Millisecond) 200 | } 201 | 202 | time.Sleep(50 * time.Millisecond) 203 | 204 | // Now we stop 205 | val, stop = b.Next() 206 | if !stop { 207 | t.Errorf("should stop") 208 | } 209 | if val != 0 { 210 | t.Errorf("expected %v to be %v", val, 0) 211 | } 212 | } 213 | 214 | func ExampleWithMaxDuration() { 215 | ctx := context.Background() 216 | 217 | b := retry.NewFibonacci(1 * time.Second) 218 | b = retry.WithMaxDuration(5*time.Second, b) 219 | 220 | if err := retry.Do(ctx, b, func(_ context.Context) error { 221 | // TODO: logic here 222 | return nil 223 | }); err != nil { 224 | // handle error 225 | } 226 | } 227 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/sethvargo/go-retry 2 | 3 | go 1.21 4 | 5 | // Something weird happened with the v0.2.0 tag where the commit in the module 6 | // registry doesn't match the commit on GitHub. 7 | retract v0.2.0 8 | -------------------------------------------------------------------------------- /rand.go: -------------------------------------------------------------------------------- 1 | package retry 2 | 3 | import ( 4 | "math/rand" 5 | "sync" 6 | ) 7 | 8 | type lockedSource struct { 9 | src *rand.Rand 10 | mu sync.Mutex 11 | } 12 | 13 | var _ rand.Source64 = (*lockedSource)(nil) 14 | 15 | func newLockedRandom(seed int64) *lockedSource { 16 | return &lockedSource{src: rand.New(rand.NewSource(seed))} 17 | } 18 | 19 | // Int63 mimics math/rand.(*Rand).Int63 with mutex locked. 20 | func (r *lockedSource) Int63() int64 { 21 | r.mu.Lock() 22 | defer r.mu.Unlock() 23 | return r.src.Int63() 24 | } 25 | 26 | // Seed mimics math/rand.(*Rand).Seed with mutex locked. 27 | func (r *lockedSource) Seed(seed int64) { 28 | r.mu.Lock() 29 | defer r.mu.Unlock() 30 | r.src.Seed(seed) 31 | } 32 | 33 | // Uint64 mimics math/rand.(*Rand).Uint64 with mutex locked. 34 | func (r *lockedSource) Uint64() uint64 { 35 | r.mu.Lock() 36 | defer r.mu.Unlock() 37 | return r.src.Uint64() 38 | } 39 | 40 | // Int63n mimics math/rand.(*Rand).Int63n with mutex locked. 41 | func (r *lockedSource) Int63n(n int64) int64 { 42 | if n <= 0 { 43 | panic("invalid argument to Int63n") 44 | } 45 | if n&(n-1) == 0 { // n is power of two, can mask 46 | return r.Int63() & (n - 1) 47 | } 48 | max := int64((1 << 63) - 1 - (1<<63)%uint64(n)) 49 | v := r.Int63() 50 | for v > max { 51 | v = r.Int63() 52 | } 53 | return v % n 54 | } 55 | -------------------------------------------------------------------------------- /retry.go: -------------------------------------------------------------------------------- 1 | // Package retry provides helpers for retrying. 2 | // 3 | // This package defines flexible interfaces for retrying Go functions that may 4 | // be flakey or eventually consistent. It abstracts the "backoff" (how long to 5 | // wait between tries) and "retry" (execute the function again) mechanisms for 6 | // maximum flexibility. Furthermore, everything is an interface, so you can 7 | // define your own implementations. 8 | // 9 | // The package is modeled after Go's built-in HTTP package, making it easy to 10 | // customize the built-in backoff with your own custom logic. Additionally, 11 | // callers specify which errors are retryable by wrapping them. This is helpful 12 | // with complex operations where only certain results should retry. 13 | package retry 14 | 15 | import ( 16 | "context" 17 | "errors" 18 | "time" 19 | ) 20 | 21 | // RetryFunc is a function passed to [Do]. 22 | type RetryFunc func(ctx context.Context) error 23 | 24 | // RetryFuncValue is a function passed to [Do] which returns a value. 25 | type RetryFuncValue[T any] func(ctx context.Context) (T, error) 26 | 27 | type retryableError struct { 28 | err error 29 | } 30 | 31 | // RetryableError marks an error as retryable. 32 | func RetryableError(err error) error { 33 | if err == nil { 34 | return nil 35 | } 36 | return &retryableError{err} 37 | } 38 | 39 | // Unwrap implements error wrapping. 40 | func (e *retryableError) Unwrap() error { 41 | return e.err 42 | } 43 | 44 | // Error returns the error string. 45 | func (e *retryableError) Error() string { 46 | if e.err == nil { 47 | return "retryable: " 48 | } 49 | return "retryable: " + e.err.Error() 50 | } 51 | 52 | func DoValue[T any](ctx context.Context, b Backoff, f RetryFuncValue[T]) (T, error) { 53 | var nilT T 54 | 55 | for { 56 | // Return immediately if ctx is canceled 57 | select { 58 | case <-ctx.Done(): 59 | return nilT, ctx.Err() 60 | default: 61 | } 62 | 63 | v, err := f(ctx) 64 | if err == nil { 65 | return v, nil 66 | } 67 | 68 | // Not retryable 69 | var rerr *retryableError 70 | if !errors.As(err, &rerr) { 71 | return nilT, err 72 | } 73 | 74 | next, stop := b.Next() 75 | if stop { 76 | return nilT, rerr.Unwrap() 77 | } 78 | 79 | // ctx.Done() has priority, so we test it alone first 80 | select { 81 | case <-ctx.Done(): 82 | return nilT, ctx.Err() 83 | default: 84 | } 85 | 86 | t := time.NewTimer(next) 87 | select { 88 | case <-ctx.Done(): 89 | t.Stop() 90 | return nilT, ctx.Err() 91 | case <-t.C: 92 | continue 93 | } 94 | } 95 | } 96 | 97 | // Do wraps a function with a backoff to retry. The provided context is the same 98 | // context passed to the [RetryFunc]. 99 | func Do(ctx context.Context, b Backoff, f RetryFunc) error { 100 | _, err := DoValue(ctx, b, func(ctx context.Context) (*struct{}, error) { 101 | return nil, f(ctx) 102 | }) 103 | return err 104 | } 105 | -------------------------------------------------------------------------------- /retry_test.go: -------------------------------------------------------------------------------- 1 | package retry_test 2 | 3 | import ( 4 | "context" 5 | "errors" 6 | "fmt" 7 | "io" 8 | "net/http" 9 | "strings" 10 | "testing" 11 | "time" 12 | 13 | "github.com/sethvargo/go-retry" 14 | ) 15 | 16 | func TestRetryableError(t *testing.T) { 17 | t.Parallel() 18 | 19 | err := retry.RetryableError(fmt.Errorf("oops")) 20 | if got, want := err.Error(), "retryable: "; !strings.Contains(got, want) { 21 | t.Errorf("expected %v to contain %v", got, want) 22 | } 23 | } 24 | 25 | func TestDoValue(t *testing.T) { 26 | t.Parallel() 27 | 28 | t.Run("returns_value", func(t *testing.T) { 29 | t.Parallel() 30 | 31 | ctx := context.Background() 32 | b := retry.WithMaxRetries(3, retry.BackoffFunc(func() (time.Duration, bool) { 33 | return 1 * time.Nanosecond, false 34 | })) 35 | 36 | v, err := retry.DoValue(ctx, b, func(_ context.Context) (string, error) { 37 | return "foo", nil 38 | }) 39 | if err != nil { 40 | t.Fatal("expected err") 41 | } 42 | 43 | if got, want := v, "foo"; got != want { 44 | t.Errorf("expected %v to be %v", got, want) 45 | } 46 | }) 47 | } 48 | 49 | func TestDo(t *testing.T) { 50 | t.Parallel() 51 | 52 | t.Run("exit_on_max_attempt", func(t *testing.T) { 53 | t.Parallel() 54 | 55 | ctx := context.Background() 56 | b := retry.WithMaxRetries(3, retry.BackoffFunc(func() (time.Duration, bool) { 57 | return 1 * time.Nanosecond, false 58 | })) 59 | 60 | var i int 61 | if err := retry.Do(ctx, b, func(_ context.Context) error { 62 | i++ 63 | return retry.RetryableError(fmt.Errorf("oops")) 64 | }); err == nil { 65 | t.Fatal("expected err") 66 | } 67 | 68 | // 1 + retries 69 | if got, want := i, 4; got != want { 70 | t.Errorf("expected %v to be %v", got, want) 71 | } 72 | }) 73 | 74 | t.Run("exit_on_non_retryable", func(t *testing.T) { 75 | t.Parallel() 76 | 77 | ctx := context.Background() 78 | b := retry.WithMaxRetries(3, retry.BackoffFunc(func() (time.Duration, bool) { 79 | return 1 * time.Nanosecond, false 80 | })) 81 | 82 | var i int 83 | if err := retry.Do(ctx, b, func(_ context.Context) error { 84 | i++ 85 | return fmt.Errorf("oops") // not retryable 86 | }); err == nil { 87 | t.Fatal("expected err") 88 | } 89 | 90 | if got, want := i, 1; got != want { 91 | t.Errorf("expected %v to be %v", got, want) 92 | } 93 | }) 94 | 95 | t.Run("unwraps", func(t *testing.T) { 96 | t.Parallel() 97 | 98 | ctx := context.Background() 99 | b := retry.WithMaxRetries(1, retry.BackoffFunc(func() (time.Duration, bool) { 100 | return 1 * time.Nanosecond, false 101 | })) 102 | 103 | err := retry.Do(ctx, b, func(_ context.Context) error { 104 | return retry.RetryableError(io.EOF) 105 | }) 106 | if err == nil { 107 | t.Fatal("expected err") 108 | } 109 | 110 | if got, want := err, io.EOF; got != want { 111 | t.Errorf("expected %#v to be %#v", got, want) 112 | } 113 | }) 114 | 115 | t.Run("exit_no_error", func(t *testing.T) { 116 | t.Parallel() 117 | 118 | ctx := context.Background() 119 | b := retry.WithMaxRetries(3, retry.BackoffFunc(func() (time.Duration, bool) { 120 | return 1 * time.Nanosecond, false 121 | })) 122 | 123 | var i int 124 | if err := retry.Do(ctx, b, func(_ context.Context) error { 125 | i++ 126 | return nil // no error 127 | }); err != nil { 128 | t.Fatal("expected no err") 129 | } 130 | 131 | if got, want := i, 1; got != want { 132 | t.Errorf("expected %v to be %v", got, want) 133 | } 134 | }) 135 | 136 | t.Run("context_canceled", func(t *testing.T) { 137 | t.Parallel() 138 | 139 | b := retry.BackoffFunc(func() (time.Duration, bool) { 140 | return 5 * time.Second, false 141 | }) 142 | 143 | ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) 144 | defer cancel() 145 | 146 | if err := retry.Do(ctx, b, func(_ context.Context) error { 147 | return retry.RetryableError(fmt.Errorf("oops")) // no error 148 | }); err != context.DeadlineExceeded { 149 | t.Errorf("expected %v to be %v", err, context.DeadlineExceeded) 150 | } 151 | }) 152 | } 153 | 154 | func ExampleDo_simple() { 155 | ctx := context.Background() 156 | 157 | b := retry.NewFibonacci(1 * time.Nanosecond) 158 | 159 | i := 0 160 | if err := retry.Do(ctx, retry.WithMaxRetries(3, b), func(ctx context.Context) error { 161 | fmt.Printf("%d\n", i) 162 | i++ 163 | return retry.RetryableError(fmt.Errorf("oops")) 164 | }); err != nil { 165 | // handle error 166 | } 167 | 168 | // Output: 169 | // 0 170 | // 1 171 | // 2 172 | // 3 173 | } 174 | 175 | func ExampleDo_customRetry() { 176 | ctx := context.Background() 177 | 178 | b := retry.NewFibonacci(1 * time.Nanosecond) 179 | 180 | // This example demonstrates selectively retrying specific errors. Only errors 181 | // wrapped with RetryableError are eligible to be retried. 182 | if err := retry.Do(ctx, retry.WithMaxRetries(3, b), func(ctx context.Context) error { 183 | resp, err := http.Get("https://google.com/") 184 | if err != nil { 185 | return err 186 | } 187 | defer resp.Body.Close() 188 | 189 | switch resp.StatusCode / 100 { 190 | case 4: 191 | return fmt.Errorf("bad response: %v", resp.StatusCode) 192 | case 5: 193 | return retry.RetryableError(fmt.Errorf("bad response: %v", resp.StatusCode)) 194 | default: 195 | return nil 196 | } 197 | }); err != nil { 198 | // handle error 199 | } 200 | } 201 | 202 | func ExampleDoValue() { 203 | ctx := context.Background() 204 | 205 | b := retry.NewFibonacci(1 * time.Nanosecond) 206 | 207 | body, err := retry.DoValue(ctx, retry.WithMaxRetries(3, b), func(ctx context.Context) ([]byte, error) { 208 | resp, err := http.Get("https://google.com/") 209 | if err != nil { 210 | return nil, err 211 | } 212 | defer resp.Body.Close() 213 | 214 | switch resp.StatusCode / 100 { 215 | case 4: 216 | return nil, fmt.Errorf("bad response: %v", resp.StatusCode) 217 | case 5: 218 | return nil, retry.RetryableError(fmt.Errorf("bad response: %v", resp.StatusCode)) 219 | default: 220 | b, _ := io.ReadAll(resp.Body) 221 | return b, nil 222 | } 223 | }) 224 | if err != nil { 225 | // handle error 226 | } 227 | _ = body 228 | } 229 | 230 | func TestCancel(t *testing.T) { 231 | for i := 0; i < 100000; i++ { 232 | ctx, cancel := context.WithCancel(context.Background()) 233 | 234 | calls := 0 235 | rf := func(ctx context.Context) error { 236 | calls++ 237 | // Never succeed. 238 | // Always return a RetryableError 239 | return retry.RetryableError(errors.New("nope")) 240 | } 241 | 242 | const delay time.Duration = time.Millisecond 243 | b := retry.NewConstant(delay) 244 | 245 | const maxRetries = 5 246 | b = retry.WithMaxRetries(maxRetries, b) 247 | 248 | const jitter time.Duration = 5 * time.Millisecond 249 | b = retry.WithJitter(jitter, b) 250 | 251 | // Here we cancel the Context *before* the call to Do 252 | cancel() 253 | retry.Do(ctx, b, rf) 254 | 255 | if calls > 1 { 256 | t.Errorf("rf was called %d times instead of 0 or 1", calls) 257 | } 258 | } 259 | } 260 | --------------------------------------------------------------------------------