Skip to content

Commit

Permalink
feat: various improvements to retry logic in event dispatcher
Browse files Browse the repository at this point in the history
- Create the request outside the loop
- Cap the backoff timeout at 3x timeout
- Print the retry attempt and backoff time in the log
- Add a jitter to the backoff time
  • Loading branch information
obycode committed Oct 17, 2024
1 parent 2207ac4 commit 1c595ad
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions testnet/stacks-node/src/event_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use clarity::vm::analysis::contract_interface_builder::build_contract_interface;
use clarity::vm::costs::ExecutionCost;
use clarity::vm::events::{FTEventType, NFTEventType, STXEventType};
use clarity::vm::types::{AssetIdentifier, QualifiedContractIdentifier, Value};
use rand::Rng;
use rusqlite::{params, Connection};
use serde_json::json;
use stacks::burnchains::{PoxConstants, Txid};
Expand Down Expand Up @@ -429,17 +430,19 @@ impl EventObserver {
.unwrap_or(PeerHost::DNS(host.to_string(), port));

let mut backoff = Duration::from_millis(100);
let mut attempts: i32 = 0;
// Cap the backoff at 3x the timeout
let max_backoff = timeout.saturating_mul(3);
loop {
let mut request = StacksHttpRequest::new_for_peer(
peerhost.clone(),
"POST".into(),
url.path().into(),
HttpRequestContents::new().payload_json(payload.clone()),
)
.unwrap_or_else(|_| panic!("FATAL: failed to encode infallible data as HTTP request"));
request.add_header("Connection".into(), "close".into());

let mut request = StacksHttpRequest::new_for_peer(
peerhost.clone(),
"POST".into(),
url.path().into(),
HttpRequestContents::new().payload_json(payload.clone()),
)
.unwrap_or_else(|_| panic!("FATAL: failed to encode infallible data as HTTP request"));
request.add_header("Connection".into(), "close".into());
loop {
match send_http_request(host, port, request, timeout) {
Ok(response) => {
if response.preamble().status_code == 200 {
Expand All @@ -457,7 +460,8 @@ impl EventObserver {
warn!(
"Event dispatcher: connection or request failed to {}:{} - {:?}",
&host, &port, err;
"backoff" => backoff
"backoff" => backoff,
"attempts" => attempts
);
}
}
Expand All @@ -473,7 +477,12 @@ impl EventObserver {
}

sleep(backoff);
backoff = std::cmp::min(backoff.saturating_mul(2), max_backoff);
let jitter: u64 = rand::thread_rng().gen_range(0..100);
backoff = std::cmp::min(
backoff.saturating_mul(2) + Duration::from_millis(jitter),
max_backoff,
);
attempts = attempts.saturating_add(1);
}
}

Expand Down

0 comments on commit 1c595ad

Please sign in to comment.