I want to test if the rate limiting of my site is working.
To do this I would like to send a controlled amount of requests. For example, exactly 100 requests per second, and probably save the responses.
From How can I perform parallel asynchronous HTTP GET requests with reqwest? and this gist I wrote the following:
# [dependencies]
# reqwest = { version = "0.11.6" }
# tokio = { version = "1.14.0", features = ["full"] }
# futures = "0.3.24"
use futures::stream::StreamExt;
use reqwest::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let paths: Vec<String> = vec![String::from("https://api.ipify.org/"); 50];
let fetches = futures::stream::iter(paths.into_iter().map(|path| {
let client = Client::new();
let send_fut = client.get(&path).send();
async move {
let response = send_fut.await;
match response {
Ok(resp) => {
println!("{}", resp.status());
}
Err(_) => println!("Error"),
}
}
}))
.buffer_unordered(100)
.collect::<Vec<()>>();
fetches.await;
Ok(())
}
The point is that I dont know how to control how many request are executed per second.
Any idea will be welcome!