-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rs
73 lines (63 loc) · 2.42 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use chrono::Utc;
use std::str::FromStr;
use tonic::transport::Channel;
use crate::proto::action_client::ActionClient;
use crate::proto::{Acknowledgement, GetResponse, RemoveRequest};
use crate::proto::{GetRequest, SetRequest};
pub use crate::replication::ReplicatedServer;
use crate::Result;
/// A client used for interacting with the [`KvStore`] via gRPC requests.
#[tonic::async_trait]
pub trait Client {
async fn get(&mut self, key: String) -> anyhow::Result<Option<GetResponse>>;
async fn set(&mut self, key: String, value: String) -> anyhow::Result<Acknowledgement>;
async fn remove(&mut self, key: String) -> anyhow::Result<Acknowledgement>;
}
/// A [`RemoteNodeClient`] is for interacting with a cache node over the network.
///
/// Owing to this, it can be used for both the user-facing interaction with the
/// service (client/server model) and when dealing with inter-node communication
/// for replication.
#[derive(Clone)]
pub struct RemoteNodeClient {
/// Inner gRPC client for actions that can be taken.
inner: ActionClient<Channel>,
/// Address of the cache server.
pub addr: String,
}
impl RemoteNodeClient {
/// Provides a new [`RemoteNodeClient`].
///
/// The channel used for the connection is not utilised until first use.
pub async fn new(addr: String) -> Result<Self> {
let ep = tonic::transport::Endpoint::from_str(&format!("http://{}", addr))?;
let inner = ActionClient::new(ep.connect_lazy());
Ok(Self { inner, addr })
}
}
#[tonic::async_trait]
impl Client for RemoteNodeClient {
async fn set(&mut self, key: String, value: String) -> anyhow::Result<Acknowledgement> {
let req = tonic::Request::new(SetRequest {
key,
value,
timestamp: Utc::now().timestamp(),
});
let result = self.inner.set(req).await?;
Ok(result.into_inner())
}
async fn get(&mut self, key: String) -> anyhow::Result<Option<GetResponse>> {
let req = tonic::Request::new(GetRequest { key });
let response = self.inner.get(req).await?.into_inner();
if response.value.is_some() {
Ok(Some(response))
} else {
Ok(None)
}
}
async fn remove(&mut self, key: String) -> anyhow::Result<Acknowledgement> {
let req = tonic::Request::new(RemoveRequest { key });
let result = self.inner.remove(req).await?;
Ok(result.into_inner())
}
}