@@ -149,7 +149,7 @@ func TestServerDataRequest(t *testing.T) {
149
149
}
150
150
return false
151
151
}),
152
- WithServerRateLimit (10 , 10 , 10 ),
152
+ WithServerRateLimit (10 , 10 , 10 , 2 ),
153
153
withAmplificationAttackPreventionDialWait (0 ),
154
154
)
155
155
defer an .Close ()
@@ -187,6 +187,69 @@ func TestServerDataRequest(t *testing.T) {
187
187
_ , err = c .GetReachability (context .Background (), []Request {{Addr : quicAddr , SendDialData : true }, {Addr : tcpAddr }})
188
188
require .Error (t , err )
189
189
}
190
+
191
+ func TestServerMaxConcurrentRequestsPerPeer (t * testing.T ) {
192
+ const concurrentRequests = 5
193
+
194
+ // server will skip all tcp addresses
195
+ dialer := bhost .NewBlankHost (swarmt .GenSwarm (t , swarmt .OptDisableTCP ))
196
+
197
+ doneChan := make (chan struct {})
198
+ an := newAutoNAT (t , dialer , allowPrivateAddrs , withDataRequestPolicy (
199
+ // stall all allowed requests
200
+ func (s network.Stream , dialAddr ma.Multiaddr ) bool {
201
+ <- doneChan
202
+ return true
203
+ }),
204
+ WithServerRateLimit (10 , 10 , 10 , concurrentRequests ),
205
+ withAmplificationAttackPreventionDialWait (0 ),
206
+ )
207
+ defer an .Close ()
208
+ defer an .host .Close ()
209
+
210
+ c := newAutoNAT (t , nil , allowPrivateAddrs )
211
+ defer c .Close ()
212
+ defer c .host .Close ()
213
+
214
+ idAndWait (t , c , an )
215
+
216
+ errChan := make (chan error )
217
+ const N = 10
218
+ // num concurrentRequests will stall and N will fail
219
+ for i := 0 ; i < concurrentRequests + N ; i ++ {
220
+ go func () {
221
+ _ , err := c .GetReachability (context .Background (), []Request {{Addr : c .host .Addrs ()[0 ], SendDialData : false }})
222
+ errChan <- err
223
+ }()
224
+ }
225
+
226
+ // check N failures
227
+ for i := 0 ; i < N ; i ++ {
228
+ select {
229
+ case err := <- errChan :
230
+ require .Error (t , err )
231
+ case <- time .After (10 * time .Second ):
232
+ t .Fatalf ("expected %d errors: got: %d" , N , i )
233
+ }
234
+ }
235
+
236
+ // check concurrentRequests failures, as we won't send dial data
237
+ close (doneChan )
238
+ for i := 0 ; i < concurrentRequests ; i ++ {
239
+ select {
240
+ case err := <- errChan :
241
+ require .Error (t , err )
242
+ case <- time .After (5 * time .Second ):
243
+ t .Fatalf ("expected %d errors: got: %d" , concurrentRequests , i )
244
+ }
245
+ }
246
+ select {
247
+ case err := <- errChan :
248
+ t .Fatalf ("expected no more errors: got: %v" , err )
249
+ default :
250
+ }
251
+ }
252
+
190
253
func TestServerDataRequestJitter (t * testing.T ) {
191
254
// server will skip all tcp addresses
192
255
dialer := bhost .NewBlankHost (swarmt .GenSwarm (t , swarmt .OptDisableTCP ))
@@ -198,7 +261,7 @@ func TestServerDataRequestJitter(t *testing.T) {
198
261
}
199
262
return false
200
263
}),
201
- WithServerRateLimit (10 , 10 , 10 ),
264
+ WithServerRateLimit (10 , 10 , 10 , 2 ),
202
265
withAmplificationAttackPreventionDialWait (5 * time .Second ),
203
266
)
204
267
defer an .Close ()
@@ -238,7 +301,7 @@ func TestServerDataRequestJitter(t *testing.T) {
238
301
}
239
302
240
303
func TestServerDial (t * testing.T ) {
241
- an := newAutoNAT (t , nil , WithServerRateLimit (10 , 10 , 10 ), allowPrivateAddrs )
304
+ an := newAutoNAT (t , nil , WithServerRateLimit (10 , 10 , 10 , 2 ), allowPrivateAddrs )
242
305
defer an .Close ()
243
306
defer an .host .Close ()
244
307
@@ -295,7 +358,7 @@ func TestServerDial(t *testing.T) {
295
358
296
359
func TestRateLimiter (t * testing.T ) {
297
360
cl := test .NewMockClock ()
298
- r := rateLimiter {RPM : 3 , PerPeerRPM : 2 , DialDataRPM : 1 , now : cl .Now }
361
+ r := rateLimiter {RPM : 3 , PerPeerRPM : 2 , DialDataRPM : 1 , now : cl .Now , MaxConcurrentRequestsPerPeer : 1 }
299
362
300
363
require .True (t , r .Accept ("peer1" ))
301
364
@@ -333,12 +396,37 @@ func TestRateLimiter(t *testing.T) {
333
396
334
397
cl .AdvanceBy (10 * time .Second )
335
398
require .True (t , r .Accept ("peer3" ))
399
+
400
+ }
401
+
402
+ func TestRateLimiterConcurrentRequests (t * testing.T ) {
403
+ const N = 5
404
+ const Peers = 5
405
+ for concurrentRequests := 1 ; concurrentRequests <= N ; concurrentRequests ++ {
406
+ cl := test .NewMockClock ()
407
+ r := rateLimiter {RPM : 10 * Peers * N , PerPeerRPM : 10 * Peers * N , DialDataRPM : 10 * Peers * N , now : cl .Now , MaxConcurrentRequestsPerPeer : concurrentRequests }
408
+ for p := 0 ; p < Peers ; p ++ {
409
+ for i := 0 ; i < concurrentRequests ; i ++ {
410
+ require .True (t , r .Accept (peer .ID (fmt .Sprintf ("peer-%d" , p ))))
411
+ }
412
+ require .False (t , r .Accept (peer .ID (fmt .Sprintf ("peer-%d" , p ))))
413
+ // Now complete the requests
414
+ for i := 0 ; i < concurrentRequests ; i ++ {
415
+ r .CompleteRequest (peer .ID (fmt .Sprintf ("peer-%d" , p )))
416
+ }
417
+ // Now we should be able to accept new requests
418
+ for i := 0 ; i < concurrentRequests ; i ++ {
419
+ require .True (t , r .Accept (peer .ID (fmt .Sprintf ("peer-%d" , p ))))
420
+ }
421
+ require .False (t , r .Accept (peer .ID (fmt .Sprintf ("peer-%d" , p ))))
422
+ }
423
+ }
336
424
}
337
425
338
426
func TestRateLimiterStress (t * testing.T ) {
339
427
cl := test .NewMockClock ()
340
428
for i := 0 ; i < 10 ; i ++ {
341
- r := rateLimiter {RPM : 20 + i , PerPeerRPM : 10 + i , DialDataRPM : i , now : cl .Now }
429
+ r := rateLimiter {RPM : 20 + i , PerPeerRPM : 10 + i , DialDataRPM : i , MaxConcurrentRequestsPerPeer : 1 , now : cl .Now }
342
430
343
431
peers := make ([]peer.ID , 10 + i )
344
432
for i := 0 ; i < len (peers ); i ++ {
@@ -386,7 +474,7 @@ func TestRateLimiterStress(t *testing.T) {
386
474
require .Equal (t , len (r .peerReqs ), 1 )
387
475
require .Equal (t , len (r .peerReqs [peers [0 ]]), 1 )
388
476
require .Equal (t , len (r .dialDataReqs ), 0 )
389
- require .Equal (t , len (r .ongoingReqs ), 1 )
477
+ require .Equal (t , len (r .inProgressReqs ), 1 )
390
478
}
391
479
}
392
480
@@ -433,7 +521,7 @@ func TestReadDialData(t *testing.T) {
433
521
}
434
522
435
523
func FuzzServerDialRequest (f * testing.F ) {
436
- a := newAutoNAT (f , nil , allowPrivateAddrs , WithServerRateLimit (math .MaxInt32 , math .MaxInt32 , math .MaxInt32 ))
524
+ a := newAutoNAT (f , nil , allowPrivateAddrs , WithServerRateLimit (math .MaxInt32 , math .MaxInt32 , math .MaxInt32 , 2 ))
437
525
c := newAutoNAT (f , nil )
438
526
idAndWait (f , c , a )
439
527
// reduce the streamTimeout before running this. TODO: fix this
0 commit comments