forked from alloy-rs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathretry_layer.rs
More file actions
38 lines (27 loc) · 1.12 KB
/
Copy pathretry_layer.rs
File metadata and controls
38 lines (27 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//! This example demonstrates how to use the [`RetryBackoffLayer`] in the provider.
use alloy::{
node_bindings::Anvil,
providers::{Provider, ProviderBuilder},
rpc::client::RpcClient,
transports::layers::RetryBackoffLayer,
};
#[tokio::main]
async fn main() -> eyre::Result<()> {
let anvil = Anvil::new().spawn();
// The maximum number of retries for rate limit errors
let max_retry = 10;
// The initial backoff in milliseconds
let backoff = 1000;
// The number of compute units per second for this provider
let cups = 100;
// Instantiate the RetryBackoffLayer with the configuration
let retry_layer = RetryBackoffLayer::new(max_retry, backoff, cups);
// Add the layer to the transport client.
// The layer will retry all requests that return a rate limit error (eg. 429) until max_retries
// have been reached.
let client = RpcClient::builder().layer(retry_layer).http(anvil.endpoint_url());
let provider = ProviderBuilder::new().connect_client(client);
let latest_block = provider.get_block_number().await?;
assert_eq!(latest_block, 0);
Ok(())
}