X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=tokio-tutorial-mini-redis%2Fsrc%2Fbin%2Fserver.rs;h=962c923dab5c13b17c34a50504c71a324f2733bf;hp=441ea1881c90007acb12897eb0fb89a060948b5c;hb=795a7f60cfacc6dbd3be562e8fd414affa803b01;hpb=ad0d6c330c5aea1ffe3a553d5d1d14a513d3cff3 diff --git a/tokio-tutorial-mini-redis/src/bin/server.rs b/tokio-tutorial-mini-redis/src/bin/server.rs index 441ea18..962c923 100644 --- a/tokio-tutorial-mini-redis/src/bin/server.rs +++ b/tokio-tutorial-mini-redis/src/bin/server.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::error::Error; use std::sync::{Arc, Mutex}; use bytes::Bytes; @@ -19,19 +20,26 @@ async fn main() { let db: Db = Arc::new(Mutex::new(HashMap::new())); loop { - // The second item contains the IP and port of the new connection - let (socket, addr) = listener.accept().await.unwrap(); - log::debug!("got connection from {:?}", addr); - let db_i = db.clone(); - tokio::spawn(async move { process(socket, db_i).await }); + match listener.accept().await { + Ok((socket, addr)) => { + log::debug!("got connection from {:?}", addr); + let db_i = db.clone(); + tokio::spawn(async move { + if let Err(e) = process(socket, db_i).await { + log::warn!("failed: {:?}", e); + } + }); + }, + Err(e) => log::warn!("Failed to accept connection: {}", e), + }; } } -async fn process(socket: TcpStream, db: Db) { +async fn process(socket: TcpStream, db: Db) -> Result<(), Box> { let mut connection = Connection::new(socket); - while let Some(frame) = connection.read_frame().await.unwrap() { - let response = match Command::from_frame(frame).unwrap() { + while let Some(frame) = connection.read_frame().await? { + let response = match Command::from_frame(frame)? { Set(cmd) => { // The value is stored as `Vec` db.lock().unwrap().insert(cmd.key().to_string(), cmd.value().clone()); @@ -51,6 +59,7 @@ async fn process(socket: TcpStream, db: Db) { }; // Write the response to the client - connection.write_frame(&response).await.unwrap(); + connection.write_frame(&response).await?; } + Ok(()) }