Skip to content
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

feat: demonstrate RetryBackoffLayer #157

Merged
merged 3 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This repository contains the following examples:
- [x] [Wallet management filler](./examples/fillers/examples/wallet_filler.rs)
- [x] Layers
- [x] [Request / response logging layer](./examples/layers/examples/logging_layer.rs)
- [x] [Retry-backoff layer](./examples/layers/examples/retry_layer.rs)
- [x] Node Bindings
- [x] [Deploy contract on local Anvil instance](./examples/node-bindings/examples/anvil_deploy_contract.rs)
- [x] [Fork instance on Anvil](./examples/node-bindings/examples/anvil_fork_instance.rs)
Expand Down
38 changes: 38 additions & 0 deletions examples/layers/examples/retry_layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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().on_client(client);

let latest_block = provider.get_block_number().await?;

assert_eq!(latest_block, 0);

Ok(())
}
Loading