@@ -31,6 +31,7 @@ prost = "0.13.1"
|
||||
prost-types = "0.13.1"
|
||||
chrono = { version = "0.4.38", features = ["serde"] }
|
||||
tokio-stream = "0.1.15"
|
||||
dagger-sdk = "0.11.10"
|
||||
|
||||
[dev-dependencies]
|
||||
tracing-test = "0.2.5"
|
||||
|
24
crates/nodata/src/component.rs
Normal file
24
crates/nodata/src/component.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use crate::dagger_engine::DaggerEngine;
|
||||
|
||||
pub struct Component {
|
||||
dagger_engine: DaggerEngine,
|
||||
}
|
||||
|
||||
impl Component {
|
||||
pub fn new(dagger_engine: DaggerEngine) -> Self {
|
||||
Self { dagger_engine }
|
||||
}
|
||||
pub async fn start_component(&mut self) -> anyhow::Result<()> {
|
||||
self.dagger_engine.start().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn close_component(&mut self) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn handle_transform_message(&mut self) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
260
crates/nodata/src/dagger_engine.rs
Normal file
260
crates/nodata/src/dagger_engine.rs
Normal file
@@ -0,0 +1,260 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Context;
|
||||
use dagger_sdk::{PortForward, ServiceUpOptsBuilder};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::grpc_component::GrpcComponentClient;
|
||||
|
||||
struct DaggerConn {
|
||||
client: dagger_sdk::Query,
|
||||
cancellation_token: CancellationToken,
|
||||
}
|
||||
|
||||
impl DaggerConn {
|
||||
pub fn new(client: &dagger_sdk::Query) -> Self {
|
||||
Self {
|
||||
client: client.clone(),
|
||||
cancellation_token: CancellationToken::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start_container(
|
||||
&self,
|
||||
name: &str,
|
||||
image: &str,
|
||||
cancellation_token: CancellationToken,
|
||||
) -> anyhow::Result<DaggerContainer> {
|
||||
let client = self.client.clone();
|
||||
|
||||
// Bind to the os, and let it select a random port above > 30000
|
||||
let component_listener = match TcpListener::bind("127.0.0.1:0").await {
|
||||
Ok(listener) => listener,
|
||||
Err(e) => {
|
||||
tracing::warn!(error = e.to_string(), "failed to allocate port");
|
||||
anyhow::bail!(e);
|
||||
}
|
||||
};
|
||||
|
||||
let port = component_listener
|
||||
.local_addr()
|
||||
.context("failed to find a valid random port, you may've run out")?
|
||||
.port();
|
||||
|
||||
// Let the blocking container run in the background, maintained by the cancellation token handle in the dagger container
|
||||
let container_name = name.to_string();
|
||||
let container_token = cancellation_token.child_token();
|
||||
let container_image = image.to_string();
|
||||
|
||||
tokio::spawn(async move {
|
||||
tokio::select! {
|
||||
_ = container_token.cancelled() => {},
|
||||
res = spawn_container(&client, &container_image, port) => {
|
||||
if let Err(e) = res {
|
||||
tracing::warn!(error=e.to_string(), "container {} failed", container_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let grpc = match GrpcComponentClient::new(format!("127.0.0.1:{}", port)).await {
|
||||
Ok(grpc) => grpc,
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
error = e.to_string(),
|
||||
"failed to bootstrap grpc component, service may not be up yet."
|
||||
);
|
||||
|
||||
anyhow::bail!(e);
|
||||
}
|
||||
};
|
||||
|
||||
match grpc.ping().await {
|
||||
Ok(_) => {
|
||||
// TODO: Finally send something back to the caller
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
error = e.to_string(),
|
||||
"failed to ping grpc server, service may not be up yet."
|
||||
);
|
||||
|
||||
anyhow::bail!("failed to ping container");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(DaggerContainer {
|
||||
name: name.into(),
|
||||
image: image.into(),
|
||||
handle: cancellation_token,
|
||||
url: format!("127.0.0.1:{}", port),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DaggerEngine {
|
||||
cancellation: CancellationToken,
|
||||
|
||||
dagger_conn: Arc<tokio::sync::Mutex<Option<DaggerConn>>>,
|
||||
}
|
||||
|
||||
impl DaggerEngine {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
cancellation: CancellationToken::default(),
|
||||
dagger_conn: Arc::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn start(&mut self) -> anyhow::Result<()> {
|
||||
let cancellation = self.cancellation.child_token();
|
||||
let dagger_conn = self.dagger_conn.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
let mut dagger_conn_handle = dagger_conn.lock().await;
|
||||
|
||||
if dagger_conn_handle.is_none() {
|
||||
let mut dagger_conn = dagger_client(cancellation.child_token()).await;
|
||||
|
||||
if let Some(dagger_conn) = dagger_conn.recv().await {
|
||||
*dagger_conn_handle = Some(dagger_conn);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn stop(&mut self) -> anyhow::Result<()> {
|
||||
self.cancellation.cancel();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn start_container(
|
||||
&self,
|
||||
name: impl Into<String>,
|
||||
image: impl Into<String>,
|
||||
) -> anyhow::Result<DaggerContainer> {
|
||||
let name = name.into();
|
||||
let image = image.into();
|
||||
|
||||
for i in 0..5 {
|
||||
let channel = self.dagger_conn.lock().await;
|
||||
if channel.is_some() {
|
||||
// TODO: fill out
|
||||
|
||||
match channel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.start_container(&name, &image, self.cancellation.child_token())
|
||||
.await
|
||||
{
|
||||
Ok(container) => return Ok(container),
|
||||
Err(e) => {
|
||||
tracing::info!(
|
||||
container_name = name,
|
||||
error = e.to_string(),
|
||||
"failed to get container"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_secs(i)).await
|
||||
}
|
||||
|
||||
anyhow::bail!("failed to find a valid channel, aborting")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DaggerContainer {
|
||||
name: String,
|
||||
handle: CancellationToken,
|
||||
image: String,
|
||||
url: String,
|
||||
}
|
||||
|
||||
impl DaggerContainer {
|
||||
pub async fn grpc_handle(&self) -> anyhow::Result<GrpcComponentClient> {
|
||||
let client = GrpcComponentClient::new(&self.url).await?;
|
||||
|
||||
Ok(client)
|
||||
}
|
||||
}
|
||||
|
||||
async fn dagger_client(cancellation: CancellationToken) -> tokio::sync::mpsc::Receiver<DaggerConn> {
|
||||
let (tx, rx) = tokio::sync::mpsc::channel::<DaggerConn>(1);
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = dagger_sdk::connect(|client| async move {
|
||||
tx.send(DaggerConn::new(&client)).await?;
|
||||
|
||||
cancellation.cancelled().await;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
{
|
||||
tracing::warn!(
|
||||
error = e.to_string(),
|
||||
"failed to handle dagger connect, components may not be executed as they should "
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
rx
|
||||
}
|
||||
|
||||
async fn spawn_container(
|
||||
client: &dagger_sdk::Query,
|
||||
image: &str,
|
||||
outer_port: u16,
|
||||
) -> anyhow::Result<()> {
|
||||
let service = client
|
||||
.container()
|
||||
.from(image)
|
||||
.with_exposed_port(80)
|
||||
.as_service();
|
||||
|
||||
service
|
||||
.up_opts(
|
||||
ServiceUpOptsBuilder::default()
|
||||
.ports(vec![PortForward {
|
||||
backend: 80,
|
||||
frontend: outer_port as isize,
|
||||
protocol: dagger_sdk::NetworkProtocol::Tcp,
|
||||
}])
|
||||
.build()?,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use tracing_test::traced_test;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[traced_test]
|
||||
async fn test_can_use_dagger_engine() -> anyhow::Result<()> {
|
||||
let mut dagger_engine = DaggerEngine::new();
|
||||
|
||||
tracing::info!("starting dagger engine");
|
||||
dagger_engine.start().await?;
|
||||
|
||||
tracing::info!("starting dagger container");
|
||||
let container = dagger_engine
|
||||
.start_container("some_name", "nginx:latest")
|
||||
.await?;
|
||||
|
||||
tracing::info!("getting grpc handle");
|
||||
let _ = container.grpc_handle().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
@@ -2,60 +2,78 @@
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct PublishEventRequest {
|
||||
#[prost(string, tag = "1")]
|
||||
#[prost(string, tag="1")]
|
||||
pub topic: ::prost::alloc::string::String,
|
||||
#[prost(message, optional, tag = "2")]
|
||||
#[prost(message, optional, tag="2")]
|
||||
pub published: ::core::option::Option<::prost_types::Timestamp>,
|
||||
#[prost(string, tag = "3")]
|
||||
#[prost(string, tag="3")]
|
||||
pub key: ::prost::alloc::string::String,
|
||||
#[prost(bytes = "vec", tag = "4")]
|
||||
#[prost(bytes="vec", tag="4")]
|
||||
pub value: ::prost::alloc::vec::Vec<u8>,
|
||||
#[prost(string, optional, tag = "5")]
|
||||
#[prost(string, optional, tag="5")]
|
||||
pub id: ::core::option::Option<::prost::alloc::string::String>,
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct PublishEventResponse {}
|
||||
pub struct PublishEventResponse {
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct GetTopicsRequest {}
|
||||
pub struct GetTopicsRequest {
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct GetTopicsResponse {
|
||||
#[prost(string, repeated, tag = "1")]
|
||||
#[prost(string, repeated, tag="1")]
|
||||
pub topics: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct GetKeysRequest {
|
||||
#[prost(string, tag = "1")]
|
||||
#[prost(string, tag="1")]
|
||||
pub topic: ::prost::alloc::string::String,
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct GetKeysResponse {
|
||||
#[prost(string, repeated, tag = "1")]
|
||||
#[prost(string, repeated, tag="1")]
|
||||
pub keys: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct SubscribeRequest {
|
||||
#[prost(string, tag = "1")]
|
||||
#[prost(string, tag="1")]
|
||||
pub topic: ::prost::alloc::string::String,
|
||||
#[prost(string, tag = "2")]
|
||||
#[prost(string, tag="2")]
|
||||
pub key: ::prost::alloc::string::String,
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct SubscribeResponse {
|
||||
#[prost(string, tag = "1")]
|
||||
#[prost(string, tag="1")]
|
||||
pub id: ::prost::alloc::string::String,
|
||||
#[prost(message, optional, tag = "2")]
|
||||
#[prost(message, optional, tag="2")]
|
||||
pub published: ::core::option::Option<::prost_types::Timestamp>,
|
||||
#[prost(uint64, tag = "3")]
|
||||
#[prost(uint64, tag="3")]
|
||||
pub offset: u64,
|
||||
#[prost(bytes = "vec", tag = "4")]
|
||||
#[prost(bytes="vec", tag="4")]
|
||||
pub value: ::prost::alloc::vec::Vec<u8>,
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct HandleMsgRequest {
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct HandleMsgResponse {
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct PingRequest {
|
||||
}
|
||||
#[allow(clippy::derive_partial_eq_without_eq)]
|
||||
#[derive(Clone, PartialEq, ::prost::Message)]
|
||||
pub struct PingResponse {
|
||||
}
|
||||
include!("nodata.v1.tonic.rs");
|
||||
// @@protoc_insertion_point(module)
|
||||
// @@protoc_insertion_point(module)
|
@@ -527,3 +527,373 @@ pub mod no_data_service_server {
|
||||
const NAME: &'static str = "nodata.v1.NoDataService";
|
||||
}
|
||||
}
|
||||
/// Generated client implementations.
|
||||
pub mod no_data_component_client {
|
||||
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
|
||||
use tonic::codegen::*;
|
||||
use tonic::codegen::http::Uri;
|
||||
///
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct NoDataComponentClient<T> {
|
||||
inner: tonic::client::Grpc<T>,
|
||||
}
|
||||
impl NoDataComponentClient<tonic::transport::Channel> {
|
||||
/// Attempt to create a new client by connecting to a given endpoint.
|
||||
pub async fn connect<D>(dst: D) -> Result<Self, tonic::transport::Error>
|
||||
where
|
||||
D: TryInto<tonic::transport::Endpoint>,
|
||||
D::Error: Into<StdError>,
|
||||
{
|
||||
let conn = tonic::transport::Endpoint::new(dst)?.connect().await?;
|
||||
Ok(Self::new(conn))
|
||||
}
|
||||
}
|
||||
impl<T> NoDataComponentClient<T>
|
||||
where
|
||||
T: tonic::client::GrpcService<tonic::body::BoxBody>,
|
||||
T::Error: Into<StdError>,
|
||||
T::ResponseBody: Body<Data = Bytes> + Send + 'static,
|
||||
<T::ResponseBody as Body>::Error: Into<StdError> + Send,
|
||||
{
|
||||
pub fn new(inner: T) -> Self {
|
||||
let inner = tonic::client::Grpc::new(inner);
|
||||
Self { inner }
|
||||
}
|
||||
pub fn with_origin(inner: T, origin: Uri) -> Self {
|
||||
let inner = tonic::client::Grpc::with_origin(inner, origin);
|
||||
Self { inner }
|
||||
}
|
||||
pub fn with_interceptor<F>(
|
||||
inner: T,
|
||||
interceptor: F,
|
||||
) -> NoDataComponentClient<InterceptedService<T, F>>
|
||||
where
|
||||
F: tonic::service::Interceptor,
|
||||
T::ResponseBody: Default,
|
||||
T: tonic::codegen::Service<
|
||||
http::Request<tonic::body::BoxBody>,
|
||||
Response = http::Response<
|
||||
<T as tonic::client::GrpcService<tonic::body::BoxBody>>::ResponseBody,
|
||||
>,
|
||||
>,
|
||||
<T as tonic::codegen::Service<
|
||||
http::Request<tonic::body::BoxBody>,
|
||||
>>::Error: Into<StdError> + Send + Sync,
|
||||
{
|
||||
NoDataComponentClient::new(InterceptedService::new(inner, interceptor))
|
||||
}
|
||||
/// Compress requests with the given encoding.
|
||||
///
|
||||
/// This requires the server to support it otherwise it might respond with an
|
||||
/// error.
|
||||
#[must_use]
|
||||
pub fn send_compressed(mut self, encoding: CompressionEncoding) -> Self {
|
||||
self.inner = self.inner.send_compressed(encoding);
|
||||
self
|
||||
}
|
||||
/// Enable decompressing responses.
|
||||
#[must_use]
|
||||
pub fn accept_compressed(mut self, encoding: CompressionEncoding) -> Self {
|
||||
self.inner = self.inner.accept_compressed(encoding);
|
||||
self
|
||||
}
|
||||
/// Limits the maximum size of a decoded message.
|
||||
///
|
||||
/// Default: `4MB`
|
||||
#[must_use]
|
||||
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
|
||||
self.inner = self.inner.max_decoding_message_size(limit);
|
||||
self
|
||||
}
|
||||
/// Limits the maximum size of an encoded message.
|
||||
///
|
||||
/// Default: `usize::MAX`
|
||||
#[must_use]
|
||||
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
|
||||
self.inner = self.inner.max_encoding_message_size(limit);
|
||||
self
|
||||
}
|
||||
///
|
||||
pub async fn transform_msg(
|
||||
&mut self,
|
||||
request: impl tonic::IntoRequest<super::HandleMsgRequest>,
|
||||
) -> std::result::Result<
|
||||
tonic::Response<super::HandleMsgResponse>,
|
||||
tonic::Status,
|
||||
> {
|
||||
self.inner
|
||||
.ready()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tonic::Status::new(
|
||||
tonic::Code::Unknown,
|
||||
format!("Service was not ready: {}", e.into()),
|
||||
)
|
||||
})?;
|
||||
let codec = tonic::codec::ProstCodec::default();
|
||||
let path = http::uri::PathAndQuery::from_static(
|
||||
"/nodata.v1.NoDataComponent/TransformMsg",
|
||||
);
|
||||
let mut req = request.into_request();
|
||||
req.extensions_mut()
|
||||
.insert(GrpcMethod::new("nodata.v1.NoDataComponent", "TransformMsg"));
|
||||
self.inner.unary(req, path, codec).await
|
||||
}
|
||||
///
|
||||
pub async fn ping(
|
||||
&mut self,
|
||||
request: impl tonic::IntoRequest<super::PingRequest>,
|
||||
) -> std::result::Result<tonic::Response<super::PingResponse>, tonic::Status> {
|
||||
self.inner
|
||||
.ready()
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tonic::Status::new(
|
||||
tonic::Code::Unknown,
|
||||
format!("Service was not ready: {}", e.into()),
|
||||
)
|
||||
})?;
|
||||
let codec = tonic::codec::ProstCodec::default();
|
||||
let path = http::uri::PathAndQuery::from_static(
|
||||
"/nodata.v1.NoDataComponent/Ping",
|
||||
);
|
||||
let mut req = request.into_request();
|
||||
req.extensions_mut()
|
||||
.insert(GrpcMethod::new("nodata.v1.NoDataComponent", "Ping"));
|
||||
self.inner.unary(req, path, codec).await
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Generated server implementations.
|
||||
pub mod no_data_component_server {
|
||||
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
|
||||
use tonic::codegen::*;
|
||||
/// Generated trait containing gRPC methods that should be implemented for use with NoDataComponentServer.
|
||||
#[async_trait]
|
||||
pub trait NoDataComponent: Send + Sync + 'static {
|
||||
///
|
||||
async fn transform_msg(
|
||||
&self,
|
||||
request: tonic::Request<super::HandleMsgRequest>,
|
||||
) -> std::result::Result<
|
||||
tonic::Response<super::HandleMsgResponse>,
|
||||
tonic::Status,
|
||||
>;
|
||||
///
|
||||
async fn ping(
|
||||
&self,
|
||||
request: tonic::Request<super::PingRequest>,
|
||||
) -> std::result::Result<tonic::Response<super::PingResponse>, tonic::Status>;
|
||||
}
|
||||
///
|
||||
#[derive(Debug)]
|
||||
pub struct NoDataComponentServer<T: NoDataComponent> {
|
||||
inner: _Inner<T>,
|
||||
accept_compression_encodings: EnabledCompressionEncodings,
|
||||
send_compression_encodings: EnabledCompressionEncodings,
|
||||
max_decoding_message_size: Option<usize>,
|
||||
max_encoding_message_size: Option<usize>,
|
||||
}
|
||||
struct _Inner<T>(Arc<T>);
|
||||
impl<T: NoDataComponent> NoDataComponentServer<T> {
|
||||
pub fn new(inner: T) -> Self {
|
||||
Self::from_arc(Arc::new(inner))
|
||||
}
|
||||
pub fn from_arc(inner: Arc<T>) -> Self {
|
||||
let inner = _Inner(inner);
|
||||
Self {
|
||||
inner,
|
||||
accept_compression_encodings: Default::default(),
|
||||
send_compression_encodings: Default::default(),
|
||||
max_decoding_message_size: None,
|
||||
max_encoding_message_size: None,
|
||||
}
|
||||
}
|
||||
pub fn with_interceptor<F>(
|
||||
inner: T,
|
||||
interceptor: F,
|
||||
) -> InterceptedService<Self, F>
|
||||
where
|
||||
F: tonic::service::Interceptor,
|
||||
{
|
||||
InterceptedService::new(Self::new(inner), interceptor)
|
||||
}
|
||||
/// Enable decompressing requests with the given encoding.
|
||||
#[must_use]
|
||||
pub fn accept_compressed(mut self, encoding: CompressionEncoding) -> Self {
|
||||
self.accept_compression_encodings.enable(encoding);
|
||||
self
|
||||
}
|
||||
/// Compress responses with the given encoding, if the client supports it.
|
||||
#[must_use]
|
||||
pub fn send_compressed(mut self, encoding: CompressionEncoding) -> Self {
|
||||
self.send_compression_encodings.enable(encoding);
|
||||
self
|
||||
}
|
||||
/// Limits the maximum size of a decoded message.
|
||||
///
|
||||
/// Default: `4MB`
|
||||
#[must_use]
|
||||
pub fn max_decoding_message_size(mut self, limit: usize) -> Self {
|
||||
self.max_decoding_message_size = Some(limit);
|
||||
self
|
||||
}
|
||||
/// Limits the maximum size of an encoded message.
|
||||
///
|
||||
/// Default: `usize::MAX`
|
||||
#[must_use]
|
||||
pub fn max_encoding_message_size(mut self, limit: usize) -> Self {
|
||||
self.max_encoding_message_size = Some(limit);
|
||||
self
|
||||
}
|
||||
}
|
||||
impl<T, B> tonic::codegen::Service<http::Request<B>> for NoDataComponentServer<T>
|
||||
where
|
||||
T: NoDataComponent,
|
||||
B: Body + Send + 'static,
|
||||
B::Error: Into<StdError> + Send + 'static,
|
||||
{
|
||||
type Response = http::Response<tonic::body::BoxBody>;
|
||||
type Error = std::convert::Infallible;
|
||||
type Future = BoxFuture<Self::Response, Self::Error>;
|
||||
fn poll_ready(
|
||||
&mut self,
|
||||
_cx: &mut Context<'_>,
|
||||
) -> Poll<std::result::Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
fn call(&mut self, req: http::Request<B>) -> Self::Future {
|
||||
let inner = self.inner.clone();
|
||||
match req.uri().path() {
|
||||
"/nodata.v1.NoDataComponent/TransformMsg" => {
|
||||
#[allow(non_camel_case_types)]
|
||||
struct TransformMsgSvc<T: NoDataComponent>(pub Arc<T>);
|
||||
impl<
|
||||
T: NoDataComponent,
|
||||
> tonic::server::UnaryService<super::HandleMsgRequest>
|
||||
for TransformMsgSvc<T> {
|
||||
type Response = super::HandleMsgResponse;
|
||||
type Future = BoxFuture<
|
||||
tonic::Response<Self::Response>,
|
||||
tonic::Status,
|
||||
>;
|
||||
fn call(
|
||||
&mut self,
|
||||
request: tonic::Request<super::HandleMsgRequest>,
|
||||
) -> Self::Future {
|
||||
let inner = Arc::clone(&self.0);
|
||||
let fut = async move {
|
||||
<T as NoDataComponent>::transform_msg(&inner, request).await
|
||||
};
|
||||
Box::pin(fut)
|
||||
}
|
||||
}
|
||||
let accept_compression_encodings = self.accept_compression_encodings;
|
||||
let send_compression_encodings = self.send_compression_encodings;
|
||||
let max_decoding_message_size = self.max_decoding_message_size;
|
||||
let max_encoding_message_size = self.max_encoding_message_size;
|
||||
let inner = self.inner.clone();
|
||||
let fut = async move {
|
||||
let inner = inner.0;
|
||||
let method = TransformMsgSvc(inner);
|
||||
let codec = tonic::codec::ProstCodec::default();
|
||||
let mut grpc = tonic::server::Grpc::new(codec)
|
||||
.apply_compression_config(
|
||||
accept_compression_encodings,
|
||||
send_compression_encodings,
|
||||
)
|
||||
.apply_max_message_size_config(
|
||||
max_decoding_message_size,
|
||||
max_encoding_message_size,
|
||||
);
|
||||
let res = grpc.unary(method, req).await;
|
||||
Ok(res)
|
||||
};
|
||||
Box::pin(fut)
|
||||
}
|
||||
"/nodata.v1.NoDataComponent/Ping" => {
|
||||
#[allow(non_camel_case_types)]
|
||||
struct PingSvc<T: NoDataComponent>(pub Arc<T>);
|
||||
impl<
|
||||
T: NoDataComponent,
|
||||
> tonic::server::UnaryService<super::PingRequest> for PingSvc<T> {
|
||||
type Response = super::PingResponse;
|
||||
type Future = BoxFuture<
|
||||
tonic::Response<Self::Response>,
|
||||
tonic::Status,
|
||||
>;
|
||||
fn call(
|
||||
&mut self,
|
||||
request: tonic::Request<super::PingRequest>,
|
||||
) -> Self::Future {
|
||||
let inner = Arc::clone(&self.0);
|
||||
let fut = async move {
|
||||
<T as NoDataComponent>::ping(&inner, request).await
|
||||
};
|
||||
Box::pin(fut)
|
||||
}
|
||||
}
|
||||
let accept_compression_encodings = self.accept_compression_encodings;
|
||||
let send_compression_encodings = self.send_compression_encodings;
|
||||
let max_decoding_message_size = self.max_decoding_message_size;
|
||||
let max_encoding_message_size = self.max_encoding_message_size;
|
||||
let inner = self.inner.clone();
|
||||
let fut = async move {
|
||||
let inner = inner.0;
|
||||
let method = PingSvc(inner);
|
||||
let codec = tonic::codec::ProstCodec::default();
|
||||
let mut grpc = tonic::server::Grpc::new(codec)
|
||||
.apply_compression_config(
|
||||
accept_compression_encodings,
|
||||
send_compression_encodings,
|
||||
)
|
||||
.apply_max_message_size_config(
|
||||
max_decoding_message_size,
|
||||
max_encoding_message_size,
|
||||
);
|
||||
let res = grpc.unary(method, req).await;
|
||||
Ok(res)
|
||||
};
|
||||
Box::pin(fut)
|
||||
}
|
||||
_ => {
|
||||
Box::pin(async move {
|
||||
Ok(
|
||||
http::Response::builder()
|
||||
.status(200)
|
||||
.header("grpc-status", "12")
|
||||
.header("content-type", "application/grpc")
|
||||
.body(empty_body())
|
||||
.unwrap(),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: NoDataComponent> Clone for NoDataComponentServer<T> {
|
||||
fn clone(&self) -> Self {
|
||||
let inner = self.inner.clone();
|
||||
Self {
|
||||
inner,
|
||||
accept_compression_encodings: self.accept_compression_encodings,
|
||||
send_compression_encodings: self.send_compression_encodings,
|
||||
max_decoding_message_size: self.max_decoding_message_size,
|
||||
max_encoding_message_size: self.max_encoding_message_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: NoDataComponent> Clone for _Inner<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(Arc::clone(&self.0))
|
||||
}
|
||||
}
|
||||
impl<T: std::fmt::Debug> std::fmt::Debug for _Inner<T> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self.0)
|
||||
}
|
||||
}
|
||||
impl<T: NoDataComponent> tonic::server::NamedService for NoDataComponentServer<T> {
|
||||
const NAME: &'static str = "nodata.v1.NoDataComponent";
|
||||
}
|
||||
}
|
||||
|
26
crates/nodata/src/grpc_component.rs
Normal file
26
crates/nodata/src/grpc_component.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use no_data_component_client::NoDataComponentClient;
|
||||
use tonic::transport::Channel;
|
||||
|
||||
include!("gen/nodata.v1.rs");
|
||||
|
||||
pub struct GrpcComponentClient {
|
||||
host_name: String,
|
||||
}
|
||||
|
||||
impl GrpcComponentClient {
|
||||
pub async fn new(host_name: impl Into<String>) -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
host_name: host_name.into(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn ping(&self) -> anyhow::Result<Option<()>> {
|
||||
self.create_client().await?.ping(PingRequest {}).await?;
|
||||
|
||||
Ok(Some(()))
|
||||
}
|
||||
|
||||
pub(crate) async fn create_client(&self) -> anyhow::Result<NoDataComponentClient<Channel>> {
|
||||
Ok(NoDataComponentClient::connect(self.host_name.clone()).await?)
|
||||
}
|
||||
}
|
@@ -1,8 +1,11 @@
|
||||
mod broker;
|
||||
mod dagger_engine;
|
||||
mod grpc;
|
||||
mod grpc_component;
|
||||
mod http;
|
||||
mod state;
|
||||
|
||||
mod component;
|
||||
mod services;
|
||||
|
||||
use std::net::SocketAddr;
|
||||
|
Reference in New Issue
Block a user