How correctly bind the sending time of each request in multy reqwest?

⚓ Rust    📅 2026-02-14    👤 surdeus    👁️ 1      

surdeus

How correctly bind the sending time of each request:
(to correlate the time of a request with its response)

Cargo.toml (click for more details)
use futures::{stream, StreamExt};
use reqwest::Client;
use tokio;
use chrono::NaiveDateTime;
use chrono::Utc;


const CONCURRENT_REQUESTS: usize = 10;

#[tokio::main]
async fn main() {

   let client = Client::new();
   let urls = vec!["https://api.ipify.org"; 10];
   
   let mut data = Vec::with_capacity(urls.len());
   let mut tm_quer: Vec<NaiveDateTime> = Vec::with_capacity(urls.len());
   let mut tm_resp: Vec<NaiveDateTime> = Vec::with_capacity(urls.len());

   let bodies = stream::iter(urls)
   .map(|url| {
      let client = &client;
      async move {
         let resp = client.get(url).send().await?;
         resp.bytes().await
      }
   })
   .buffer_unordered(CONCURRENT_REQUESTS);
   
   // ^^^_ tm_quer.push(Utc::now().naive_utc());

   bodies
   .for_each(|b| {
      match b {
         Ok(b) => {
            tm_resp.push(Utc::now().naive_utc());
            data.push(b);
         },
         Err(e) => eprintln!("Error:{}", e),
      }
      async {()}})
   .await;

   println!("{:#?}\n", &tm_quer);
   println!("{:#?}\n", &tm_resp);
   println!("{:#?}", &data);
}
Code returns: (click for more details)

1 post - 1 participant

Read full topic

🏷️ Rust_feed