-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
67 lines (56 loc) · 1.77 KB
/
Copy pathexample_test.go
File metadata and controls
67 lines (56 loc) · 1.77 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package stormbreak_test
import (
"context"
"errors"
"fmt"
"github.com/magnexis/stormbreak"
)
func ExampleDo() {
budget, _ := stormbreak.NewBudget(stormbreak.Config{Capacity: 1})
policy := stormbreak.Policy{MaxAttempts: 2, Multiplier: 1}
attempts := 0
value, err := stormbreak.Do(context.Background(), budget, policy, func(context.Context) (int, error) {
attempts++
if attempts == 1 {
return 0, errors.New("temporary failure")
}
return 42, nil
})
fmt.Println(value, err, attempts, budget.Remaining())
// Output: 42 <nil> 2 0
}
func ExamplePermanent() {
budget, _ := stormbreak.NewBudget(stormbreak.Config{Capacity: 5})
attempts := 0
err := stormbreak.DoVoid(context.Background(), budget, stormbreak.DefaultPolicy(), func(context.Context) error {
attempts++
return stormbreak.Permanent(errors.New("invalid credentials"))
})
fmt.Println(stormbreak.IsPermanent(err), attempts, budget.Remaining())
// Output: true 1 5
}
func ExampleRegistry() {
registry := stormbreak.NewRegistry()
_, _ = registry.Create("database", stormbreak.Config{Capacity: 10})
_, _ = registry.Create("github-api", stormbreak.Config{Capacity: 20})
fmt.Println(registry.Names())
// Output: [database github-api]
}
func ExampleWithHooks() {
budget, _ := stormbreak.NewBudget(stormbreak.Config{Capacity: 1})
policy := stormbreak.Policy{MaxAttempts: 2, Multiplier: 1}
attempts := 0
hooks := stormbreak.Hooks{
OnRetry: func(event stormbreak.RetryEvent) {
fmt.Printf("retry attempt=%d remaining=%d\n", event.Attempt, event.BudgetRemaining)
},
}
_ = stormbreak.DoVoid(context.Background(), budget, policy, func(context.Context) error {
attempts++
if attempts == 1 {
return errors.New("temporary")
}
return nil
}, stormbreak.WithHooks(hooks))
// Output: retry attempt=2 remaining=0
}