Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
7
crates/churn/src/server/config.rs
Normal file
7
crates/churn/src/server/config.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
#[derive(clap::Args)]
|
||||
pub struct ServerConfig {
|
||||
#[arg(long = "external-host", env = "EXTERNAL_HOST")]
|
||||
pub external_host: String,
|
||||
#[arg(long = "process-host", env = "PROCESS_HOST")]
|
||||
pub process_host: String,
|
||||
}
|
93
crates/churn/src/server/grpc_server.rs
Normal file
93
crates/churn/src/server/grpc_server.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use std::{net::SocketAddr, pin::Pin};
|
||||
|
||||
use anyhow::Context;
|
||||
use futures::Stream;
|
||||
use notmad::{Component, MadError};
|
||||
use tonic::transport::Server;
|
||||
|
||||
use crate::grpc::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct GrpcServer {
|
||||
grpc_host: SocketAddr,
|
||||
}
|
||||
|
||||
impl GrpcServer {
|
||||
pub fn new(grpc_host: SocketAddr) -> Self {
|
||||
Self { grpc_host }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Component for GrpcServer {
|
||||
async fn run(
|
||||
&self,
|
||||
cancellation_token: tokio_util::sync::CancellationToken,
|
||||
) -> Result<(), notmad::MadError> {
|
||||
let task = Server::builder()
|
||||
.add_service(crate::grpc::churn_server::ChurnServer::new(self.clone()))
|
||||
.serve(self.grpc_host);
|
||||
|
||||
tokio::select! {
|
||||
_ = cancellation_token.cancelled() => {},
|
||||
res = task => {
|
||||
res.context("failed to run grpc server").map_err(MadError::Inner)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl crate::grpc::churn_server::Churn for GrpcServer {
|
||||
async fn get_key(
|
||||
&self,
|
||||
request: tonic::Request<GetKeyRequest>,
|
||||
) -> std::result::Result<tonic::Response<GetKeyResponse>, tonic::Status> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
async fn set_key(
|
||||
&self,
|
||||
request: tonic::Request<SetKeyRequest>,
|
||||
) -> std::result::Result<tonic::Response<SetKeyResponse>, tonic::Status> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
#[doc = " Server streaming response type for the ListenEvents method."]
|
||||
type ListenEventsStream =
|
||||
Pin<Box<dyn Stream<Item = Result<ListenEventsResponse, tonic::Status>> + Send>>;
|
||||
|
||||
async fn listen_events(
|
||||
&self,
|
||||
request: tonic::Request<ListenEventsRequest>,
|
||||
) -> std::result::Result<tonic::Response<Self::ListenEventsStream>, tonic::Status> {
|
||||
let (tx, rx) = tokio::sync::mpsc::channel(128);
|
||||
tokio::spawn(async move {
|
||||
let mut interval = tokio::time::interval(std::time::Duration::from_secs(10));
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
|
||||
if let Err(e) = tx
|
||||
.send(Ok(ListenEventsResponse {
|
||||
value: uuid::Uuid::new_v4().to_string(),
|
||||
}))
|
||||
.await
|
||||
{
|
||||
tracing::warn!("failed to send response: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let stream = futures::stream::unfold(rx, |mut msg| async move {
|
||||
let next = msg.recv().await?;
|
||||
|
||||
Some((next, msg))
|
||||
});
|
||||
|
||||
Ok(tonic::Response::new(Box::pin(stream)))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user