diff --git a/example_test.go b/example_test.go index 802f665..3793d2f 100644 --- a/example_test.go +++ b/example_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "log" "net/http" "strconv" ) @@ -50,3 +51,38 @@ func ExampleRetry() { fmt.Println(result) // Output: hello } + +func ExampleTicker() { + // An operation that may fail. + operation := func() (string, error) { + return "hello", nil + } + + ticker := NewTicker(NewExponentialBackOff()) + defer ticker.Stop() + + var result string + var err error + + // Ticks will continue to arrive when the previous operation is still running, + // so operations that take a while to fail could run in quick succession. + for range ticker.C { + if result, err = operation(); err != nil { + log.Println(err, "will retry...") + continue + } + + break + } + + if err != nil { + // Operation has failed. + fmt.Println("Error:", err) + return + } + + // Operation is successful. + + fmt.Println(result) + // Output: hello +}