Description
Component
rpc, transports
Describe the feature you would like
In some instances, it would be useful to have a ThrottleLayer
to not flood the node with requests all at once. While the RetryLayer
works perfectly for retrying requests that exceed RPC provider limits, the ThrottleLayer
could throttle outbound requests before they are sent.
The following snippet shows an example that fetches 100k blocks concurrently. In this example a ThrottleLayer
would be helpful to limit n
requests per second.
#[tokio::main]
async fn main() -> eyre::Result<()> {
// --snip--
// Init the provider
let client = ClientBuilder::default()
.layer(RetryBackoffLayer::new(10, 300, 330))
.http(rpc_endpoint.parse()?);
let provider = ProviderBuilder::new().on_client(client);
let futures_unordered = FuturesUnordered::new();
// Get 100_000 blocks
let block_number = provider.get_block_number().await?;
for i in block_number - 100_000..block_number {
let provider = provider.clone();
futures_unordered.push(async move {
provider
.get_block(i.into(), BlockTransactionsKind::Hashes)
.await
});
}
// Await the results
let res = futures_unordered.collect::<Vec<_>>().await;
Ok(())
}
We currently have a working version of a ThrottleLayer built and I would be happy to open a draft PR with this if you think it would be useful to add to alloy
.
Additional context
No response