use std::{fs, io, path::Path, sync::Arc, time::Duration};
use tonic::transport::{Certificate, ClientTlsConfig};
use tracing::trace;
use crate::grpc_wrapper::grpc_limits::DEFAULT_GRPC_MESSAGE_SIZE_LIMIT_BYTES;
#[derive(Debug, Clone)]
#[cfg_attr(not(feature = "force-exhaustive-all"), non_exhaustive)]
pub struct GrpcOptions {
pub keepalive_interval: Option<Duration>,
pub tls_config: Option<Arc<ClientTlsConfig>>,
pub max_message_size: usize,
}
impl Default for GrpcOptions {
fn default() -> Self {
Self {
keepalive_interval: Some(Duration::from_secs(10)),
tls_config: None,
max_message_size: DEFAULT_GRPC_MESSAGE_SIZE_LIMIT_BYTES,
}
}
}
pub trait HasGrpcOptions {
fn with_grpc_keepalive_interval<I: Into<Option<Duration>>>(mut self, value: I) -> Self
where
Self: Sized,
{
self.grpc_opts_mut().keepalive_interval = value.into();
self
}
fn load_certificate<P: AsRef<Path>>(self, path: P) -> io::Result<Self>
where
Self: Sized,
{
let pem = fs::read_to_string(path)?;
trace!("loaded cert: {pem}");
let ca = Certificate::from_pem(pem);
Ok(self.with_tls_config(ClientTlsConfig::new().ca_certificate(ca)))
}
fn with_tls_config<I: Into<Arc<ClientTlsConfig>>>(mut self, value: I) -> Self
where
Self: Sized,
{
self.grpc_opts_mut().tls_config = Some(value.into());
self
}
fn with_grpc_max_message_size(mut self, value: usize) -> Self
where
Self: Sized,
{
self.grpc_opts_mut().max_message_size = value;
self
}
fn with_grpc_opts(mut self, opts: GrpcOptions) -> Self
where
Self: Sized,
{
*self.grpc_opts_mut() = opts;
self
}
fn grpc_opts_mut(&mut self) -> &mut GrpcOptions;
}