-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathclient.rs
More file actions
61 lines (50 loc) · 1.57 KB
/
client.rs
File metadata and controls
61 lines (50 loc) · 1.57 KB
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
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
use std::any::Any;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use derive_new::new;
use tonic::transport::Channel;
use super::Request;
use crate::proto::tikvpb::tikv_client::TikvClient;
use crate::Config;
use crate::Result;
use crate::SecurityManager;
/// A trait for connecting to TiKV stores.
#[async_trait]
pub trait KvConnect: Sized + Send + Sync + 'static {
type KvClient: KvClient + Clone + Send + Sync + 'static;
async fn connect(&self, address: &str) -> Result<Self::KvClient>;
}
#[derive(new, Clone)]
pub struct TikvConnect {
security_mgr: Arc<SecurityManager>,
config: Config,
}
#[async_trait]
impl KvConnect for TikvConnect {
type KvClient = KvRpcClient;
async fn connect(&self, address: &str) -> Result<KvRpcClient> {
self.security_mgr
.connect(address, TikvClient::new, &self.config)
.await
.map(|c| KvRpcClient::new(c, self.config.timeout))
}
}
#[async_trait]
pub trait KvClient {
async fn dispatch(&self, req: &dyn Request) -> Result<Box<dyn Any>>;
}
/// This client handles requests for a single TiKV node. It converts the data
/// types and abstractions of the client program into the grpc data types.
#[derive(new, Clone)]
pub struct KvRpcClient {
rpc_client: TikvClient<Channel>,
timeout: Duration,
}
#[async_trait]
impl KvClient for KvRpcClient {
async fn dispatch(&self, request: &dyn Request) -> Result<Box<dyn Any>> {
request.dispatch(&self.rpc_client, self.timeout).await
}
}