-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelloworld-grpc-client.rs
More file actions
62 lines (55 loc) · 1.82 KB
/
helloworld-grpc-client.rs
File metadata and controls
62 lines (55 loc) · 1.82 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
use comprehensive::v1::{AssemblyRuntime, Resource, resource};
use comprehensive_grpc::GrpcClient;
use std::sync::Arc;
use std::time::Duration;
// Generated protobufs for gRPC
mod pb {
tonic::include_proto!("comprehensive");
}
#[derive(GrpcClient)]
struct Client(pb::test_client::TestClient<comprehensive_grpc::client::Channel>);
struct GreeterInALoop;
#[derive(clap::Args)]
struct GreeterInALoopArgs {
#[arg(long, value_parser = humantime::parse_duration, default_value = "5s")]
greet_interval: Duration,
}
#[resource]
impl Resource for GreeterInALoop {
fn new(
(client_resource, _): (
Arc<Client>,
std::marker::PhantomData<comprehensive_spiffe::SpiffeTlsProvider>,
),
a: GreeterInALoopArgs,
api: &mut AssemblyRuntime<'_>,
) -> Result<Arc<Self>, std::convert::Infallible> {
let mut client = client_resource.client();
api.set_task(async move {
loop {
println!("{:?}", client.greet(tonic::Request::new(())).await);
tokio::time::sleep(a.greet_interval).await;
}
});
Ok(Arc::new(Self))
}
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
// Will send gRPC greetings at regular intervals using a gRPC client
// with or without TLS depending on flags, and also serve HTTP and/or
// HTTPS (again, depending on flags) at least for metrics.
comprehensive::Assembly::<(
// Including this causes the greeter-in-a-loop to run.
Arc<GreeterInALoop>,
// Serves metrics!
Arc<comprehensive_http::diag::HttpServer>,
)>::new()?
.run()
.await?;
Ok(())
}