-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
176 lines (163 loc) · 5.55 KB
/
example_test.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package ratelimiter
import (
"context"
"fmt"
"time"
)
func runExample(limiter *RateLimiter, key string) {
// every 10 min , burst 5
durationPerToken := 10 * time.Minute
burst := 5
now := time.Now()
ctx := context.Background()
try := func(delta time.Duration) bool {
reserveReq := &ReserveRequest{
Key: key,
DurationPerToken: durationPerToken,
Burst: burst,
Tokens: 1,
MaxFutureReserve: 0,
}
advancedNow := now.Add(delta)
r, err := limiter.Reserve(
// only for test, you should not use this in production !!
WithNowFuncForTest(ctx, func() time.Time {
return advancedNow
}),
reserveReq,
)
if err != nil {
panic(err)
}
if r.OK {
fmt.Printf("%v: allowed: %t\n", delta, r.OK)
return true
}
fmt.Printf("%v: allowed: %t , you can retry after %v\n", delta, false, r.RetryAfterFrom(advancedNow))
return false
}
for i := 0; i < int(25); i++ {
delta := time.Duration(i) * time.Minute
try(delta)
}
fmt.Printf("--- Sleep 20 minutes ---\n")
for i := 45; i < int(55); i++ {
delta := time.Duration(i) * time.Minute
try(delta)
}
fmt.Printf("--- Sleep 100 minutes ---\n")
for i := 155; i < int(165); i++ {
delta := time.Duration(i) * time.Minute
try(delta)
}
}
func ExampleNewGormDriver() {
limiter := New(
NewGormDriver(db),
)
runExample(limiter, "ExampleNewGormDriver")
// Output:
// 0s: allowed: true
// 1m0s: allowed: true
// 2m0s: allowed: true
// 3m0s: allowed: true
// 4m0s: allowed: true
// 5m0s: allowed: false , you can retry after 5m0s
// 6m0s: allowed: false , you can retry after 4m0s
// 7m0s: allowed: false , you can retry after 3m0s
// 8m0s: allowed: false , you can retry after 2m0s
// 9m0s: allowed: false , you can retry after 1m0s
// 10m0s: allowed: true
// 11m0s: allowed: false , you can retry after 9m0s
// 12m0s: allowed: false , you can retry after 8m0s
// 13m0s: allowed: false , you can retry after 7m0s
// 14m0s: allowed: false , you can retry after 6m0s
// 15m0s: allowed: false , you can retry after 5m0s
// 16m0s: allowed: false , you can retry after 4m0s
// 17m0s: allowed: false , you can retry after 3m0s
// 18m0s: allowed: false , you can retry after 2m0s
// 19m0s: allowed: false , you can retry after 1m0s
// 20m0s: allowed: true
// 21m0s: allowed: false , you can retry after 9m0s
// 22m0s: allowed: false , you can retry after 8m0s
// 23m0s: allowed: false , you can retry after 7m0s
// 24m0s: allowed: false , you can retry after 6m0s
// --- Sleep 20 minutes ---
// 45m0s: allowed: true
// 46m0s: allowed: true
// 47m0s: allowed: false , you can retry after 3m0s
// 48m0s: allowed: false , you can retry after 2m0s
// 49m0s: allowed: false , you can retry after 1m0s
// 50m0s: allowed: true
// 51m0s: allowed: false , you can retry after 9m0s
// 52m0s: allowed: false , you can retry after 8m0s
// 53m0s: allowed: false , you can retry after 7m0s
// 54m0s: allowed: false , you can retry after 6m0s
// --- Sleep 100 minutes ---
// 2h35m0s: allowed: true
// 2h36m0s: allowed: true
// 2h37m0s: allowed: true
// 2h38m0s: allowed: true
// 2h39m0s: allowed: true
// 2h40m0s: allowed: false , you can retry after 5m0s
// 2h41m0s: allowed: false , you can retry after 4m0s
// 2h42m0s: allowed: false , you can retry after 3m0s
// 2h43m0s: allowed: false , you can retry after 2m0s
// 2h44m0s: allowed: false , you can retry after 1m0s
}
func ExampleInitRedisDriver() {
d, err := InitRedisDriver(context.Background(), redisCli)
if err != nil {
panic(err)
}
limiter := New(d)
runExample(limiter, "ExampleInitRedisDriver")
// Output:
// 0s: allowed: true
// 1m0s: allowed: true
// 2m0s: allowed: true
// 3m0s: allowed: true
// 4m0s: allowed: true
// 5m0s: allowed: false , you can retry after 5m0s
// 6m0s: allowed: false , you can retry after 4m0s
// 7m0s: allowed: false , you can retry after 3m0s
// 8m0s: allowed: false , you can retry after 2m0s
// 9m0s: allowed: false , you can retry after 1m0s
// 10m0s: allowed: true
// 11m0s: allowed: false , you can retry after 9m0s
// 12m0s: allowed: false , you can retry after 8m0s
// 13m0s: allowed: false , you can retry after 7m0s
// 14m0s: allowed: false , you can retry after 6m0s
// 15m0s: allowed: false , you can retry after 5m0s
// 16m0s: allowed: false , you can retry after 4m0s
// 17m0s: allowed: false , you can retry after 3m0s
// 18m0s: allowed: false , you can retry after 2m0s
// 19m0s: allowed: false , you can retry after 1m0s
// 20m0s: allowed: true
// 21m0s: allowed: false , you can retry after 9m0s
// 22m0s: allowed: false , you can retry after 8m0s
// 23m0s: allowed: false , you can retry after 7m0s
// 24m0s: allowed: false , you can retry after 6m0s
// --- Sleep 20 minutes ---
// 45m0s: allowed: true
// 46m0s: allowed: true
// 47m0s: allowed: false , you can retry after 3m0s
// 48m0s: allowed: false , you can retry after 2m0s
// 49m0s: allowed: false , you can retry after 1m0s
// 50m0s: allowed: true
// 51m0s: allowed: false , you can retry after 9m0s
// 52m0s: allowed: false , you can retry after 8m0s
// 53m0s: allowed: false , you can retry after 7m0s
// 54m0s: allowed: false , you can retry after 6m0s
// --- Sleep 100 minutes ---
// 2h35m0s: allowed: true
// 2h36m0s: allowed: true
// 2h37m0s: allowed: true
// 2h38m0s: allowed: true
// 2h39m0s: allowed: true
// 2h40m0s: allowed: false , you can retry after 5m0s
// 2h41m0s: allowed: false , you can retry after 4m0s
// 2h42m0s: allowed: false , you can retry after 3m0s
// 2h43m0s: allowed: false , you can retry after 2m0s
// 2h44m0s: allowed: false , you can retry after 1m0s
}