@@ -26,6 +26,7 @@ use clarity::vm::analysis::contract_interface_builder::build_contract_interface;
26
26
use clarity:: vm:: costs:: ExecutionCost ;
27
27
use clarity:: vm:: events:: { FTEventType , NFTEventType , STXEventType } ;
28
28
use clarity:: vm:: types:: { AssetIdentifier , QualifiedContractIdentifier , Value } ;
29
+ use rand:: Rng ;
29
30
use rusqlite:: { params, Connection } ;
30
31
use serde_json:: json;
31
32
use stacks:: burnchains:: { PoxConstants , Txid } ;
@@ -429,17 +430,19 @@ impl EventObserver {
429
430
. unwrap_or ( PeerHost :: DNS ( host. to_string ( ) , port) ) ;
430
431
431
432
let mut backoff = Duration :: from_millis ( 100 ) ;
433
+ let mut attempts: i32 = 0 ;
434
+ // Cap the backoff at 3x the timeout
432
435
let max_backoff = timeout. saturating_mul ( 3 ) ;
433
- loop {
434
- let mut request = StacksHttpRequest :: new_for_peer (
435
- peerhost. clone ( ) ,
436
- "POST" . into ( ) ,
437
- url. path ( ) . into ( ) ,
438
- HttpRequestContents :: new ( ) . payload_json ( payload. clone ( ) ) ,
439
- )
440
- . unwrap_or_else ( |_| panic ! ( "FATAL: failed to encode infallible data as HTTP request" ) ) ;
441
- request. add_header ( "Connection" . into ( ) , "close" . into ( ) ) ;
442
436
437
+ let mut request = StacksHttpRequest :: new_for_peer (
438
+ peerhost. clone ( ) ,
439
+ "POST" . into ( ) ,
440
+ url. path ( ) . into ( ) ,
441
+ HttpRequestContents :: new ( ) . payload_json ( payload. clone ( ) ) ,
442
+ )
443
+ . unwrap_or_else ( |_| panic ! ( "FATAL: failed to encode infallible data as HTTP request" ) ) ;
444
+ request. add_header ( "Connection" . into ( ) , "close" . into ( ) ) ;
445
+ loop {
443
446
match send_http_request ( host, port, request, timeout) {
444
447
Ok ( response) => {
445
448
if response. preamble ( ) . status_code == 200 {
@@ -457,7 +460,8 @@ impl EventObserver {
457
460
warn ! (
458
461
"Event dispatcher: connection or request failed to {}:{} - {:?}" ,
459
462
& host, & port, err;
460
- "backoff" => backoff
463
+ "backoff" => backoff,
464
+ "attempts" => attempts
461
465
) ;
462
466
}
463
467
}
@@ -473,7 +477,12 @@ impl EventObserver {
473
477
}
474
478
475
479
sleep ( backoff) ;
476
- backoff = std:: cmp:: min ( backoff. saturating_mul ( 2 ) , max_backoff) ;
480
+ let jitter: u64 = rand:: thread_rng ( ) . gen_range ( 0 ..100 ) ;
481
+ backoff = std:: cmp:: min (
482
+ backoff. saturating_mul ( 2 ) + Duration :: from_millis ( jitter) ,
483
+ max_backoff,
484
+ ) ;
485
+ attempts = attempts. saturating_add ( 1 ) ;
477
486
}
478
487
}
479
488
0 commit comments