pax_global_header00006660000000000000000000000064134667430130014521gustar00rootroot0000000000000052 comment=cd7d3b308163f575bf3e14623edd50ef2b90b877 retry-1.0.3/000077500000000000000000000000001346674301300126675ustar00rootroot00000000000000retry-1.0.3/.travis.yml000066400000000000000000000001201346674301300147710ustar00rootroot00000000000000language: go go_import_path: gopkg.in/retry.v1 go: - 1.8.x - 1.x - master retry-1.0.3/LICENSE000066400000000000000000000211221346674301300136720ustar00rootroot00000000000000Copyright (C) 2011-2015 Canonical Ltd. This software is licensed under the LGPLv3, included below. As a special exception to the GNU Lesser General Public License version 3 ("LGPL3"), the copyright holders of this Library give you permission to convey to a third party a Combined Work that links statically or dynamically to this Library without providing any Minimal Corresponding Source or Minimal Application Code as set out in 4d or providing the installation information set out in section 4e, provided that you comply with the other provisions of LGPL3 and provided that you meet, for the Application the terms and conditions of the license(s) which apply to the Application. Except as stated in this special exception, the provisions of LGPL3 will continue to comply in full to this Library. If you modify this Library, you may apply this exception to your version of this Library, but you are not obliged to do so. If you do not wish to do so, delete this exception statement from your version. This exception does not (and cannot) modify any license terms which apply to the Application, with which you must still comply. GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. retry-1.0.3/README.md000066400000000000000000000117751346674301300141610ustar00rootroot00000000000000# retry -- import "gopkg.in/retry.v1" Package retry provides a framework for retrying actions. It does not itself invoke the action to be retried, but is intended to be used in a retry loop. The basic usage is as follows: for a := someStrategy.Start(); a.Next(); { try() } See examples for details of suggested usage. ## Usage #### type Attempt ```go type Attempt struct { } ``` Attempt represents a running retry attempt. #### func Start ```go func Start(strategy Strategy, clk Clock) *Attempt ``` Start begins a new sequence of attempts for the given strategy using the given Clock implementation for time keeping. If clk is nil, the time package will be used to keep time. #### func StartWithCancel ```go func StartWithCancel(strategy Strategy, clk Clock, stop <-chan struct{}) *Attempt ``` StartWithCancel is like Start except that if a value is received on stop while waiting, the attempt will be aborted. #### func (*Attempt) Count ```go func (a *Attempt) Count() int ``` Count returns the current attempt count number, starting at 1. It returns 0 if called before Next is called. When the loop has terminated, it holds the total number of retries made. #### func (*Attempt) More ```go func (a *Attempt) More() bool ``` More reports whether there are more retry attempts to be made. It does not sleep. If More returns false, Next will return false. If More returns true, Next will return true except when the attempt has been explicitly stopped via the stop channel. #### func (*Attempt) Next ```go func (a *Attempt) Next() bool ``` Next reports whether another attempt should be made, waiting as necessary until it's time for the attempt. It always returns true the first time it is called unless a value is received on the stop channel - we are guaranteed to make at least one attempt unless stopped. #### func (*Attempt) Stopped ```go func (a *Attempt) Stopped() bool ``` Stopped reports whether the attempt has terminated because a value was received on the stop channel. #### type Clock ```go type Clock interface { Now() time.Time After(time.Duration) <-chan time.Time } ``` Clock represents a virtual clock interface that can be replaced for testing. #### type Exponential ```go type Exponential struct { // Initial holds the initial delay. Initial time.Duration // Factor holds the factor that the delay time will be multiplied // by on each iteration. Factor float64 // MaxDelay holds the maximum delay between the start // of attempts. If this is zero, there is no maximum delay. MaxDelay time.Duration } ``` Exponential represents an exponential backoff retry strategy. To limit the number of attempts or their overall duration, wrap this in LimitCount or LimitDuration. #### func (Exponential) NewTimer ```go func (r Exponential) NewTimer(now time.Time) Timer ``` NewTimer implements Strategy.NewTimer. #### type Regular ```go type Regular struct { // Total specifies the total duration of the attempt. Total time.Duration // Delay specifies the interval between the start of each try // in the burst. If an try takes longer than Delay, the // next try will happen immediately. Delay time.Duration // Min holds the minimum number of retries. It overrides Total. // To limit the maximum number of retries, use LimitCount. Min int } ``` Regular represents a strategy that repeats at regular intervals. #### func (Regular) NewTimer ```go func (r Regular) NewTimer(now time.Time) Timer ``` NewTimer implements Strategy.NewTimer. #### func (Regular) Start ```go func (r Regular) Start(clk Clock) *Attempt ``` Start is short for Start(r, clk, nil) #### type Strategy ```go type Strategy interface { // NewTimer is called when the strategy is started - it is // called with the time that the strategy is started and returns // an object that is used to find out how long to sleep before // each retry attempt. NewTimer(now time.Time) Timer } ``` Strategy is implemented by types that represent a retry strategy. Note: You probably won't need to implement a new strategy - the existing types and functions are intended to be sufficient for most purposes. #### func LimitCount ```go func LimitCount(n int, strategy Strategy) Strategy ``` LimitCount limits the number of attempts that the given strategy will perform to n. Note that all strategies will allow at least one attempt. #### func LimitTime ```go func LimitTime(limit time.Duration, strategy Strategy) Strategy ``` LimitTime limits the given strategy such that no attempt will made after the given duration has elapsed. #### type Timer ```go type Timer interface { // NextSleep is called with the time that Next or More has been // called and returns the length of time to sleep before the // next retry. If no more attempts should be made it should // return false, and the returned duration will be ignored. // // Note that NextSleep is called once after each iteration has // completed, assuming the retry loop is continuing. NextSleep(now time.Time) (time.Duration, bool) } ``` Timer represents a source of timing events for a retry strategy. retry-1.0.3/clock.go000066400000000000000000000010021346674301300143020ustar00rootroot00000000000000package retry // import "gopkg.in/retry.v1" import "time" // Clock represents a virtual clock interface that // can be replaced for testing. type Clock interface { Now() time.Time After(time.Duration) <-chan time.Time } // WallClock exposes wall-clock time as returned by time.Now. type wallClock struct{} // Now implements Clock.Now. func (wallClock) Now() time.Time { return time.Now() } // After implements Clock.After. func (wallClock) After(d time.Duration) <-chan time.Time { return time.After(d) } retry-1.0.3/example_test.go000066400000000000000000000023651346674301300157160ustar00rootroot00000000000000package retry_test // import "gopkg.in/retry.v1" import ( "net/http" "time" "gopkg.in/retry.v1" ) func doSomething() (int, error) { return 0, nil } func shouldRetry(error) bool { return false } func doSomethingWith(int) {} func ExampleAttempt_More() { // This example shows how Attempt.More can be used to help // structure an attempt loop. If the godoc example code allowed // us to make the example return an error, we would uncomment // the commented return statements. attempts := retry.Regular{ Total: 1 * time.Second, Delay: 250 * time.Millisecond, } for attempt := attempts.Start(nil); attempt.Next(); { x, err := doSomething() if shouldRetry(err) && attempt.More() { continue } if err != nil { // return err return } doSomethingWith(x) } // return ErrTimedOut return } func ExampleExponential() { // This example shows a retry loop that will retry an // HTTP POST request with an exponential backoff // for up to 30s. strategy := retry.LimitTime(30*time.Second, retry.Exponential{ Initial: 10 * time.Millisecond, Factor: 1.5, }, ) for a := retry.Start(strategy, nil); a.Next(); { if reply, err := http.Post("http://example.com/form", "", nil); err == nil { reply.Body.Close() break } } } retry-1.0.3/exp.go000066400000000000000000000042101346674301300140070ustar00rootroot00000000000000package retry // import "gopkg.in/retry.v1" import ( "math/rand" "sync" "time" ) var ( // randomMu guards random. randomMu sync.Mutex // random is used as a random number source for jitter. // We avoid using the global math/rand source // as we don't want to be responsible for seeding it, // and its lock may be more contended. random = rand.New(rand.NewSource(time.Now().UnixNano())) ) // Exponential represents an exponential backoff retry strategy. // To limit the number of attempts or their overall duration, wrap // this in LimitCount or LimitDuration. type Exponential struct { // Initial holds the initial delay. Initial time.Duration // Factor holds the factor that the delay time will be multiplied // by on each iteration. If this is zero, a factor of two will be used. Factor float64 // MaxDelay holds the maximum delay between the start // of attempts. If this is zero, there is no maximum delay. MaxDelay time.Duration // Jitter specifies whether jitter should be added to the // retry interval. The algorithm used is described as "Full Jitter" // in https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ Jitter bool } type exponentialTimer struct { strategy Exponential start time.Time end time.Time delay time.Duration } // NewTimer implements Strategy.NewTimer. func (r Exponential) NewTimer(now time.Time) Timer { if r.Factor <= 0 { r.Factor = 2 } return &exponentialTimer{ strategy: r, start: now, delay: r.Initial, } } // NextSleep implements Timer.NextSleep. func (a *exponentialTimer) NextSleep(now time.Time) (time.Duration, bool) { sleep := a.delay - now.Sub(a.start) if sleep <= 0 { sleep = 0 } if a.strategy.Jitter { sleep = randDuration(sleep) } // Set the start of the next try. a.start = now.Add(sleep) a.delay = time.Duration(float64(a.delay) * a.strategy.Factor) if a.strategy.MaxDelay > 0 && a.delay > a.strategy.MaxDelay { a.delay = a.strategy.MaxDelay } return sleep, true } func randDuration(max time.Duration) time.Duration { if max <= 0 { return 0 } randomMu.Lock() defer randomMu.Unlock() return time.Duration(random.Int63n(int64(max))) } retry-1.0.3/go.mod000066400000000000000000000002231346674301300137720ustar00rootroot00000000000000module gopkg.in/retry.v1 require ( github.com/frankban/quicktest v1.2.2 github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a ) go 1.12 retry-1.0.3/go.sum000066400000000000000000000017771346674301300140360ustar00rootroot00000000000000github.com/frankban/quicktest v1.2.2 h1:xfmOhhoH5fGPgbEAlhLpJH9p0z/0Qizio9osmvn9IUY= github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20= github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42 h1:q3pnF5JFBNRz8sRD+IRj7Y6DMyYGTNqnZ9axTbSfoNI= github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a h1:3QH7VyOaaiUHNrA9Se4YQIRkDTCw1EJls9xTUCaCeRM= github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a/go.mod h1:4r5QyqhjIWCcK8DO4KMclc5Iknq5qVBAlbYYzAbUScQ= retry-1.0.3/regular.go000066400000000000000000000030731346674301300146620ustar00rootroot00000000000000// Copyright 2016 Canonical Ltd. // Licensed under the LGPLv3, see LICENCE file for details. package retry // import "gopkg.in/retry.v1" import ( "time" ) // Regular represents a strategy that repeats at regular intervals. type Regular struct { // Total specifies the total duration of the attempt. Total time.Duration // Delay specifies the interval between the start of each try // in the burst. If an try takes longer than Delay, the // next try will happen immediately. Delay time.Duration // Min holds the minimum number of retries. It overrides Total. // To limit the maximum number of retries, use LimitCount. Min int } // regularTimer holds a running instantiation of the Regular timer. type regularTimer struct { strategy Regular count int // start holds when the current try started. start time.Time end time.Time } // Start is short for Start(r, clk, nil) func (r Regular) Start(clk Clock) *Attempt { return Start(r, clk) } // NewTimer implements Strategy.NewTimer. func (r Regular) NewTimer(now time.Time) Timer { return ®ularTimer{ strategy: r, start: now, end: now.Add(r.Total), } } // NextSleep implements Timer.NextSleep. func (a *regularTimer) NextSleep(now time.Time) (time.Duration, bool) { sleep := a.strategy.Delay - now.Sub(a.start) if sleep <= 0 { sleep = 0 } a.count++ // Set the start of the next try. a.start = now.Add(sleep) if a.count < a.strategy.Min { return sleep, true } // The next try is not before the end - no more attempts. if !a.start.Before(a.end) { return 0, false } return sleep, true } retry-1.0.3/retry.go000066400000000000000000000106211346674301300143630ustar00rootroot00000000000000// Copyright 2016 Canonical Ltd. // Licensed under the LGPLv3, see LICENCE file for details. // Package retry implements flexible retry loops, including support for // channel cancellation, mocked time, and composable retry strategies // including exponential backoff with jitter. // // The basic usage is as follows: // // for a := retry.Start(someStrategy, nil); a.Next(); { // try() // } // // See examples for details of suggested usage. package retry // import "gopkg.in/retry.v1" import ( "time" ) // Strategy is implemented by types that represent a retry strategy. // // Note: You probably won't need to implement a new strategy - the existing types // and functions are intended to be sufficient for most purposes. type Strategy interface { // NewTimer is called when the strategy is started - it is // called with the time that the strategy is started and returns // an object that is used to find out how long to sleep before // each retry attempt. NewTimer(now time.Time) Timer } // Timer represents a source of timing events for a retry strategy. type Timer interface { // NextSleep is called with the time that Next or More has been // called and returns the length of time to sleep before the // next retry. If no more attempts should be made it should // return false, and the returned duration will be ignored. // // Note that NextSleep is called once after each iteration has // completed, assuming the retry loop is continuing. NextSleep(now time.Time) (time.Duration, bool) } // Attempt represents a running retry attempt. type Attempt struct { clock Clock stop <-chan struct{} timer Timer // next holds when the next attempt should start. // It is valid only when known is true. next time.Time // count holds the iteration count. count int // known holds whether next and running are known. known bool // running holds whether the attempt is still going. running bool // stopped holds whether the attempt has been stopped. stopped bool } // Start begins a new sequence of attempts for the given strategy using // the given Clock implementation for time keeping. If clk is // nil, the time package will be used to keep time. func Start(strategy Strategy, clk Clock) *Attempt { return StartWithCancel(strategy, clk, nil) } // StartWithCancel is like Start except that if a value // is received on stop while waiting, the attempt will be aborted. func StartWithCancel(strategy Strategy, clk Clock, stop <-chan struct{}) *Attempt { if clk == nil { clk = wallClock{} } now := clk.Now() return &Attempt{ clock: clk, stop: stop, timer: strategy.NewTimer(now), known: true, running: true, next: now, } } // Next reports whether another attempt should be made, waiting as // necessary until it's time for the attempt. It always returns true the // first time it is called unless a value is received on the stop // channel - we are guaranteed to make at least one attempt unless // stopped. func (a *Attempt) Next() bool { if !a.More() { return false } sleep := a.next.Sub(a.clock.Now()) if sleep <= 0 { // We're not going to sleep for any length of time, // so guarantee that we respect the stop channel. This // ensures that we make no attempts if Next is called // with a value available on the stop channel. select { case <-a.stop: a.stopped = true a.running = false return false default: a.known = false a.count++ return true } } select { case <-a.clock.After(sleep): a.known = false a.count++ case <-a.stop: a.running = false a.stopped = true } return a.running } // More reports whether there are more retry attempts to be made. It // does not sleep. // // If More returns false, Next will return false. If More returns true, // Next will return true except when the attempt has been explicitly // stopped via the stop channel. func (a *Attempt) More() bool { if !a.known { now := a.clock.Now() sleepDuration, running := a.timer.NextSleep(now) a.next, a.running, a.known = now.Add(sleepDuration), running, true } return a.running } // Stopped reports whether the attempt has terminated because // a value was received on the stop channel. func (a *Attempt) Stopped() bool { return a.stopped } // Count returns the current attempt count number, starting at 1. // It returns 0 if called before Next is called. // When the loop has terminated, it holds the total number // of retries made. func (a *Attempt) Count() int { return a.count } retry-1.0.3/retry_test.go000066400000000000000000000234521346674301300154300ustar00rootroot00000000000000// Copyright 2011, 2012, 2013 Canonical Ltd. // Licensed under the LGPLv3, see LICENCE file for details. package retry_test // import "gopkg.in/retry.v1" import ( "testing" "time" "github.com/rogpeppe/clock/testclock" qt "github.com/frankban/quicktest" "gopkg.in/retry.v1" ) func TestAttemptTiming(t *testing.T) { c := qt.New(t) testAttempt := retry.Regular{ Total: 0.25e9, Delay: 0.1e9, } want := []time.Duration{0, 0.1e9, 0.2e9, 0.2e9} got := make([]time.Duration, 0, len(want)) // avoid allocation when testing timing t0 := time.Now() a := testAttempt.Start(nil) for a.Next() { got = append(got, time.Now().Sub(t0)) } got = append(got, time.Now().Sub(t0)) c.Assert(a.Stopped(), qt.Equals, false) c.Assert(got, qt.HasLen, len(want)) const margin = 0.01e9 for i, got := range want { lo := want[i] - margin hi := want[i] + margin if got < lo || got > hi { c.Errorf("attempt %d want %g got %g", i, want[i].Seconds(), got.Seconds()) } } } func TestAttemptNextMore(t *testing.T) { c := qt.New(t) a := retry.Regular{}.Start(nil) c.Assert(a.Next(), qt.Equals, true) c.Assert(a.Next(), qt.Equals, false) a = retry.Regular{}.Start(nil) c.Assert(a.Next(), qt.Equals, true) c.Assert(a.More(), qt.Equals, false) c.Assert(a.Next(), qt.Equals, false) a = retry.Regular{Total: 2e8}.Start(nil) c.Assert(a.Next(), qt.Equals, true) c.Assert(a.More(), qt.Equals, true) time.Sleep(2e8) c.Assert(a.More(), qt.Equals, true) c.Assert(a.Next(), qt.Equals, true) c.Assert(a.Next(), qt.Equals, false) a = retry.Regular{Total: 1e8, Min: 2}.Start(nil) time.Sleep(1e8) c.Assert(a.Next(), qt.Equals, true) c.Assert(a.More(), qt.Equals, true) c.Assert(a.Next(), qt.Equals, true) c.Assert(a.More(), qt.Equals, false) c.Assert(a.Next(), qt.Equals, false) } func TestAttemptWithStop(t *testing.T) { c := qt.New(t) stop := make(chan struct{}) close(stop) done := make(chan struct{}) go func() { strategy := retry.Regular{ Delay: 5 * time.Second, Total: 30 * time.Second, } a := retry.StartWithCancel(strategy, nil, stop) for a.Next() { c.Errorf("unexpected attempt") } c.Check(a.Stopped(), qt.Equals, true) close(done) }() assertReceive(c, done, "attempt loop abort") } func TestAttemptWithLaterStop(t *testing.T) { c := qt.New(t) clock := testclock.NewClock(time.Now()) stop := make(chan struct{}) done := make(chan struct{}) progress := make(chan struct{}, 10) go func() { strategy := retry.Regular{ Delay: 5 * time.Second, Total: 30 * time.Second, } a := retry.StartWithCancel(strategy, clock, stop) for a.Next() { progress <- struct{}{} } c.Check(a.Stopped(), qt.Equals, true) close(done) }() assertReceive(c, progress, "progress") clock.Advance(5 * time.Second) assertReceive(c, progress, "progress") clock.Advance(2 * time.Second) close(stop) assertReceive(c, done, "attempt loop abort") select { case <-progress: c.Fatalf("unxpected loop iteration after stop") default: } } func TestAttemptWithMockClock(t *testing.T) { c := qt.New(t) clock := testclock.NewClock(time.Now()) strategy := retry.Regular{ Delay: 5 * time.Second, Total: 30 * time.Second, } progress := make(chan struct{}) done := make(chan struct{}) go func() { for a := strategy.Start(clock); a.Next(); { progress <- struct{}{} } close(done) }() assertReceive(c, progress, "progress first time") clock.Advance(5 * time.Second) assertReceive(c, progress, "progress second time") clock.Advance(5 * time.Second) assertReceive(c, progress, "progress third time") clock.Advance(30 * time.Second) assertReceive(c, progress, "progress fourth time") assertReceive(c, done, "loop finish") } type strategyTest struct { about string strategy retry.Strategy calls []nextCall terminates bool } type nextCall struct { // t holds the time since the timer was started that // the Next call will be made. t time.Duration // delay holds the length of time that a call made at // time t is expected to sleep for. sleep time.Duration } var strategyTests = []strategyTest{{ about: "regular retry (same params as TestAttemptTiming)", strategy: retry.Regular{ Total: 0.25e9, Delay: 0.1e9, }, calls: []nextCall{ {0, 0}, {0, 0.1e9}, {0.1e9, 0.1e9}, {0.2e9, 0}, }, terminates: true, }, { about: "regular retry with calls at different times", strategy: retry.Regular{ Total: 2.5e9, Delay: 1e9, }, calls: []nextCall{ {0.5e9, 0}, {0.5e9, 0.5e9}, {1.1e9, 0.9e9}, {2.2e9, 0}, }, terminates: true, }, { about: "regular retry with call after next deadline", strategy: retry.Regular{ Total: 3.5e9, Delay: 1e9, }, calls: []nextCall{ {0.5e9, 0}, // We call Next at well beyond the deadline, // so we get a zero delay, but subsequent events // resume pace. {2e9, 0}, {2.1e9, 0.9e9}, {3e9, 0}, }, terminates: true, }, { about: "exponential retry", strategy: retry.Exponential{ Initial: 1e9, Factor: 2, }, calls: []nextCall{ {0, 0}, {0.1e9, 0.9e9}, {1e9, 2e9}, {3e9, 4e9}, {7e9, 8e9}, }, }, { about: "exponential retry with zero factor defaults to 2", strategy: retry.Exponential{ Initial: 1e9, }, calls: []nextCall{ {0, 0}, {0.1e9, 0.9e9}, {1e9, 2e9}, {3e9, 4e9}, {7e9, 8e9}, }, }, { about: "time-limited exponential retry", strategy: retry.LimitTime(5e9, retry.Exponential{ Initial: 1e9, Factor: 2, }), calls: []nextCall{ {0, 0}, {0.1e9, 0.9e9}, {1e9, 2e9}, {3e9, 0}, }, terminates: true, }, { about: "count-limited exponential retry", strategy: retry.LimitCount(2, retry.Exponential{ Initial: 1e9, Factor: 2, }), calls: []nextCall{ {0, 0}, {0.1e9, 0.9e9}, {1e9, 0}, }, terminates: true, }} func TestStrategies(t *testing.T) { c := qt.New(t) for i, test := range strategyTests { c.Logf("test %d: %s", i, test.about) testStrategy(c, test) } } func testStrategy(c *qt.C, test strategyTest) { t0 := time.Now() clk := &mockClock{ now: t0, } a := retry.Start(test.strategy, clk) for i, call := range test.calls { c.Logf("call %d - %v", i, call.t) clk.now = t0.Add(call.t) ok := a.Next() expectTerminate := test.terminates && i == len(test.calls)-1 c.Assert(ok, qt.Equals, !expectTerminate) if got, want := clk.now.Sub(t0), call.t+call.sleep; !closeTo(got, want) { c.Fatalf("incorrect time after Next; got %v want %v", got, want) } if ok { c.Assert(a.Count(), qt.Equals, i+1) } } } func TestExponentialWithJitter(t *testing.T) { c := qt.New(t) // We use a stochastic test because we don't want // to mock rand and have detailed dependence on // the exact way it's used. We run the strategy many // times and note the delays that we found; if the // jitter is working, the delays should be roughly equally // distributed and it shouldn't take long before all the // buckets are hit. const numBuckets = 8 tries := []struct { max time.Duration buckets [numBuckets]int }{{ max: 1e9, }, { max: 2e9, }, { max: 4e9, }, { max: 5e9, }} strategy := retry.Exponential{ Initial: 1e9, Factor: 2, MaxDelay: 5e9, Jitter: true, } count := 0 for i := 0; i < 10000 && count < len(tries)*numBuckets; i++ { clk := &mockClock{ now: time.Now(), } t := clk.Now() a := retry.Start(strategy, clk) if !a.Next() { c.Fatalf("no first try") } if clk.Now().Sub(t) != 0 { c.Fatalf("first try was not immediate") } for try := 0; a.Next(); try++ { if try >= len(tries) { break } d := clk.Now().Sub(t) t = clk.Now() max := tries[try].max if d > max { c.Fatalf("try %d exceeded max %v; actual duration %v", try, tries[try].max, d) } slot := int(float64(d) / float64(max+1) * numBuckets) if slot >= numBuckets || slot < 0 { c.Fatalf("try %d slot %d out of range; d %v; max %v", try, slot, d, max) } buckets := &tries[try].buckets if buckets[slot] == 0 { count++ } buckets[slot]++ } } if count < len(tries)*numBuckets { c.Fatalf("distribution was not evenly spread; tries %#v", tries) } } func TestGapBetweenMoreAndNext(t *testing.T) { c := qt.New(t) t0 := time.Now().UTC() clk := &mockClock{ now: t0, } a := (&retry.Regular{ Min: 3, Delay: time.Second, }).Start(clk) c.Assert(a.Next(), qt.Equals, true) c.Assert(clk.now, qt.Equals, t0) clk.now = clk.now.Add(500 * time.Millisecond) // Sanity check that the first iteration sleeps for half a second. c.Assert(a.More(), qt.Equals, true) c.Assert(a.Next(), qt.Equals, true) c.Assert(clk.now.Sub(t0), qt.Equals, t0.Add(time.Second).Sub(t0)) clk.now = clk.now.Add(500 * time.Millisecond) c.Assert(a.More(), qt.Equals, true) // Add a delay between calling More and Next. // Next should wait until the correct time anyway. clk.now = clk.now.Add(250 * time.Millisecond) c.Assert(a.More(), qt.Equals, true) c.Assert(a.Next(), qt.Equals, true) c.Assert(clk.now.Sub(t0), qt.Equals, t0.Add(2*time.Second).Sub(t0)) } func TestOnlyOneHitOnZeroTotal(t *testing.T) { c := qt.New(t) t0 := time.Now().UTC() clk := &mockClock{ now: t0, } a := (&retry.Regular{ Total: 0, Delay: 0, Min: 0, }).Start(clk) // Even if the clock didn't advanced we want to have only one hit c.Check(a.Next(), qt.Equals, true) c.Check(a.More(), qt.Equals, false) } // closeTo reports whether d0 and d1 are close enough // to one another to cater for inaccuracies of floating point arithmetic. func closeTo(d0, d1 time.Duration) bool { const margin = 20 * time.Nanosecond diff := d1 - d0 if diff < 0 { diff = -diff } return diff < margin } type mockClock struct { retry.Clock now time.Time sleep func(d time.Duration) } func (c *mockClock) After(d time.Duration) <-chan time.Time { c.now = c.now.Add(d) ch := make(chan time.Time) close(ch) return ch } func (c *mockClock) Now() time.Time { return c.now } func assertReceive(c *qt.C, ch <-chan struct{}, what string) { select { case <-ch: case <-time.After(time.Second): c.Fatalf("timed out waiting for %s", what) } } retry-1.0.3/strategy.go000066400000000000000000000025221346674301300150610ustar00rootroot00000000000000package retry // import "gopkg.in/retry.v1" import ( "time" ) type strategyFunc func(now time.Time) Timer // NewTimer implements Strategy.NewTimer. func (f strategyFunc) NewTimer(now time.Time) Timer { return f(now) } // LimitCount limits the number of attempts that the given // strategy will perform to n. Note that all strategies // will allow at least one attempt. func LimitCount(n int, strategy Strategy) Strategy { return strategyFunc(func(now time.Time) Timer { return &countLimitTimer{ timer: strategy.NewTimer(now), remain: n, } }) } type countLimitTimer struct { timer Timer remain int } func (t *countLimitTimer) NextSleep(now time.Time) (time.Duration, bool) { if t.remain--; t.remain <= 0 { return 0, false } return t.timer.NextSleep(now) } // LimitTime limits the given strategy such that no attempt will // made after the given duration has elapsed. func LimitTime(limit time.Duration, strategy Strategy) Strategy { return strategyFunc(func(now time.Time) Timer { return &timeLimitTimer{ timer: strategy.NewTimer(now), end: now.Add(limit), } }) } type timeLimitTimer struct { timer Timer end time.Time } func (t *timeLimitTimer) NextSleep(now time.Time) (time.Duration, bool) { sleep, ok := t.timer.NextSleep(now) if ok && now.Add(sleep).After(t.end) { return 0, false } return sleep, ok }