Skip to content

Commit

Permalink
add raw_client
Browse files Browse the repository at this point in the history
  • Loading branch information
cospectrum committed Jan 3, 2024
1 parent 4a39b21 commit 118fb52
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 15 deletions.
3 changes: 3 additions & 0 deletions memcrab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ repository = "https://github.com/cospectrum/memcrab"
[dependencies]
prost = { workspace = true }
tonic = { workspace = true }

[dev-dependencies]
anyhow = "1.0.79"
40 changes: 31 additions & 9 deletions memcrab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,58 @@

## Examples

### gRPC
### RawClient

```rust
use memcrab::RawClient;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let addr = "http://[::1]:50051";
let client = RawClient::connect(addr).await?;

client.set("age", vec![0, 21]).await?;
client.set("year", "2024".into()).await?;

let name = client.get("name").await?;
match name {
Some(val) => println!("got {:?} from cache", val),
None => println!("cache miss for name"),
}
Ok(())
}
```

### tonic

```rust
use memcrab::pb::{cache_rpc_client::CacheRpcClient, GetRequest, SetRequest};

#[tokio::main]
async fn main() {
let dst_addr = "http://[::1]:50051";
let mut client = CacheRpcClient::connect(dst_addr).await.unwrap();
async fn main() -> anyhow::Result<()> {
let addr = "http://[::1]:50051";
let mut client = CacheRpcClient::connect(addr).await?;

let msg = GetRequest {
key: "name".to_owned(),
};
let req = tonic::Request::new(msg);
let resp = client.get(req).await.unwrap().into_inner();

let resp = client.get(req).await?.into_inner();
match resp.value {
Some(val) => {
println!("got bytes from cache: {:?}", val);
},
}
None => {
println!("no value in cache");
},
}
}

let msg = SetRequest {
key: "fullname".to_owned(),
value: vec![1, 3, 4, 5, 6, 7],
};
let req = tonic::Request::new(msg);
client.set(req).await.unwrap();
client.set(req).await?;
Ok(())
}
```
4 changes: 4 additions & 0 deletions memcrab/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
mod raw_client;

#[allow(clippy::all)]
#[rustfmt::skip]
pub mod pb;

pub use raw_client::RawClient;

#[cfg(test)]
mod tests {}
34 changes: 34 additions & 0 deletions memcrab/src/raw_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use tonic::{transport::Channel, Status};

use crate::pb::{cache_rpc_client::CacheRpcClient, GetRequest, SetRequest};

type StdError = Box<dyn std::error::Error + Send + Sync + 'static>;

pub struct RawClient {
inner: CacheRpcClient<Channel>,
}

impl RawClient {
pub async fn connect<D>(endpoint: D) -> Result<Self, tonic::transport::Error>
where
D: TryInto<tonic::transport::Endpoint>,
D::Error: Into<StdError>,
{
let inner = CacheRpcClient::connect(endpoint).await?;
Ok(Self { inner })
}
pub async fn get(&self, key: impl Into<String>) -> Result<Option<Vec<u8>>, Status> {
let key = key.into();
let msg = GetRequest { key };
let req = tonic::Request::new(msg);
let resp = self.inner.clone().get(req).await?;
Ok(resp.into_inner().value)
}
pub async fn set(&self, key: impl Into<String>, value: Vec<u8>) -> Result<(), Status> {
let key = key.into();
let msg = SetRequest { key, value };
let req = tonic::Request::new(msg);
self.inner.clone().set(req).await?;
Ok(())
}
}
16 changes: 16 additions & 0 deletions memcrab/tests/raw_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use memcrab::RawClient;

async fn _test_raw_client() -> anyhow::Result<()> {
let addr = "http://[::1]:50051";
let client = RawClient::connect(addr).await?;

client.set("age", vec![0, 21]).await?;
client.set("year", "2024".into()).await?;

let name = client.get("name").await?;
match name {
Some(val) => println!("got {:?} from cache", val),
None => println!("cache miss for name"),
}
Ok(())
}
13 changes: 7 additions & 6 deletions memcrab/tests/run.rs → memcrab/tests/tonic.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use memcrab::pb::{cache_rpc_client::CacheRpcClient, GetRequest, SetRequest};

async fn _test_client() {
let dst_addr = "http://[::1]:50051";
let mut client = CacheRpcClient::connect(dst_addr).await.unwrap();
async fn _test_tonic_client() -> anyhow::Result<()> {
let addr = "http://[::1]:50051";
let mut client = CacheRpcClient::connect(addr).await?;

let msg = GetRequest {
key: "name".to_owned(),
};
let req = tonic::Request::new(msg);
let resp = client.get(req).await.unwrap().into_inner();

let resp = client.get(req).await?.into_inner();
match resp.value {
Some(val) => {
println!("got bytes from cache: {:?}", val);
Expand All @@ -24,5 +23,7 @@ async fn _test_client() {
value: vec![1, 3, 4, 5, 6, 7],
};
let req = tonic::Request::new(msg);
client.set(req).await.unwrap();
client.set(req).await?;

Ok(())
}

0 comments on commit 118fb52

Please sign in to comment.