forked from kop/gowrap
/
interface_with_ratelimit.go
executable file
·68 lines (53 loc) · 1.6 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
package templatestests
// DO NOT EDIT!
// This code is generated with http://github.com/SergiiShapoval/gowrap tool
// using ../templates/ratelimit template
//go:generate gowrap gen -p github.com/SergiiShapoval/gowrap/templates_tests -i TestInterface -t ../templates/ratelimit -o interface_with_ratelimit.go
import (
"context"
"time"
)
// TestInterfaceWithRateLimit implements TestInterface
type TestInterfaceWithRateLimit struct {
_base TestInterface
_ticks chan time.Time
}
// NewTestInterfaceWithRateLimit instruments an implementation of the TestInterface with with rate limiting
func NewTestInterfaceWithRateLimit(base TestInterface, concurrentRequests int, rps float64) *TestInterfaceWithRateLimit {
d := &TestInterfaceWithRateLimit{
_base: base,
_ticks: make(chan time.Time, concurrentRequests),
}
now := time.Now()
for i := 0; i < concurrentRequests; i++ {
d._ticks <- now
}
delay := time.Duration(float64(time.Second) / rps)
go func() {
for t := range time.Tick(delay) {
d._ticks <- t
}
}()
return d
}
// 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
}