Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add annotation #7

Merged
merged 1 commit into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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