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

[Feature] Alloy ThrottleLayer #1636

Open
0xKitsune opened this issue Nov 8, 2024 · 1 comment
Open

[Feature] Alloy ThrottleLayer #1636

0xKitsune opened this issue Nov 8, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@0xKitsune
Copy link

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

@0xKitsune 0xKitsune added the enhancement New feature or request label Nov 8, 2024
@0xKitsune 0xKitsune changed the title [Feature] Alloy Provider Throttle [Feature] Alloy ThrottleLayer Nov 8, 2024
@zerosnacks
Copy link
Member

Hi @0xKitsune thanks for your suggestion

I think this could be useful addition, would be added to https://github.com/alloy-rs/alloy/tree/main/crates/transport/src/layers

cc @yash-atreya / @mattsse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants