Skip to content

Commit

Permalink
Other cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillFish8 committed Sep 3, 2023
1 parent 7b63bf6 commit 5365121
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 59 deletions.
9 changes: 4 additions & 5 deletions datacake-rpc/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::marker::PhantomData;
use std::time::Duration;

Expand Down Expand Up @@ -140,8 +139,8 @@ where
<Svc as Handler<Msg>>::Reply: RequestContents + TryIntoBody,
{
let metadata = MessageMetadata {
service_name: Cow::Borrowed(<Svc as RpcService>::service_name()),
path: Cow::Borrowed(<Svc as Handler<Msg>>::path()),
service_name: <Svc as RpcService>::service_name(),
path: <Svc as Handler<Msg>>::path(),
};

let body = msg.try_as_body()?;
Expand All @@ -164,8 +163,8 @@ where
<Svc as Handler<Msg>>::Reply: RequestContents + TryIntoBody,
{
let metadata = MessageMetadata {
service_name: Cow::Borrowed(<Svc as RpcService>::service_name()),
path: Cow::Borrowed(<Svc as Handler<Msg>>::path()),
service_name: <Svc as RpcService>::service_name(),
path: <Svc as Handler<Msg>>::path(),
};

let body = msg.try_into_body()?;
Expand Down
6 changes: 1 addition & 5 deletions datacake-rpc/src/net/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ impl Channel {
metadata: MessageMetadata,
msg: Body,
) -> Result<Result<Body, AlignedVec>, Error> {
let uri = format!(
"http://{}{}",
self.remote_addr,
crate::to_uri_path(&metadata.service_name, &metadata.path),
);
let uri = format!("http://{}{}", self.remote_addr, metadata.to_uri_path(),);
let request = Request::builder()
.method(Method::POST)
.uri(uri)
Expand Down
61 changes: 12 additions & 49 deletions datacake-rpc/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::borrow::Cow;
use std::fmt::{Debug, Formatter};
use std::net::SocketAddr;
use std::ops::Deref;

use async_trait::async_trait;
use rkyv::{Archive, Deserialize, Serialize};
use rkyv::Archive;

use crate::rkyv_tooling::DataView;
use crate::{Body, Status};
Expand Down Expand Up @@ -48,17 +47,21 @@ where
}
}

#[repr(C)]
#[derive(Serialize, Deserialize, Archive, PartialEq)]
#[derive(PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[archive(check_bytes)]
pub struct MessageMetadata {
#[with(rkyv::with::AsOwned)]
/// The name of the service being targeted.
pub(crate) service_name: Cow<'static, str>,
#[with(rkyv::with::AsOwned)]
pub(crate) service_name: &'static str,
/// The message name/path.
pub(crate) path: Cow<'static, str>,
pub(crate) path: &'static str,
}

impl MessageMetadata {
#[inline]
/// Produces a uri path for the metadata.
pub(crate) fn to_uri_path(&self) -> String {
crate::to_uri_path(self.service_name, self.path)
}
}

/// A zero-copy view of the message data and any additional metadata provided
Expand Down Expand Up @@ -157,43 +160,3 @@ where
Self::new(addr, contents)
}
}

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

#[test]
fn test_metadata() {
let meta = MessageMetadata {
service_name: Cow::Borrowed("test"),
path: Cow::Borrowed("demo"),
};

let bytes = rkyv::to_bytes::<_, 1024>(&meta).expect("Serialize");
let copy: MessageMetadata = rkyv::from_bytes(&bytes).expect("Deserialize");
assert_eq!(meta, copy, "Deserialized value should match");
}

#[test]
fn test_request() {
let msg = MessageMetadata {
service_name: Cow::Borrowed("test"),
path: Cow::Borrowed("demo"),
};

let addr = "127.0.0.1:8000".parse().unwrap();
let mut bytes = rkyv::to_bytes::<_, 1024>(&msg).expect("Serialize");
let checksum = crc32fast::hash(&bytes);
bytes.extend_from_slice(&checksum.to_le_bytes());

let view: DataView<MessageMetadata> =
DataView::using(bytes).expect("Create view");
let req = Request::<MessageMetadata>::new(addr, view);
assert_eq!(req.remote_addr(), addr, "Remote addr should match.");
assert_eq!(
req.to_owned().unwrap(),
msg,
"Deserialized value should match."
);
}
}

0 comments on commit 5365121

Please sign in to comment.