Skip to content

Commit cea4fe7

Browse files
authored
Merge pull request #7 from upupnoah/main
docs: add annotation
2 parents a994ab7 + 77f4a1f commit cea4fe7

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.PYONY: run_with_log
2+
3+
run_with_log:
4+
@RUST_LOG=info cargo run -- $(ARGS)

src/cmd/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{Backend, RespArray, RespError, RespFrame, SimpleString};
88
mod hmap;
99
mod map;
1010

11-
// you could also use once_cell instead of lazy_static
11+
// NOTE: you could also use once_cell instead of lazy_static
1212
// lazy_static:
1313
// 1. init in runtime
1414
// 2. thread safe
@@ -17,7 +17,7 @@ mod map;
1717
// static ref RESP_OK: RespFrame = SimpleString::new("OK").into();
1818
// }
1919

20-
// > Rust 1.80.0
20+
// NOTE: > Rust 1.80.0
2121
// https://blog.rust-lang.org/2024/07/25/Rust-1.80.0.html
2222
static RESP_OK: LazyLock<RespFrame> = LazyLock::new(|| SimpleString::new("OK").into());
2323

src/main.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,34 @@ use simple_redis::{network, Backend};
33
use tokio::net::TcpListener;
44
use tracing::info;
55

6+
// #[tokio::main]
7+
// async fn main() -> Result<()> {
8+
// tracing_subscriber::fmt::init();
9+
10+
// let addr = "0.0.0.0:6379";
11+
// let listener = TcpListener::bind(addr).await?;
12+
// info!("Simple-Redis-Server is listening on {}", addr);
13+
14+
// let backend = Backend::new();
15+
// loop {
16+
// let (stream, remote_addr) = listener.accept().await?;
17+
// info!("Accepted connection from {}", remote_addr);
18+
// let cloned_backend = backend.clone();
19+
// tokio::spawn(async move {
20+
// // handling of stream
21+
// match network::stream_handler(stream, cloned_backend).await {
22+
// Ok(_) => info!("Connection from {} closed", remote_addr),
23+
// Err(e) => info!("Connection from {} closed with error: {}", remote_addr, e),
24+
// }
25+
// });
26+
// }
27+
// }
28+
629
#[tokio::main]
730
async fn main() -> Result<()> {
831
tracing_subscriber::fmt::init();
932

10-
let addr = "0.0.0.0:6389";
33+
let addr = "0.0.0.0:6379";
1134
let listener = TcpListener::bind(addr).await?;
1235
info!("Simple-Redis-Server is listening on {}", addr);
1336

src/network.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ pub async fn stream_handler(stream: TcpStream, backend: Backend) -> Result<()> {
3737
};
3838
let response = request_handler(request).await?;
3939
info!("Sending response: {:?}", response.frame);
40-
framed.send(response.frame).await?;
40+
41+
// NOTE: When dealing with a large amount of concurrent data,
42+
// flushing the sink each time will incur performance overhead.
43+
// framed.send(response.frame).await?;
44+
45+
// 使用 feed 方法添加响应
46+
framed.feed(response.frame).await?;
47+
// 在合适的时候调用 flush 方法
48+
framed.flush().await?;
4149
}
4250
Some(Err(e)) => return Err(e),
4351
None => return Ok(()),

0 commit comments

Comments
 (0)