-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathasync-server.rs
More file actions
89 lines (73 loc) · 2.33 KB
/
Copy pathasync-server.rs
File metadata and controls
89 lines (73 loc) · 2.33 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
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
// Copyright (c) 2020 Ant Financial
//
// SPDX-License-Identifier: Apache-2.0
//
mod protocols;
mod utils;
use std::sync::Arc;
use log::LevelFilter;
use protocols::asynchronous::{agent, agent_ttrpc, health, health_ttrpc};
use ttrpc::asynchronous::Server;
use async_trait::async_trait;
use tokio::time::sleep;
struct HealthService;
#[async_trait]
impl health_ttrpc::Health for HealthService {
async fn check(
&self,
_ctx: &::ttrpc::r#async::TtrpcContext,
_req: health::CheckRequest,
) -> ttrpc::Result<health::HealthCheckResponse> {
// Mock timeout
sleep(std::time::Duration::from_secs(1)).await;
unreachable!();
}
async fn version(
&self,
_ctx: &::ttrpc::r#async::TtrpcContext,
_req: health::CheckRequest,
) -> ttrpc::Result<health::VersionCheckResponse> {
utils::resp::asynchronous::health_version()
}
}
struct AgentService;
#[async_trait]
impl agent_ttrpc::AgentService for AgentService {
async fn list_interfaces(
&self,
_ctx: &::ttrpc::r#async::TtrpcContext,
_req: agent::ListInterfacesRequest,
) -> ::ttrpc::Result<agent::Interfaces> {
utils::resp::asynchronous::agent_list_interfaces()
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
simple_logging::log_to_stderr(LevelFilter::Trace);
let hservice = health_ttrpc::create_health(Arc::new(HealthService {}));
let aservice = agent_ttrpc::create_agent_service(Arc::new(AgentService {}));
let sock_addr = utils::get_sock_addr();
utils::remove_if_sock_exist(sock_addr).unwrap();
let mut server = Server::new()
.bind(sock_addr)
.unwrap()
.register_service(hservice)
.register_service(aservice);
server.start().await.unwrap();
tokio::select! {
_ = utils::hangup() => {
// test stop_listen -> start
println!("stop listen");
server.stop_listen().await;
println!("start listen");
server.start().await.unwrap();
// hold some time for the new test connection.
sleep(std::time::Duration::from_secs(100)).await;
}
_ = utils::interrupt() => {
// test graceful shutdown
println!("graceful shutdown");
server.shutdown().await.unwrap();
}
};
}