]> piware.de Git - learn-rust.git/commitdiff
async-http: Serve requests in parallel in threads
authorMartin Pitt <martin@piware.de>
Fri, 16 Sep 2022 07:03:46 +0000 (09:03 +0200)
committerMartin Pitt <martin@piware.de>
Fri, 16 Sep 2022 07:03:46 +0000 (09:03 +0200)
Use async_std::task::spawn to launch a request in a thread.

Now even synchronous sleep does not block other requests, as long as
they don't exceed the thread pool capacity.

async-http/src/main.rs

index cf0b46de8e2995b0da941ce29f0609bf540571cb..c2884082c095f96dcb4598483b484ca66e4d92fd 100644 (file)
@@ -15,7 +15,7 @@ async fn main() {
 
     listener.incoming().for_each_concurrent(/* limit */ None, |tcpstream| async move {
         let tcpstream = tcpstream.unwrap();
 
     listener.incoming().for_each_concurrent(/* limit */ None, |tcpstream| async move {
         let tcpstream = tcpstream.unwrap();
-        handle_connection(tcpstream).await;
+        task::spawn(handle_connection(tcpstream));
     }).await;
 }
 
     }).await;
 }