/
interface_with_ratelimit.go
executable file
·84 lines (65 loc) · 1.94 KB
/
interface_with_ratelimit.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
73
74
75
76
77
78
79
80
81
82
83
84
// Code generated by gowrap. DO NOT EDIT.
// template: ../templates/ratelimit
// gowrap: http://github.com/hexdigest/gowrap
package templatestests
//go:generate gowrap gen -p github.com/hexdigest/gowrap/templates_tests -i TestInterface -t ../templates/ratelimit -o interface_with_ratelimit.go -l ""
import (
"context"
"time"
)
// TestInterfaceWithRateLimit implements TestInterface
type TestInterfaceWithRateLimit struct {
_base TestInterface
_ticks chan time.Time
}
// NewTestInterfaceWithRateLimit instruments an implementation of the TestInterface with rate limiting
func NewTestInterfaceWithRateLimit(base TestInterface, burst int, rps float64) *TestInterfaceWithRateLimit {
d := &TestInterfaceWithRateLimit{
_base: base,
_ticks: make(chan time.Time, burst),
}
now := time.Now()
for i := 0; i < burst; i++ {
d._ticks <- now
}
delay := time.Duration(float64(time.Second) / rps)
go func() {
for t := range time.Tick(delay) {
d._ticks <- t
}
}()
return d
}
// Channels implements TestInterface
func (_d *TestInterfaceWithRateLimit) Channels(chA chan bool, chB chan<- bool, chanC <-chan bool) {
<-_d._ticks
_d._base.Channels(chA, chB, chanC)
return
}
// ContextNoError implements TestInterface
func (_d *TestInterfaceWithRateLimit) ContextNoError(ctx context.Context, a1 string, a2 string) {
<-_d._ticks
_d._base.ContextNoError(ctx, a1, a2)
return
}
// F implements TestInterface
func (_d *TestInterfaceWithRateLimit) F(ctx context.Context, a1 string, a2 ...string) (result1 string, result2 string, err error) {
select {
case <-ctx.Done():
err = ctx.Err()
return
case <-_d._ticks:
}
return _d._base.F(ctx, a1, a2...)
}
// NoError implements TestInterface
func (_d *TestInterfaceWithRateLimit) NoError(s1 string) (s2 string) {
<-_d._ticks
return _d._base.NoError(s1)
}
// NoParamsOrResults implements TestInterface
func (_d *TestInterfaceWithRateLimit) NoParamsOrResults() {
<-_d._ticks
_d._base.NoParamsOrResults()
return
}