-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathretry
48 lines (44 loc) · 1.33 KB
/
retry
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
import(
"time"
)
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithRetry" .Interface.Name)) }}
// {{$decorator}} implements {{.Interface.Type}} interface instrumented with retries
type {{$decorator}} struct {
{{.Interface.Type}}
_retryCount int
_retryInterval time.Duration
}
// New{{$decorator}} returns {{$decorator}}
func New{{$decorator}} (base {{.Interface.Type}}, retryCount int, retryInterval time.Duration) {{$decorator}} {
return {{$decorator}} {
{{.Interface.Name}}: base,
_retryCount: retryCount,
_retryInterval: retryInterval,
}
}
{{range $method := .Interface.Methods}}
{{if $method.ReturnsError}}
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
{{$method.ResultsNames}} = _d.{{$.Interface.Name}}.{{$method.Call}}
if err == nil || _d._retryCount < 1 {
return
}
_ticker:= time.NewTicker(_d._retryInterval)
defer _ticker.Stop()
for _i := 0; _i < _d._retryCount && err != nil; _i++ {
{{- if $method.AcceptsContext}}
select {
case <-ctx.Done():
return
case <-_ticker.C:
}
{{else}}
<-_ticker.C
{{end -}}
{{$method.ResultsNames}} = _d.{{$.Interface.Name}}.{{$method.Call}}
}
return
}
{{end}}
{{end}}