Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package retry | 5 package retry |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "time" | 8 "time" |
| 9 | 9 |
| 10 "github.com/luci/luci-go/common/clock" | 10 "github.com/luci/luci-go/common/clock" |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 ) | 12 ) |
| 13 | 13 |
| 14 // Stop is a sentinel value returned by a Iterator to indicate that no more | 14 // Stop is a sentinel value returned by a Iterator to indicate that no more |
| 15 // attempts should be made. | 15 // attempts should be made. |
| 16 const Stop time.Duration = -1 | 16 const Stop time.Duration = -1 |
| 17 | 17 |
| 18 // Callback is a callback function that Retry will invoke every time an | 18 // Callback is a callback function that Retry will invoke every time an |
| 19 // attempt fails prior to sleeping. | 19 // attempt fails prior to sleeping. |
| 20 type Callback func(error, time.Duration) | 20 type Callback func(error, time.Duration) |
| 21 | 21 |
| 22 // Iterator describes a stateful implementation of retry logic. | 22 // Iterator describes a stateful implementation of retry logic. |
| 23 type Iterator interface { | 23 type Iterator interface { |
| 24 // Returns the next retry delay, or Stop if no more retries should be ma de. | 24 // Returns the next retry delay, or Stop if no more retries should be ma de. |
| 25 Next(context.Context, error) time.Duration | 25 Next(context.Context, error) time.Duration |
| 26 } | 26 } |
| 27 | 27 |
| 28 // Generator is a function that produces an Iterator instance. | |
| 29 type Generator func() Iterator | |
|
dnj (Google)
2016/01/21 04:36:24
This concept is used enough, we might as well codi
| |
| 30 | |
| 28 // Retry executes a function 'f'. If the function returns an error, it will | 31 // Retry executes a function 'f'. If the function returns an error, it will |
| 29 // be re-executed according to a retry plan. | 32 // be re-executed according to a retry plan. |
| 30 // | 33 // |
| 31 // If the supplied context is canceled, retry will stop executing. Retry will | 34 // If the supplied context is canceled, retry will stop executing. Retry will |
| 32 // not execute the supplied function at all if the context is canceled when | 35 // not execute the supplied function at all if the context is canceled when |
| 33 // Retry is invoked. | 36 // Retry is invoked. |
| 34 // | 37 // |
| 35 // If 'callback' is not nil, it will be invoked if an error occurs (prior to | 38 // If 'callback' is not nil, it will be invoked if an error occurs (prior to |
| 36 // sleeping). | 39 // sleeping). |
| 37 func Retry(ctx context.Context, it Iterator, f func() error, callback Callback) (err error) { | 40 func Retry(ctx context.Context, it Iterator, f func() error, callback Callback) (err error) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 65 select { | 68 select { |
| 66 case <-ctx.Done(): | 69 case <-ctx.Done(): |
| 67 return ctx.Err() | 70 return ctx.Err() |
| 68 | 71 |
| 69 case <-clock.After(ctx, delay): | 72 case <-clock.After(ctx, delay): |
| 70 break | 73 break |
| 71 } | 74 } |
| 72 } | 75 } |
| 73 } | 76 } |
| 74 } | 77 } |
| OLD | NEW |