Skip to content

Commit 4562285

Browse files
committed
feat: custom lag error
1 parent 29fd442 commit 4562285

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pub enum ScannerError {
3333

3434
#[error("Subscription closed")]
3535
SubscriptionClosed,
36+
37+
#[error("Subscriptions Lagged too often")]
38+
SubscriptionLagged,
3639
}
3740

3841
impl From<RobustProviderError> for ScannerError {
@@ -42,6 +45,7 @@ impl From<RobustProviderError> for ScannerError {
4245
RobustProviderError::RpcError(err) => ScannerError::RpcError(err),
4346
RobustProviderError::BlockNotFound(block) => ScannerError::BlockNotFound(block),
4447
RobustProviderError::Closed => ScannerError::SubscriptionClosed,
48+
RobustProviderError::SubscriptionLagged => ScannerError::SubscriptionLagged,
4549
}
4650
}
4751
}

src/robust_provider/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::robust_provider::subscription::MAX_LAG_COUNT;
12
use std::sync::Arc;
23

34
use alloy::{
@@ -17,6 +18,8 @@ pub enum Error {
1718
BlockNotFound(BlockId),
1819
#[error("Subscription closed")]
1920
Closed,
21+
#[error("Subscription lag exceeded maximum consecutive lag count: {MAX_LAG_COUNT:?}")]
22+
SubscriptionLagged,
2023
}
2124

2225
impl From<RpcError<TransportErrorKind>> for Error {

src/robust_provider/subscription.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use alloy::{
88
network::Network,
99
providers::{Provider, RootProvider},
1010
pubsub::Subscription,
11-
transports::{RpcError, TransportErrorKind},
1211
};
1312
use tokio::{sync::broadcast::error::RecvError, time::timeout};
1413
use tokio_stream::Stream;
@@ -124,11 +123,7 @@ impl<N: Network> RobustSubscription<N> {
124123

125124
if self.consecutive_lags >= MAX_LAG_COUNT {
126125
warn!("Too many consecutive lags, switching provider");
127-
let error = RpcError::Transport(TransportErrorKind::Custom(
128-
"Encountered too much lag".into(),
129-
))
130-
.into();
131-
self.switch_to_fallback(error).await?;
126+
self.switch_to_fallback(Error::SubscriptionLagged).await?;
132127
}
133128
}
134129
}
@@ -324,6 +319,9 @@ mod tests {
324319
Error::Closed => {
325320
panic!("Unexpected Closed error");
326321
}
322+
Error::SubscriptionLagged => {
323+
panic!("Unexpected SubscriptionLagged error");
324+
}
327325
}
328326
}
329327

0 commit comments

Comments
 (0)