-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Licensed under the Apache License, Version 2.0 | ||
// 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()) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Licensed under the Apache License, Version 2.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 _); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
TCP_SEND_BYTE.add(response_buf.len() as _); | ||
if let Err(e) = socket.write_all(&response_buf).await { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think you could |
||
SESSION_SEND_EX.increment(); | ||
return Err(e); | ||
} | ||
Ok(()) | ||
} |
There was a problem hiding this comment.
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.