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

feat: adds PING support to momento proxy resp impl #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions src/protocol/resp/src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use std::sync::Arc;

mod badd;
mod get;
mod ping;
mod set;

pub use badd::BAddRequest;
pub use get::GetRequest;
pub use ping::PingRequest;
pub use set::SetRequest;

#[derive(Default)]
Expand Down Expand Up @@ -98,6 +100,9 @@ impl Parse<Request> for RequestParser {
Some(b"set") | Some(b"SET") => {
SetRequest::try_from(message).map(Request::from)
}
Some(b"ping") | Some(b"PING") => {
PingRequest::try_from(message).map(Request::from)
}
_ => Err(Error::new(ErrorKind::Other, "unknown command")),
},
_ => {
Expand All @@ -121,6 +126,7 @@ impl Compose for Request {
Self::BAdd(r) => r.compose(buf),
Self::Get(r) => r.compose(buf),
Self::Set(r) => r.compose(buf),
Self::Ping(r) => r.compose(buf),
}
}
}
Expand All @@ -130,6 +136,7 @@ pub enum Request {
BAdd(BAddRequest),
Get(GetRequest),
Set(SetRequest),
Ping(PingRequest),
}

impl From<BAddRequest> for Request {
Expand All @@ -150,11 +157,18 @@ impl From<SetRequest> for Request {
}
}

impl From<PingRequest> for Request {
fn from(other: PingRequest) -> Self {
Self::Ping(other)
}
}

#[derive(Debug, PartialEq, Eq)]
pub enum Command {
BAdd,
Get,
Set,
Ping,
}

impl TryFrom<&[u8]> for Command {
Expand All @@ -165,6 +179,7 @@ impl TryFrom<&[u8]> for Command {
b"badd" | b"BADD" => Ok(Command::BAdd),
b"get" | b"GET" => Ok(Command::Get),
b"set" | b"SET" => Ok(Command::Set),
b"ping" | b"PING" => Ok(Command::Ping),
_ => Err(()),
}
}
Expand Down
59 changes: 59 additions & 0 deletions src/protocol/resp/src/request/ping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed under the Apache License, Version 2.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add the Pelikan Foundation LLC as the copyright holder w/ 2022 year.

// http://www.apache.org/licenses/LICENSE-2.0

use super::*;
use std::io::{Error, ErrorKind};

#[derive(Debug, PartialEq, Eq)]
#[allow(clippy::redundant_allocation)]
pub struct PingRequest {}

impl TryFrom<Message> for PingRequest {
type Error = Error;

fn try_from(other: Message) -> Result<Self, Error> {
if let Message::Array(array) = other {
if array.inner.is_none() {
return Err(Error::new(ErrorKind::Other, "malformed command"));
}
Ok(Self {})
} else {
Err(Error::new(ErrorKind::Other, "malformed command"))
}
}
}

impl PingRequest {
pub fn new() -> Self {
Self {}
}
}

impl From<&PingRequest> for Message {
fn from(_: &PingRequest) -> Message {
Message::Array(Array {
inner: Some(vec![Message::BulkString(BulkString::new(b"Ping"))]),
})
}
}

impl Compose for PingRequest {
fn compose(&self, buf: &mut dyn BufMut) -> usize {
let message = Message::from(self);
message.compose(buf)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parser() {
let parser = RequestParser::new();
assert_eq!(
parser.parse(b"PING\r\n").unwrap().into_inner(),
Request::Ping(PingRequest::new())
);
}
}
5 changes: 5 additions & 0 deletions src/proxy/momento/src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ pub(crate) async fn handle_resp_client(
break;
}
}
resp::Request::Ping(_) => {
if resp::ping(&mut socket).await.is_err() {
break;
}
}
_ => {
println!("bad request");
let _ = socket.write_all(b"CLIENT_ERROR\r\n").await;
Expand Down
2 changes: 2 additions & 0 deletions src/proxy/momento/src/protocol/resp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
pub use protocol_resp::{Request, RequestParser};

mod get;
mod ping;
mod set;

pub use get::*;
pub use ping::*;
pub use set::*;
22 changes: 22 additions & 0 deletions src/proxy/momento/src/protocol/resp/ping.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed under the Apache License, Version 2.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

// http://www.apache.org/licenses/LICENSE-2.0

use crate::Error;
use net::TCP_SEND_BYTE;
use session::{SESSION_SEND, SESSION_SEND_BYTE, SESSION_SEND_EX};
use tokio::io::AsyncWriteExt;

const PONG_RSP: &[u8; 7] = b"+PONG\r\n";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this wasn't implemented in the RESP protocol instead?


pub async fn ping(socket: &mut tokio::net::TcpStream) -> Result<(), Error> {
let mut response_buf = Vec::new();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do all this w/o allocating here

response_buf.extend_from_slice(PONG_RSP);
SESSION_SEND.increment();
SESSION_SEND_BYTE.add(response_buf.len() as _);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PONG_RSP.len() should work too?

TCP_SEND_BYTE.add(response_buf.len() as _);
if let Err(e) = socket.write_all(&response_buf).await {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you could .write_all(PONG_RSP) here?

SESSION_SEND_EX.increment();
return Err(e);
}
Ok(())
}