Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request stacks-network#5042 from stacks-network/feat/timou…
Browse files Browse the repository at this point in the history
…t-connections

Timeout connections to observers
  • Loading branch information
obycode authored Aug 5, 2024
2 parents 1d61f35 + 19076c1 commit 3a48b09
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions testnet/stacks-node/src/event_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::thread::sleep;
use std::time::Duration;

use async_h1::client;
use async_std::future::timeout;
use async_std::net::TcpStream;
use async_std::task;
use clarity::vm::analysis::contract_interface_builder::build_contract_interface;
use clarity::vm::costs::ExecutionCost;
use clarity::vm::events::{FTEventType, NFTEventType, STXEventType};
Expand Down Expand Up @@ -318,27 +320,33 @@ impl EventObserver {
};

let backoff = Duration::from_millis((1.0 * 1_000.0) as u64);
let connection_timeout = Duration::from_secs(5);

loop {
let body = body.clone();
let mut req = Request::new(Method::Post, url.clone());
req.append_header("Content-Type", "application/json");
req.set_body(body);

let response = async_std::task::block_on(async {
let stream = match TcpStream::connect(self.endpoint.clone()).await {
Ok(stream) => stream,
Err(err) => {
warn!("Event dispatcher: connection failed - {:?}", err);
return None;
}
};
let response = task::block_on(async {
let stream =
match timeout(connection_timeout, TcpStream::connect(&self.endpoint)).await {
Ok(Ok(stream)) => stream,
Ok(Err(err)) => {
warn!("Event dispatcher: connection failed - {:?}", err);
return None;
}
Err(_) => {
error!("Event dispatcher: connection attempt timed out");
return None;
}
};

match client::connect(stream, req).await {
Ok(response) => Some(response),
Err(err) => {
warn!("Event dispatcher: rpc invocation failed - {:?}", err);
return None;
None
}
}
});
Expand Down

0 comments on commit 3a48b09

Please sign in to comment.