#request #http #http-request #get

simple-concurrent-get

Simply make multiple concurrent HTTP GET requests

8 releases

0.3.1 Nov 25, 2024
0.3.0 Nov 24, 2024
0.2.1 Nov 23, 2024
0.1.4 Nov 20, 2024

#1731 in Network programming

Download history 425/week @ 2024-11-15 436/week @ 2024-11-22 36/week @ 2024-11-29 22/week @ 2024-12-06

919 downloads per month

GPL-3.0 license

31KB
395 lines

simple-concurrent-get

Latest version Documentation MIT

Make multiple concurrent HTTP GET requests with ease.

Usage

  • concurrent_get
#[tokio::main]
async fn main() {
    let request_urls = vec![
        "https://example.com/1",
        "https://example.com/2",
        "https://example.com/3",
        "https://example.com/4",
        // ...
    ];

    // for example: maximum 3 concurrent request
    let request_results = simple_concurrent_get::concurrent_get(request_urls, 3);
    let _: Vec<_> = request_results
        .await
        .map(|result| match result {
            Ok(response) => {
                let url = response.url().to_owned();
                let bytes = futures_executor::block_on(async{ response.bytes().await }).unwrap();
                println!("Successfully got '{}' with '{}' bytes of content", url, bytes.len())
            },
            Err(e) => eprintln!("{}", e),
        })
        .collect();
}
  • concurrent_get_foreach
#[tokio::main]
async fn main() {
    let request_urls = vec![
        "https://example.com/1",
        "https://example.com/2",
        "https://example.com/3",
        "https://example.com/4",
        // ...
    ];

    // for example: maximum 3 concurrent request
    let _: Vec<_> = simple_concurrent_get::concurrent_get_foreach(request_urls, 3,
        |result| match result {
            Ok(response) => {
                let url = response.url().to_owned();
                let bytes = futures_executor::block_on(async{ response.bytes().await }).unwrap();
                println!("Successfully got '{}' with '{}' bytes of content", url, bytes.len())
            },
            Err(e) => eprintln!("{}", e),
        }
    )
        .await
        .collect(); 
}

Dependencies

~4–15MB
~190K SLoC