Skip to content

Commit 88e70f9

Browse files
committed
put back ticker example
1 parent e5db35e commit 88e70f9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

example_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log"
78
"net/http"
89
"strconv"
910
)
@@ -50,3 +51,38 @@ func ExampleRetry() {
5051
fmt.Println(result)
5152
// Output: hello
5253
}
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+
}

0 commit comments

Comments
 (0)