Skip to content

Commit

Permalink
add timeout for connecting to l1 provider
Browse files Browse the repository at this point in the history
  • Loading branch information
imabdulbasit committed Feb 4, 2025
1 parent 11619d0 commit 92d060c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
25 changes: 17 additions & 8 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,11 +332,13 @@ 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();

tracing::info!("0x03");
let span = tracing::warn_span!("L1 client update");
tracing::info!("0x04");
async move {
for i in 0.. {
let ws;
Expand All @@ -348,12 +351,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 @@ -1247,16 +1255,17 @@ mod test {
subscription_timeout: Duration::from_secs(1000),
l1_ws_provider: if ws {
Some(vec![
"ws://1.2.3.4:1234".parse().unwrap(),
"ws://notarealurl:1234".parse().unwrap(),
anvil.ws_endpoint().parse().unwrap(),
])
} else {
None
},

..Default::default()
}
.connect([
"http://1.2.3.4:1234".parse().unwrap(),
"http://notarealurl:1234".parse().unwrap(),
anvil.endpoint().parse().unwrap(),
]);

Expand Down Expand Up @@ -1306,7 +1315,7 @@ mod test {
..Default::default()
},
[
"http://1.2.3.4:1234".parse().unwrap(),
"http://notarealurl:1234".parse().unwrap(),
anvil.endpoint().parse().unwrap(),
],
));
Expand Down Expand Up @@ -1341,7 +1350,7 @@ mod test {
..Default::default()
},
[
"http://1.2.3.4:1234".parse().unwrap(),
"http://notarealurl:1234".parse().unwrap(),
anvil.endpoint().parse().unwrap(),
],
));
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 92d060c

Please sign in to comment.