@@ -15,8 +15,8 @@ func TestRedisCounter(t *testing.T) {
15
15
limitCounter , err := httprateredis .NewRedisLimitCounter (& httprateredis.Config {
16
16
Host : "localhost" ,
17
17
Port : 6379 ,
18
- MaxIdle : 100 ,
19
- MaxActive : 200 ,
18
+ MaxIdle : 0 ,
19
+ MaxActive : 2 ,
20
20
DBIndex : 0 ,
21
21
ClientName : "httprateredis_test" ,
22
22
PrefixKey : fmt .Sprintf ("httprate:test:%v" , rand .Int31n (100000 )), // Unique Redis key for each test
@@ -162,7 +162,10 @@ func BenchmarkLocalCounter(b *testing.B) {
162
162
DBIndex : 0 ,
163
163
ClientName : "httprateredis_test" ,
164
164
PrefixKey : fmt .Sprintf ("httprate:test:%v" , rand .Int31n (100000 )), // Unique key for each test
165
+ MaxActive : 10 ,
166
+ MaxIdle : 0 ,
165
167
FallbackDisabled : true ,
168
+ FallbackTimeout : 5 * time .Second ,
166
169
})
167
170
if err != nil {
168
171
b .Fatalf ("redis not available: %v" , err )
@@ -174,6 +177,8 @@ func BenchmarkLocalCounter(b *testing.B) {
174
177
currentWindow := time .Now ().UTC ().Truncate (time .Minute )
175
178
previousWindow := currentWindow .Add (- time .Minute )
176
179
180
+ concurrentRequests := 100
181
+
177
182
b .ResetTimer ()
178
183
179
184
for i := 0 ; i < b .N ; i ++ {
@@ -183,14 +188,14 @@ func BenchmarkLocalCounter(b *testing.B) {
183
188
previousWindow .Add (time .Duration (i ) * time .Minute )
184
189
185
190
wg := sync.WaitGroup {}
186
- wg .Add (1000 )
187
- for i := 0 ; i < 1000 ; i ++ {
191
+ wg .Add (concurrentRequests )
192
+ for i := 0 ; i < concurrentRequests ; i ++ {
188
193
// Simulate concurrent requests with different rate-limit keys.
189
194
go func (i int ) {
190
195
defer wg .Done ()
191
196
192
197
_ , _ , _ = limitCounter .Get (fmt .Sprintf ("key:%v" , i ), currentWindow , previousWindow )
193
- _ = limitCounter .IncrementBy (fmt .Sprintf ("key:%v" , i ), currentWindow , rand .Intn (100 ))
198
+ _ = limitCounter .IncrementBy (fmt .Sprintf ("key:%v" , i ), currentWindow , rand .Intn (20 ))
194
199
}(i )
195
200
}
196
201
wg .Wait ()
0 commit comments