forked from containerd/ttrpc-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-server.rs
110 lines (89 loc) · 2.86 KB
/
async-server.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
// Copyright (c) 2020 Ant Financial
//
// SPDX-License-Identifier: Apache-2.0
//
mod protocols;
#[macro_use]
extern crate log;
use std::sync::Arc;
use log::LevelFilter;
use protocols::r#async::{agent, agent_ttrpc, health, health_ttrpc, types};
use ttrpc::asynchronous::server::*;
use ttrpc::error::{Error, Result};
use ttrpc::ttrpc::{Code, Status};
use async_trait::async_trait;
use tokio;
use tokio::signal::unix::{signal, SignalKind};
struct HealthService;
#[async_trait]
impl health_ttrpc::Health for HealthService {
async fn check(
&self,
_ctx: &::ttrpc::r#async::TtrpcContext,
_req: health::CheckRequest,
) -> Result<health::HealthCheckResponse> {
let mut status = Status::new();
status.set_code(Code::NOT_FOUND);
status.set_message("Just for fun".to_string());
let delay = tokio::time::delay_for(std::time::Duration::from_secs(10));
delay.await;
Err(Error::RpcStatus(status))
}
async fn version(
&self,
_ctx: &::ttrpc::r#async::TtrpcContext,
req: health::CheckRequest,
) -> Result<health::VersionCheckResponse> {
info!("version {:?}", req);
let mut rep = health::VersionCheckResponse::new();
rep.agent_version = "mock.0.1".to_string();
rep.grpc_version = "0.0.1".to_string();
let mut status = Status::new();
status.set_code(Code::NOT_FOUND);
Ok(rep)
}
}
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> {
let mut rp = protobuf::RepeatedField::new();
let mut i = types::Interface::new();
i.set_name("first".to_string());
rp.push(i);
let mut i = types::Interface::new();
i.set_name("second".to_string());
rp.push(i);
let mut i = agent::Interfaces::new();
i.set_Interfaces(rp);
Ok(i)
}
}
#[tokio::main(core_threads = 1)]
async fn main() {
simple_logging::log_to_stderr(LevelFilter::Trace);
let h = Box::new(HealthService {}) as Box<dyn health_ttrpc::Health + Send + Sync>;
let h = Arc::new(h);
let hservice = health_ttrpc::create_health(h);
let a = Box::new(AgentService {}) as Box<dyn agent_ttrpc::AgentService + Send + Sync>;
let a = Arc::new(a);
let aservice = agent_ttrpc::create_agent_service(a);
let server = Server::new()
.bind("unix:///tmp/1")
.unwrap()
.register_service(hservice)
.register_service(aservice);
let mut stream = signal(SignalKind::hangup()).unwrap();
tokio::select! {
_ = stream.recv() => {
println!("signal received")
}
_ = server.start() => {
println!("server exit")
}
};
}