-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathtimeout
40 lines (35 loc) · 1.12 KB
/
timeout
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
import (
"context"
"time"
)
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithTimeout" .Interface.Name)) }}
// {{$decorator}} implements {{.Interface.Type}} interface instrumented with timeouts
type {{$decorator}} struct {
{{.Interface.Type}}
config {{$decorator}}Config
}
type {{$decorator}}Config struct {
{{range $method := .Interface.Methods}}
{{if $method.AcceptsContext}}{{$method.Name}}Timeout time.Duration{{ end }}
{{end}}
}
// New{{$decorator}} returns {{$decorator}}
func New{{$decorator}} (base {{.Interface.Type}}, config {{$decorator}}Config) {{$decorator}} {
return {{$decorator}} {
{{.Interface.Name}}: base,
config: config,
}
}
{{range $method := .Interface.Methods}}
{{if $method.AcceptsContext }}
// {{$method.Name}} implements {{$.Interface.Type}}
func (_d {{$decorator}}) {{$method.Declaration}} {
var cancelFunc func()
if _d.config.{{$method.Name}}Timeout > 0 {
ctx, cancelFunc = context.WithTimeout(ctx, _d.config.{{$method.Name}}Timeout)
defer cancelFunc()
}
{{$method.Pass (printf "_d.%s." $.Interface.Name) }}
}
{{end}}
{{end}}