Skip to content

Commit

Permalink
add timeout to provier::connect()
Browse files Browse the repository at this point in the history
  • Loading branch information
imabdulbasit committed Feb 4, 2025
1 parent c5788cc commit 028e7c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 11 additions & 4 deletions types/src/v0/impls/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ impl JsonRpcClient for MultiRpcClient {
};
let provider = current % self.clients.len();
let client = &self.clients[provider];

match client.request(method, &params).await {
Ok(res) => Ok(res),
Err(err) => {
Expand Down Expand Up @@ -331,10 +332,10 @@ impl L1Client {
let ws_urls = opt.l1_ws_provider.clone();
let retry_delay = opt.l1_retry_delay;
let subscription_timeout = opt.subscription_timeout;
let connect_timeout = opt.connect_timeout;
let state = self.state.clone();
let sender = self.sender.clone();
let metrics = self.metrics().clone();

let span = tracing::warn_span!("L1 client update");
async move {
for i in 0.. {
Expand All @@ -348,12 +349,17 @@ impl L1Client {
// problem with one of the hosts specifically.
let provider = i % urls.len();
let url = &urls[provider];
ws = match Provider::<Ws>::connect(url.clone()).await {
Ok(ws) => ws,
Err(err) => {
ws = match tokio::time::timeout(connect_timeout, Provider::<Ws>::connect(url.clone())).await {
Ok(Ok(ws)) => ws,
Ok(Err(err)) => {
tracing::warn!(provider, "failed to connect WebSockets provider: {err:#}");
sleep(retry_delay).await;
continue;
},
Err(_) => {
tracing::warn!(provider, "timed out connecting to WebSockets provider");
sleep(retry_delay).await;
continue;
}
};
ws.subscribe_blocks().await.map(StreamExt::boxed)
Expand Down Expand Up @@ -1253,6 +1259,7 @@ mod test {
} else {
None
},

..Default::default()
}
.connect([
Expand Down
9 changes: 9 additions & 0 deletions types/src/v0/v0_1/l1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ pub struct L1ClientOptions {
)]
pub l1_retry_delay: Duration,

/// Timeout for connecting to the L1 provider.
#[clap(
long,
env = "ESPRESSO_SEQUENCER_L1_CONNECT_TIMEOUT",
default_value = "2s",
value_parser = parse_duration,
)]
pub connect_timeout: Duration,

/// Request rate when polling L1.
#[clap(
long,
Expand Down

0 comments on commit 028e7c2

Please sign in to comment.