Skip to content

Commit

Permalink
Merge pull request #7 from upupnoah/main
Browse files Browse the repository at this point in the history
docs: add annotation
  • Loading branch information
upupnoah authored Jul 28, 2024
2 parents a994ab7 + 77f4a1f commit cea4fe7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PYONY: run_with_log

run_with_log:
@RUST_LOG=info cargo run -- $(ARGS)
4 changes: 2 additions & 2 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<RespFrame> = LazyLock::new(|| SimpleString::new("OK").into());

Expand Down
25 changes: 24 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 9 additions & 1 deletion src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(()),
Expand Down

0 comments on commit cea4fe7

Please sign in to comment.