forked from rogpeppe/retry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_simple_test.go
More file actions
50 lines (43 loc) · 917 Bytes
/
Copy pathexample_simple_test.go
File metadata and controls
50 lines (43 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package retry_test
import (
"fmt"
"log"
"math/rand"
"time"
"github.com/gopherine/retry"
)
type Foo struct{}
func Example_simple() {
log.SetFlags(log.Lmicroseconds)
_, err := getFooWithRetry()
if err != nil {
log.Printf("getFooWithRetry: %v", err)
} else {
log.Printf("getFooWithRetry: ok")
}
}
var retryStrategy = retry.Strategy{
Delay: 100 * time.Millisecond,
MaxDelay: 5 * time.Second,
MaxDuration: 10 * time.Second,
Factor: 2,
}
// getFooWithRetry demonstrates a retry loop.
func getFooWithRetry() (*Foo, error) {
for i := retryStrategy.Start(); ; {
log.Printf("getting foo")
foo, err := getFoo()
if err == nil {
return foo, nil
}
if !i.Next(nil) {
return nil, fmt.Errorf("error getting foo after %d tries: %v", i.Count(), err)
}
}
}
func getFoo() (*Foo, error) {
if rand.Intn(5000) == 0 {
return &Foo{}, nil
}
return nil, fmt.Errorf("some error")
}