Search code examples
rustreqwest

How to make some parallel https requests using reqwest?


I need to make 10 parallel requests to the site from different IPs (at the same time).

I have an array of 10 proxies, I need to make 10 parallel requests to the site using 1 of 10 proxies for each request in order.

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let proxy = [
        "https://example.prox:4545",
        "https://example.prox:8080",
        "https://example.prox:80",
        "https://example.prox:90",
        "https://example.prox:9050",
        // ... array of 10 proxy
    ];
    let client = reqwest::Client::builder()
        .proxy(reqwest::Proxy::all("https://example.prox:4545")?)
        .build()?;

    let url = "http://httpbin.org/ip";

    let resp = client.get(url).send().await?;

    Ok(())
}

Solution

  • Use futures::future::join_all():

    #[tokio::main]
    async fn main() -> Result<(), Box<dyn std::error::Error>> {
        let proxy = [
            "https://example.prox:4545",
            "https://example.prox:8080",
            "https://example.prox:80",
            "https://example.prox:90",
            "https://example.prox:9050",
            // ... array of 10 proxy
        ];
        let responses: Vec<reqwest::Result<reqwest::Response>> =
            futures::future::join_all(proxy.iter().map(|&proxy| async move {
                let client = reqwest::Client::builder()
                    .proxy(reqwest::Proxy::all(proxy)?)
                    .build()?;
    
                let url = "http://httpbin.org/ip";
    
                client.get(url).send().await
            }))
            .await;
    
        Ok(())
    }
    

    Note: This does not execute the requests in parallel but concurrently, however this probably does not matter for you.