forked from containerd/ttrpc-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-client.rs
111 lines (96 loc) · 2.79 KB
/
async-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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2020 Ant Financial
//
// SPDX-License-Identifier: Apache-2.0
//
mod protocols;
use nix::sys::socket::*;
use protocols::r#async::{agent, agent_ttrpc, health, health_ttrpc};
use tokio;
use ttrpc::r#async::Client;
#[tokio::main(core_threads = 1)]
async fn main() {
let path = "/tmp/1";
let fd = socket(
AddressFamily::Unix,
SockType::Stream,
SockFlag::empty(),
None,
)
.unwrap();
let sockaddr = path.to_owned() + &"\x00".to_string();
let sockaddr = UnixAddr::new_abstract(sockaddr.as_bytes()).unwrap();
let sockaddr = SockAddr::Unix(sockaddr);
connect(fd, &sockaddr).unwrap();
let c = Client::new(fd);
let mut hc = health_ttrpc::HealthClient::new(c.clone());
let mut ac = agent_ttrpc::AgentServiceClient::new(c);
let mut thc = hc.clone();
let mut tac = ac.clone();
let now = std::time::Instant::now();
let t1 = tokio::spawn(async move {
let req = health::CheckRequest::new();
println!(
"Green Thread 1 - {} started: {:?}",
"health.check()",
now.elapsed(),
);
println!(
"Green Thread 1 - {} -> {:?} ended: {:?}",
"health.check()",
thc.check(&req, 0).await,
now.elapsed(),
);
});
let t2 = tokio::spawn(async move {
println!(
"Green Thread 2 - {} started: {:?}",
"agent.list_interfaces()",
now.elapsed(),
);
let show = match tac
.list_interfaces(&agent::ListInterfacesRequest::new(), 0)
.await
{
Err(e) => format!("{:?}", e),
Ok(s) => format!("{:?}", s),
};
println!(
"Green Thread 2 - {} -> {} ended: {:?}",
"agent.list_interfaces()",
show,
now.elapsed(),
);
});
let t3 = tokio::spawn(async move {
println!(
"Green Thread 3 - {} started: {:?}",
"agent.online_cpu_mem()",
now.elapsed()
);
let show = match ac
.online_cpu_mem(&agent::OnlineCPUMemRequest::new(), 0)
.await
{
Err(e) => format!("{:?}", e),
Ok(s) => format!("{:?}", s),
};
println!(
"Green Thread 3 - {} -> {} ended: {:?}",
"agent.online_cpu_mem()",
show,
now.elapsed()
);
println!(
"Green Thread 3 - {} started: {:?}",
"health.version()",
now.elapsed()
);
println!(
"Green Thread 3 - {} -> {:?} ended: {:?}",
"health.version()",
hc.version(&health::CheckRequest::new(), 0).await,
now.elapsed()
);
});
let _ = tokio::join!(t1, t2, t3);
}