handle tokio delay_for block #2349
Answered
by
fakeshadow
daiguadaidai
asked this question in
Q&A
Replies: 1 comment 1 reply
-
use std::{
io::{Read, Write},
net::TcpStream,
};
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
async fn handle_hello() -> impl Responder {
HttpResponse::Ok().body("get api hello!")
}
#[get("/world")]
async fn handle_world() -> impl Responder {
println!("tokio sleep start");
tokio::time::delay_for(tokio::time::Duration::from_secs(5)).await;
println!("tokio sleep end");
HttpResponse::Ok().body("get api world!")
}
fn config(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("/hello").route(web::get().to(handle_hello)))
.service(handle_world);
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let server = HttpServer::new(|| App::new().service(web::scope("/api").configure(config)))
.bind("127.0.0.1:8080")?
.workers(4)
.run();
let req = "GET /api/world HTTP/1.1\r\n\r\n";
(0..32)
.map(|_| {
std::thread::spawn(move || {
let mut stream = TcpStream::connect("127.0.0.1:8080").unwrap();
stream.write(req.as_ref()).unwrap();
let mut buf = [0u8; 1024];
let n = stream.read(&mut buf).unwrap();
println!("response is {:?}", String::from_utf8_lossy(&buf[..n]));
})
})
.collect::<Vec<_>>()
.into_iter()
.for_each(|h| {
h.join().unwrap();
});
server.stop(false).await;
Ok(())
} Your curl is most likely reusing a single TCP connection so they are queued up. You need a better tool for testing concurrency and/or parallel. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
daiguadaidai
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
verstion
actix-web = "3"
tokio = { version = "0.2", features = ["full"] }
code
client request
server output
I think it should output
Beta Was this translation helpful? Give feedback.
All reactions