Skip to content

Commit 3d1f829

Browse files
authored
Merge pull request #5327 from stacks-network/chore/event-dispatcher-logs
Improvements to event dispatcher retry logic
2 parents f245115 + 2b36268 commit 3d1f829

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

testnet/stacks-node/src/event_dispatcher.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use clarity::vm::analysis::contract_interface_builder::build_contract_interface;
2626
use clarity::vm::costs::ExecutionCost;
2727
use clarity::vm::events::{FTEventType, NFTEventType, STXEventType};
2828
use clarity::vm::types::{AssetIdentifier, QualifiedContractIdentifier, Value};
29+
use rand::Rng;
2930
use rusqlite::{params, Connection};
3031
use serde_json::json;
3132
use stacks::burnchains::{PoxConstants, Txid};
@@ -429,6 +430,10 @@ impl EventObserver {
429430
.unwrap_or(PeerHost::DNS(host.to_string(), port));
430431

431432
let mut backoff = Duration::from_millis(100);
433+
let mut attempts: i32 = 0;
434+
// Cap the backoff at 3x the timeout
435+
let max_backoff = timeout.saturating_mul(3);
436+
432437
loop {
433438
let mut request = StacksHttpRequest::new_for_peer(
434439
peerhost.clone(),
@@ -438,7 +443,6 @@ impl EventObserver {
438443
)
439444
.unwrap_or_else(|_| panic!("FATAL: failed to encode infallible data as HTTP request"));
440445
request.add_header("Connection".into(), "close".into());
441-
442446
match send_http_request(host, port, request, timeout) {
443447
Ok(response) => {
444448
if response.preamble().status_code == 200 {
@@ -455,7 +459,9 @@ impl EventObserver {
455459
Err(err) => {
456460
warn!(
457461
"Event dispatcher: connection or request failed to {}:{} - {:?}",
458-
&host, &port, err
462+
&host, &port, err;
463+
"backoff" => ?backoff,
464+
"attempts" => attempts
459465
);
460466
}
461467
}
@@ -471,7 +477,12 @@ impl EventObserver {
471477
}
472478

473479
sleep(backoff);
474-
backoff *= 2;
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);
475486
}
476487
}
477488

0 commit comments

Comments
 (0)