-
Notifications
You must be signed in to change notification settings - Fork 25
/
extend.go
72 lines (59 loc) · 1.76 KB
/
extend.go
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
68
69
70
71
72
package main
import (
"context"
"fmt"
"log"
"github.com/slok/goresilience"
"github.com/slok/goresilience/retry"
)
// Config is the configuration of constFailer
type Config struct {
// FailEveryTimes will make the runner return an error every N executed times.
FailEveryTimes int
}
// NewFailer is like NewFailerMiddleware but will not wrap any other runner, is standalone.
func NewFailer(cfg Config) goresilience.Runner {
return NewFailerMiddleware(cfg)(nil)
}
// NewFailerMiddleware returns a new middleware that will wrap runners and will fail
// evey N times of executions.
func NewFailerMiddleware(cfg Config) goresilience.Middleware {
return func(next goresilience.Runner) goresilience.Runner {
calledTimes := 0
// Use the RunnerFunc helper so we don't need to create a new type.
return goresilience.RunnerFunc(func(ctx context.Context, f goresilience.Func) error {
// We should lock the counter writes, not made because this is an example.
calledTimes++
if calledTimes == cfg.FailEveryTimes {
calledTimes = 0
return fmt.Errorf("failed due to %d call", calledTimes)
}
// Run using the the chain.
next = goresilience.SanitizeRunner(next)
return next.Run(ctx, f)
})
}
}
func main() {
failerCfg := Config{
FailEveryTimes: 2,
}
// Use it standalone.
//runner := NewFailer(failerCfg)
// Or... create our execution chain.
retrier := retry.NewMiddleware(retry.Config{})
failer := NewFailerMiddleware(failerCfg)
runner := goresilience.RunnerChain(retrier, failer)
for i := 0; i < 200; i++ {
// Execute.
result := ""
err := runner.Run(context.TODO(), func(_ context.Context) error {
result = "all ok"
return nil
})
if err != nil {
result = "not ok, but fallback"
}
log.Printf("the result is: %s", result)
}
}