use std::time::Duration;
use redup_config::{Config, Report};
const MIN_STREAM_WINDOW: u64 = 64 * 1024;
const MAX_STREAM_WINDOW: u64 = 16 * 1024 * 1024;
const MIN_KEEP_ALIVE: Duration = Duration::from_secs(10);
const SAFE_KEEP_ALIVE: Duration = Duration::from_secs(300);
#[derive(Debug, Clone, PartialEq, Eq, Config)]
#[config(validate)]
pub struct ClientConfig {
connect_timeout: Option<Duration>,
idle_timeout: Option<Duration>,
max_receive_message_size: u64,
max_send_message_size: Option<u64>,
stream_window_size: Option<u64>,
keep_alive: Option<KeepAliveConfig>,
}
impl Default for ClientConfig {
fn default() -> Self {
Self {
connect_timeout: Some(Duration::from_secs(15)),
idle_timeout: None,
max_receive_message_size: 1024 * 1024 * 1024,
max_send_message_size: None,
stream_window_size: Some(16 * 1024 * 1024),
keep_alive: None,
}
}
}
impl ClientConfig {
pub fn connect_timeout(&self) -> Option<Duration> {
self.connect_timeout
}
pub fn idle_timeout(&self) -> Option<Duration> {
self.idle_timeout
}
pub fn max_receive_message_size(&self) -> u64 {
self.max_receive_message_size
}
pub fn max_send_message_size(&self) -> Option<u64> {
self.max_send_message_size
}
pub fn stream_window_size(&self) -> Option<u64> {
self.stream_window_size
}
pub fn keep_alive(&self) -> Option<&KeepAliveConfig> {
self.keep_alive.as_ref()
}
fn validate(&self, report: &mut Report) {
if let Some(window) = self.stream_window_size
&& !(MIN_STREAM_WINDOW..=MAX_STREAM_WINDOW).contains(&window)
{
report.error(
"stream_window_size",
format!(
"The stream window must be between {MIN_STREAM_WINDOW} and {MAX_STREAM_WINDOW} \
bytes, got {window}."
),
);
}
if let Some(keep_alive) = &self.keep_alive {
if keep_alive.ping_delay < MIN_KEEP_ALIVE {
report.error(
"keep_alive",
format!(
"The keepalive period must be at least {} seconds.",
MIN_KEEP_ALIVE.as_secs()
),
);
} else if keep_alive.ping_delay < SAFE_KEEP_ALIVE {
report.warning(
"keep_alive",
format!(
"A keepalive period below {} minutes may make the server answer \
too_many_pings.",
SAFE_KEEP_ALIVE.as_secs() / 60
),
);
}
if keep_alive.ping_timeout.is_zero() {
report.error("keep_alive", "The keepalive timeout must be positive.");
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Config)]
pub struct KeepAliveConfig {
ping_delay: Duration,
ping_timeout: Duration,
policy: KeepAlivePolicy,
}
impl Default for KeepAliveConfig {
fn default() -> Self {
Self {
ping_delay: Duration::from_secs(60),
ping_timeout: Duration::from_secs(20),
policy: KeepAlivePolicy::WithActiveRequests,
}
}
}
impl KeepAliveConfig {
pub fn ping_delay(&self) -> Duration {
self.ping_delay
}
pub fn ping_timeout(&self) -> Duration {
self.ping_timeout
}
pub fn policy(&self) -> KeepAlivePolicy {
self.policy
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Config)]
pub enum KeepAlivePolicy {
#[default]
WithActiveRequests,
WhileIdle,
}
#[cfg(test)]
mod tests {
use redup_config::{Source, load};
use super::{ClientConfig, KeepAlivePolicy};
#[test]
fn defaults_apply_without_a_section() {
let config = ClientConfig::default();
assert_eq!(
config.connect_timeout(),
Some(std::time::Duration::from_secs(15))
);
assert_eq!(config.max_receive_message_size(), 1024 * 1024 * 1024);
assert_eq!(config.stream_window_size(), Some(16 * 1024 * 1024));
assert!(config.keep_alive().is_none());
}
#[test]
fn section_is_read_from_the_common_file() {
let directory = tempfile::tempdir().expect("a temporary directory");
std::fs::write(
directory.path().join("config.yaml"),
"client:\n\
\x20 connectTimeout: 3s\n\
\x20 streamWindowSize: 1Mi\n\
\x20 keepAlive:\n\
\x20 pingDelay: 2m\n\
\x20 policy: whileIdle\n",
)
.expect("the configuration file");
let loaded = load::<ClientConfig>(&Source::directory(directory.path()))
.expect("the configuration is accepted");
let value = loaded.value;
assert_eq!(
value.connect_timeout(),
Some(std::time::Duration::from_secs(3))
);
assert_eq!(value.stream_window_size(), Some(1024 * 1024));
let keep_alive = value.keep_alive().expect("keepalive is on");
assert_eq!(keep_alive.policy(), KeepAlivePolicy::WhileIdle);
}
#[test]
fn too_narrow_a_window_is_rejected() {
let directory = tempfile::tempdir().expect("a temporary directory");
std::fs::write(
directory.path().join("config.yaml"),
"client:\n streamWindowSize: 1ki\n",
)
.expect("the configuration file");
let problems = load::<ClientConfig>(&Source::directory(directory.path()))
.expect_err("the configuration is rejected");
assert!(
problems.to_string().contains("streamWindowSize"),
"{problems}"
);
}
}