mod control;
mod health;
mod info;
mod metrics;
pub use control::ControlClient;
pub use health::HealthClient;
pub use info::InfoClient;
pub use metrics::MetricsClient;
use redup_bootstrap_grpc::above_threshold;
use crate::factory::Settings;
macro_rules! clients {
($generated:ty, $calls:expr, $channel:expr) => {{
let settings: std::sync::Arc<Settings> = std::sync::Arc::clone(&$calls.settings);
let build = |channel: tonic::transport::Channel| {
let client = <$generated>::new(channel)
.accept_compressed(tonic::codec::CompressionEncoding::Gzip)
.accept_compressed(tonic::codec::CompressionEncoding::Deflate)
.max_decoding_message_size(
usize::try_from(settings.channel.max_receive_message_size())
.unwrap_or(usize::MAX),
);
match settings.channel.max_send_message_size() {
Some(limit) => {
client.max_encoding_message_size(usize::try_from(limit).unwrap_or(usize::MAX))
}
None => client,
}
};
let plain = build($channel.clone());
let compressed = settings
.compression
.client_requests()
.then(|| build($channel).send_compressed(tonic::codec::CompressionEncoding::Gzip));
(plain, compressed, settings)
}};
}
pub(crate) use clients;
pub(crate) fn pick<'a, T>(
plain: &'a mut T,
compressed: &'a mut Option<T>,
settings: &Settings,
size: usize,
) -> &'a mut T {
let threshold = usize::try_from(settings.compression_threshold).unwrap_or(usize::MAX);
match compressed {
Some(client) if above_threshold(size, threshold) => client,
_ => plain,
}
}