-
Notifications
You must be signed in to change notification settings - Fork 2
Retry logic #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Retry logic #135
Conversation
doc doc
remove import remove wrapped
src/safe_provider.rs
Outdated
pub async fn subscribe_blocks( | ||
&self, | ||
) -> Result<Subscription<N::HeaderResponse>, RpcError<TransportErrorKind>> { | ||
let provider = self.provider.clone(); | ||
self.retry_with_timeout(|| async { provider.subscribe_blocks().await }).await | ||
} | ||
|
||
#[allow(clippy::missing_errors_doc)] | ||
pub(crate) async fn retry_with_timeout<T, F, Fut>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: instead of relying on closures, you could implement a local trait extension on all F: Fn() -> Fut
, potentially avoiding provider
clones, something like the following pseudocode:
trait RetryableWithTimeout<T, Fut> {
async fn retry_with_timeout(&self) -> Result<T, RpcError<TransportErrorKind>>;
}
impl<T, Fut> RetryableWithTimeout<T, Fut> for F
where
F: Fn() -> Fut
Fut: Future<Output = Result<T, RpcError<TransportErrorKind>>>,
{
//...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice API, will be useful for multi-provider support (see #6 ).
Left comments mostly related to refactors and nits 👍
src/safe_provider.rs
Outdated
//! ```rust,no_run | ||
//! use alloy::{ | ||
//! network::Ethereum, | ||
//! providers::{RootProvider, WsConnect}, | ||
//! rpc::client::ClientBuilder, | ||
//! }; | ||
//! use event_scanner::safe_provider::SafeProvider; | ||
//! use std::time::Duration; | ||
//! | ||
//! async fn example() -> Result<(), Box<dyn std::error::Error>> { | ||
//! let provider = RootProvider::<Ethereum>::new( | ||
//! ClientBuilder::default().ws(WsConnect::new("wss://localhost:8000")).await?, | ||
//! ); | ||
//! let safe_provider = SafeProvider::new(provider) | ||
//! .with_max_timeout(Duration::from_secs(30)) | ||
//! .with_max_retries(5); | ||
//! | ||
//! let block = safe_provider.get_block_by_number(12345.into()).await?; | ||
//! Ok(()) | ||
//! } | ||
//! ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay very interesting
Took me a while to realise the doc comment has to be above the actual object (ofc..)
Also i adding no run still compiles the test https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html?highlight=no_run#attributes it just doesnt 'run' the code. Im not sure whats better but since we dont actually want to connect to a provider for the doc test we can leave it to no run
lmk what you think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually removed the doc completely - i dont think we should expose the safe provider to outside the crate, therefore it doesnt make sense
Co-authored-by: Nenad <[email protected]>
Resolves #112