Add doc about Ping cmd to README, change the internal data type of Ping cmd to Bytes, and rm unnecessary reference (#118)

This commit is contained in:
Ting Sun
2023-03-28 22:53:33 +08:00
committed by GitHub
parent aab4e39d97
commit b1e365b62f
7 changed files with 14 additions and 12 deletions

1
Cargo.lock generated
View File

@@ -510,7 +510,6 @@ dependencies = [
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tracing", "tracing",
"tracing-futures",
"tracing-opentelemetry", "tracing-opentelemetry",
"tracing-subscriber", "tracing-subscriber",
] ]

View File

@@ -29,7 +29,6 @@ clap = { version = "3.1.18", features = ["derive"] }
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1" tokio-stream = "0.1"
tracing = "0.1.34" tracing = "0.1.34"
tracing-futures = { version = "0.2.3" }
tracing-subscriber = { version = "0.3.11", features = ["env-filter"] } tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
# Implements the types defined in the OTel spec # Implements the types defined in the OTel spec
opentelemetry = { version = "0.17.0", optional = true } opentelemetry = { version = "0.17.0", optional = true }

View File

@@ -88,6 +88,7 @@ https://github.com/aws-observability/aws-otel-collector/blob/main/docs/developer
`mini-redis` currently supports the following commands. `mini-redis` currently supports the following commands.
* [PING](https://redis.io/commands/ping)
* [GET](https://redis.io/commands/get) * [GET](https://redis.io/commands/get)
* [SET](https://redis.io/commands/set) * [SET](https://redis.io/commands/set)
* [PUBLISH](https://redis.io/commands/publish) * [PUBLISH](https://redis.io/commands/publish)

View File

@@ -28,7 +28,8 @@ struct Cli {
enum Command { enum Command {
Ping { Ping {
/// Message to ping /// Message to ping
msg: Option<String>, #[clap(parse(from_str = bytes_from_str))]
msg: Option<Bytes>,
}, },
/// Get the value of key. /// Get the value of key.
Get { Get {

View File

@@ -110,7 +110,7 @@ impl Client {
/// } /// }
/// ``` /// ```
#[instrument(skip(self))] #[instrument(skip(self))]
pub async fn ping(&mut self, msg: Option<String>) -> crate::Result<Bytes> { pub async fn ping(&mut self, msg: Option<Bytes>) -> crate::Result<Bytes> {
let frame = Ping::new(msg).into_frame(); let frame = Ping::new(msg).into_frame();
debug!(request = ?frame); debug!(request = ?frame);
@@ -459,7 +459,7 @@ impl Subscriber {
/// Unsubscribe to a list of new channels /// Unsubscribe to a list of new channels
#[instrument(skip(self))] #[instrument(skip(self))]
pub async fn unsubscribe(&mut self, channels: &[String]) -> crate::Result<()> { pub async fn unsubscribe(&mut self, channels: &[String]) -> crate::Result<()> {
let frame = Unsubscribe::new(&channels).into_frame(); let frame = Unsubscribe::new(channels).into_frame();
debug!(request = ?frame); debug!(request = ?frame);

View File

@@ -1,6 +1,6 @@
use crate::{Connection, Frame, Parse, ParseError}; use crate::{Connection, Frame, Parse, ParseError};
use bytes::Bytes; use bytes::Bytes;
use tracing::instrument; use tracing::{debug, instrument};
/// Returns PONG if no argument is provided, otherwise /// Returns PONG if no argument is provided, otherwise
/// return a copy of the argument as a bulk. /// return a copy of the argument as a bulk.
@@ -10,12 +10,12 @@ use tracing::instrument;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Ping { pub struct Ping {
/// optional message to be returned /// optional message to be returned
msg: Option<String>, msg: Option<Bytes>,
} }
impl Ping { impl Ping {
/// Create a new `Ping` command with optional `msg`. /// Create a new `Ping` command with optional `msg`.
pub fn new(msg: Option<String>) -> Ping { pub fn new(msg: Option<Bytes>) -> Ping {
Ping { msg } Ping { msg }
} }
@@ -40,7 +40,7 @@ impl Ping {
/// PING [message] /// PING [message]
/// ``` /// ```
pub(crate) fn parse_frames(parse: &mut Parse) -> crate::Result<Ping> { pub(crate) fn parse_frames(parse: &mut Parse) -> crate::Result<Ping> {
match parse.next_string() { match parse.next_bytes() {
Ok(msg) => Ok(Ping::new(Some(msg))), Ok(msg) => Ok(Ping::new(Some(msg))),
Err(ParseError::EndOfStream) => Ok(Ping::default()), Err(ParseError::EndOfStream) => Ok(Ping::default()),
Err(e) => Err(e.into()), Err(e) => Err(e.into()),
@@ -55,9 +55,11 @@ impl Ping {
pub(crate) async fn apply(self, dst: &mut Connection) -> crate::Result<()> { pub(crate) async fn apply(self, dst: &mut Connection) -> crate::Result<()> {
let response = match self.msg { let response = match self.msg {
None => Frame::Simple("PONG".to_string()), None => Frame::Simple("PONG".to_string()),
Some(msg) => Frame::Bulk(Bytes::from(msg)), Some(msg) => Frame::Bulk(msg),
}; };
debug!(?response);
// Write the response back to the client // Write the response back to the client
dst.write_frame(&response).await?; dst.write_frame(&response).await?;
@@ -72,7 +74,7 @@ impl Ping {
let mut frame = Frame::array(); let mut frame = Frame::array();
frame.push_bulk(Bytes::from("ping".as_bytes())); frame.push_bulk(Bytes::from("ping".as_bytes()));
if let Some(msg) = self.msg { if let Some(msg) = self.msg {
frame.push_bulk(Bytes::from(msg)); frame.push_bulk(msg);
} }
frame frame
} }

View File

@@ -21,7 +21,7 @@ async fn ping_pong_with_message() {
let (addr, _) = start_server().await; let (addr, _) = start_server().await;
let mut client = client::connect(addr).await.unwrap(); let mut client = client::connect(addr).await.unwrap();
let pong = client.ping(Some("你好世界".to_string())).await.unwrap(); let pong = client.ping(Some("你好世界".into())).await.unwrap();
assert_eq!("你好世界".as_bytes(), &pong[..]); assert_eq!("你好世界".as_bytes(), &pong[..]);
} }