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: Add ThrottleLayer to Transport layers #2154

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

0xKitsune
Copy link

@0xKitsune 0xKitsune commented Mar 7, 2025

Closes #1636

This PR adds a ThrottleLayer for alloy providers to throttle outbound rpc requests. While the RetryLayer works great for for retrying requests that exceed RPC provider limits, the ThrottleLayer reduces the frequency that requests need to be retried.

async fn main() -> eyre::Result<()> {
// --snip--
 let requests_per_second = NonZeroU32::new(50).unwrap();

 let client = ClientBuilder::default()
    // Throttle RPC requests at 40 requests per second 
    .layer(ThrottleLayer::new(requests_per_second))
    // The RetryBackoffLayer can be stacked with the throttle layer to retry failed requests
    .layer(RetryBackoffLayer::new(10, 300, 330))
    .http(rpc_endpoint.parse()?);
 
    // Get 100_000 blocks concurrently
    let futures_unordered = FuturesUnordered::new();
    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;

    }

Copy link
Member

@yash-atreya yash-atreya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

Can we put this behind a feature flag since it adds a new dependency?

A test would be great as well, you can put it inprovider/trait.rs

@0xKitsune
Copy link
Author

0xKitsune commented Mar 8, 2025

@yash-atreya Ive updated the PR to put the Throttle logic behind a feature flag as well as added tests.

Note that I changed the ThrottleLayer::new() function to take a u32 instead of NonZeroU32 for simplicity. If 0 is passed to this function it will panic. I can revert this change back to use NonZeroU32 if preferred.

Lmk if you would like any other updates.

@0xKitsune 0xKitsune requested a review from yash-atreya March 8, 2025 01:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

[Feature] Alloy ThrottleLayer
2 participants