diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..630178d --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PYONY: run_with_log + +run_with_log: + @RUST_LOG=info cargo run -- $(ARGS) diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 12da088..82d83d8 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -8,7 +8,7 @@ use crate::{Backend, RespArray, RespError, RespFrame, SimpleString}; mod hmap; mod map; -// you could also use once_cell instead of lazy_static +// NOTE: you could also use once_cell instead of lazy_static // lazy_static: // 1. init in runtime // 2. thread safe @@ -17,7 +17,7 @@ mod map; // static ref RESP_OK: RespFrame = SimpleString::new("OK").into(); // } -// > Rust 1.80.0 +// NOTE: > Rust 1.80.0 // https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html static RESP_OK: LazyLock = LazyLock::new(|| SimpleString::new("OK").into()); diff --git a/src/main.rs b/src/main.rs index ef80ad8..46468cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,34 @@ use simple_redis::{network, Backend}; use tokio::net::TcpListener; use tracing::info; +// #[tokio::main] +// async fn main() -> Result<()> { +// tracing_subscriber::fmt::init(); + +// let addr = "0.0.0.0:6379"; +// let listener = TcpListener::bind(addr).await?; +// info!("Simple-Redis-Server is listening on {}", addr); + +// let backend = Backend::new(); +// loop { +// let (stream, remote_addr) = listener.accept().await?; +// info!("Accepted connection from {}", remote_addr); +// let cloned_backend = backend.clone(); +// tokio::spawn(async move { +// // handling of stream +// match network::stream_handler(stream, cloned_backend).await { +// Ok(_) => info!("Connection from {} closed", remote_addr), +// Err(e) => info!("Connection from {} closed with error: {}", remote_addr, e), +// } +// }); +// } +// } + #[tokio::main] async fn main() -> Result<()> { tracing_subscriber::fmt::init(); - let addr = "0.0.0.0:6389"; + let addr = "0.0.0.0:6379"; let listener = TcpListener::bind(addr).await?; info!("Simple-Redis-Server is listening on {}", addr); diff --git a/src/network.rs b/src/network.rs index 29a1656..0cb13b2 100644 --- a/src/network.rs +++ b/src/network.rs @@ -37,7 +37,15 @@ pub async fn stream_handler(stream: TcpStream, backend: Backend) -> Result<()> { }; let response = request_handler(request).await?; info!("Sending response: {:?}", response.frame); - framed.send(response.frame).await?; + + // NOTE: When dealing with a large amount of concurrent data, + // flushing the sink each time will incur performance overhead. + // framed.send(response.frame).await?; + + // 使用 feed 方法添加响应 + framed.feed(response.frame).await?; + // 在合适的时候调用 flush 方法 + framed.flush().await?; } Some(Err(e)) => return Err(e), None => return Ok(()),