-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
597 additions
and
30 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod backend; | ||
pub mod cmd; | ||
pub mod network; | ||
mod resp; | ||
|
||
pub use backend::*; | ||
pub use resp::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
use anyhow::Result; | ||
use r_redis::{network, Backend}; | ||
use tokio::net::TcpListener; | ||
use tracing::{info, warn}; | ||
|
||
fn main() -> Result<()> { | ||
println!("Hello, world!"); | ||
Ok(()) | ||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
tracing_subscriber::fmt::init(); | ||
|
||
let addr = "0.0.0.0:6379"; | ||
let listener = TcpListener::bind(addr).await?; | ||
info!("Listening on: {}", addr); | ||
|
||
let backend = Backend::new(); | ||
loop { | ||
let (stream, raddr) = listener.accept().await?; | ||
let cloned_backend = backend.clone(); | ||
tokio::spawn(async move { | ||
match network::stream_handler(stream, cloned_backend).await { | ||
Ok(_) => info!("Connection from {} closed", raddr), | ||
Err(e) => warn!("Connection from {} error: {:?}", raddr, e), | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use crate::cmd::{Command, CommandExecutor}; | ||
use crate::{Backend, RespDecode, RespEncode, RespError, RespFrame}; | ||
use anyhow::Result; | ||
use futures::SinkExt; | ||
use tokio::net::TcpStream; | ||
use tokio_stream::StreamExt; | ||
use tokio_util::codec::{Decoder, Encoder, Framed}; | ||
use tracing::info; | ||
|
||
pub async fn stream_handler(stream: TcpStream, backend: Backend) -> Result<()> { | ||
let mut framed = Framed::new(stream, RespFrameCodec); | ||
loop { | ||
match framed.next().await { | ||
Some(Ok(req)) => { | ||
info!( | ||
"Received request: {:?}", | ||
String::from_utf8_lossy(req.encode().as_slice()) | ||
); | ||
let resp = request_handler(RedisRequest { | ||
frame: req, | ||
backend: backend.clone(), | ||
}) | ||
.await?; | ||
info!( | ||
"Send response: {:?}", | ||
String::from_utf8_lossy(resp.frame.encode().as_slice()) | ||
); | ||
framed.send(resp.frame).await?; | ||
} | ||
Some(Err(e)) => return Err(e), | ||
None => { | ||
info!("Connection closed"); | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
async fn request_handler(req: RedisRequest) -> Result<RedisResponse> { | ||
let cmd = Command::try_from(req.frame)?; | ||
info!("Execute command: {:?}", cmd); | ||
let resp = cmd.execute(&req.backend)?; | ||
Ok(RedisResponse { frame: resp }) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct RedisRequest { | ||
frame: RespFrame, | ||
backend: Backend, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct RedisResponse { | ||
frame: RespFrame, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct RespFrameCodec; | ||
|
||
impl Encoder<RespFrame> for RespFrameCodec { | ||
type Error = anyhow::Error; | ||
|
||
fn encode(&mut self, item: RespFrame, dst: &mut bytes::BytesMut) -> Result<()> { | ||
let data = item.encode(); | ||
dst.extend_from_slice(&data); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Decoder for RespFrameCodec { | ||
type Item = RespFrame; | ||
type Error = anyhow::Error; | ||
|
||
fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> { | ||
match RespFrame::decode(src) { | ||
Ok(frame) => Ok(Some(frame)), | ||
Err(RespError::NotComplete) => Ok(None), | ||
Err(RespError::Empty) => Ok(None), | ||
Err(e) => Err(e.into()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters