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
112
pub mod grpc_server {
#![allow(unused_qualifications, missing_docs)]
tonic::include_proto!("grpc");
}
pub use grpc_server::rpc_service_server::{RpcService, RpcServiceServer};
pub use grpc_server::{ReadyRequest, ReadyResponse};
use crate::shutdown_signal;
use crate::Config;
use std::fmt::Debug;
use std::net::SocketAddr;
use tonic::transport::Server;
use tonic::{Request, Response, Status};
#[derive(Debug, Default, Copy, Clone)]
pub struct ServerImpl {}
#[cfg(not(feature = "stub_server"))]
#[tonic::async_trait]
impl RpcService for ServerImpl {
async fn is_ready(
&self,
request: Request<ReadyRequest>,
) -> Result<Response<ReadyResponse>, Status> {
grpc_info!("(is_ready) telemetry server.");
grpc_debug!("(is_ready) request: {:?}", request);
let response = ReadyResponse { ready: true };
Ok(Response::new(response))
}
}
pub async fn grpc_server(config: Config, shutdown_rx: Option<tokio::sync::oneshot::Receiver<()>>) {
grpc_debug!("(grpc_server) entry.");
let grpc_port = config.docker_port_grpc;
let full_grpc_addr: SocketAddr = match format!("[::]:{}", grpc_port).parse() {
Ok(addr) => addr,
Err(e) => {
grpc_error!("(grpc_server) Failed to parse gRPC address: {}", e);
return;
}
};
let imp = ServerImpl::default();
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter
.set_serving::<RpcServiceServer<ServerImpl>>()
.await;
grpc_info!(
"(grpc_server) Starting gRPC services on: {}.",
full_grpc_addr
);
match Server::builder()
.add_service(health_service)
.add_service(RpcServiceServer::new(imp))
.serve_with_shutdown(full_grpc_addr, shutdown_signal("grpc", shutdown_rx))
.await
{
Ok(_) => grpc_info!("(grpc_server) gRPC server running at: {}.", full_grpc_addr),
Err(e) => {
grpc_error!("(grpc_server) could not start gRPC server: {}", e);
}
};
}
#[cfg(feature = "stub_server")]
#[tonic::async_trait]
impl RpcService for ServerImpl {
async fn is_ready(
&self,
request: Request<ReadyRequest>,
) -> Result<Response<ReadyResponse>, Status> {
grpc_warn!("(is_ready MOCK) telemetry server.");
grpc_debug!("(is_ready MOCK) request: {:?}", request);
let response = ReadyResponse { ready: true };
Ok(Response::new(response))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_grpc_server_is_ready() {
let imp = ServerImpl::default();
let result = imp.is_ready(Request::new(ReadyRequest {})).await;
assert!(result.is_ok());
let result: ReadyResponse = result.unwrap().into_inner();
assert_eq!(result.ready, true);
}
}