File tree 1 file changed +36
-0
lines changed
1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ "log"
7
8
"net/http"
8
9
"strconv"
9
10
)
@@ -50,3 +51,38 @@ func ExampleRetry() {
50
51
fmt .Println (result )
51
52
// Output: hello
52
53
}
54
+
55
+ func ExampleTicker () {
56
+ // An operation that may fail.
57
+ operation := func () (string , error ) {
58
+ return "hello" , nil
59
+ }
60
+
61
+ ticker := NewTicker (NewExponentialBackOff ())
62
+ defer ticker .Stop ()
63
+
64
+ var result string
65
+ var err error
66
+
67
+ // Ticks will continue to arrive when the previous operation is still running,
68
+ // so operations that take a while to fail could run in quick succession.
69
+ for range ticker .C {
70
+ if result , err = operation (); err != nil {
71
+ log .Println (err , "will retry..." )
72
+ continue
73
+ }
74
+
75
+ break
76
+ }
77
+
78
+ if err != nil {
79
+ // Operation has failed.
80
+ fmt .Println ("Error:" , err )
81
+ return
82
+ }
83
+
84
+ // Operation is successful.
85
+
86
+ fmt .Println (result )
87
+ // Output: hello
88
+ }
You can’t perform that action at this time.
0 commit comments