redup_bootstrap_client/
config.rs1use std::time::Duration;
4
5use redup_config::{Config, Report};
6
7const MIN_STREAM_WINDOW: u64 = 64 * 1024;
9const MAX_STREAM_WINDOW: u64 = 16 * 1024 * 1024;
11const MIN_KEEP_ALIVE: Duration = Duration::from_secs(10);
13const SAFE_KEEP_ALIVE: Duration = Duration::from_secs(300);
15
16#[derive(Debug, Clone, PartialEq, Eq, Config)]
18#[config(validate)]
19pub struct ClientConfig {
20 connect_timeout: Option<Duration>,
23 idle_timeout: Option<Duration>,
28 max_receive_message_size: u64,
30 max_send_message_size: Option<u64>,
32 stream_window_size: Option<u64>,
36 keep_alive: Option<KeepAliveConfig>,
38}
39
40impl Default for ClientConfig {
41 fn default() -> Self {
42 Self {
43 connect_timeout: Some(Duration::from_secs(10)),
44 idle_timeout: None,
45 max_receive_message_size: 1024 * 1024 * 1024,
46 max_send_message_size: None,
47 stream_window_size: Some(16 * 1024 * 1024),
48 keep_alive: None,
49 }
50 }
51}
52
53impl ClientConfig {
54 pub fn connect_timeout(&self) -> Option<Duration> {
56 self.connect_timeout
57 }
58
59 pub fn idle_timeout(&self) -> Option<Duration> {
61 self.idle_timeout
62 }
63
64 pub fn max_receive_message_size(&self) -> u64 {
66 self.max_receive_message_size
67 }
68
69 pub fn max_send_message_size(&self) -> Option<u64> {
71 self.max_send_message_size
72 }
73
74 pub fn stream_window_size(&self) -> Option<u64> {
76 self.stream_window_size
77 }
78
79 pub fn keep_alive(&self) -> Option<&KeepAliveConfig> {
81 self.keep_alive.as_ref()
82 }
83
84 fn validate(&self, report: &mut Report) {
85 if let Some(window) = self.stream_window_size
86 && !(MIN_STREAM_WINDOW..=MAX_STREAM_WINDOW).contains(&window)
87 {
88 report.error(
89 "stream_window_size",
90 format!(
91 "The stream window must be between {MIN_STREAM_WINDOW} and {MAX_STREAM_WINDOW} \
92 bytes, got {window}."
93 ),
94 );
95 }
96 if let Some(keep_alive) = &self.keep_alive {
97 if keep_alive.ping_delay < MIN_KEEP_ALIVE {
98 report.error(
99 "keep_alive",
100 format!(
101 "The keepalive period must be at least {} seconds.",
102 MIN_KEEP_ALIVE.as_secs()
103 ),
104 );
105 } else if keep_alive.ping_delay < SAFE_KEEP_ALIVE {
106 report.warning(
107 "keep_alive",
108 format!(
109 "A keepalive period below {} minutes may make the server answer \
110 too_many_pings.",
111 SAFE_KEEP_ALIVE.as_secs() / 60
112 ),
113 );
114 }
115 if keep_alive.ping_timeout.is_zero() {
116 report.error("keep_alive", "The keepalive timeout must be positive.");
117 }
118 }
119 }
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq, Config)]
124pub struct KeepAliveConfig {
125 ping_delay: Duration,
127 ping_timeout: Duration,
129 policy: KeepAlivePolicy,
131}
132
133impl Default for KeepAliveConfig {
134 fn default() -> Self {
135 Self {
136 ping_delay: Duration::from_secs(60),
137 ping_timeout: Duration::from_secs(20),
138 policy: KeepAlivePolicy::WithActiveRequests,
139 }
140 }
141}
142
143impl KeepAliveConfig {
144 pub fn ping_delay(&self) -> Duration {
146 self.ping_delay
147 }
148
149 pub fn ping_timeout(&self) -> Duration {
151 self.ping_timeout
152 }
153
154 pub fn policy(&self) -> KeepAlivePolicy {
156 self.policy
157 }
158}
159
160#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Config)]
162pub enum KeepAlivePolicy {
163 #[default]
165 WithActiveRequests,
166 WhileIdle,
168}
169
170#[cfg(test)]
171mod tests {
172 use redup_config::{Source, load};
173
174 use super::{ClientConfig, KeepAlivePolicy};
175
176 #[test]
177 fn defaults_apply_without_a_section() {
178 let config = ClientConfig::default();
179 assert_eq!(
180 config.connect_timeout(),
181 Some(std::time::Duration::from_secs(10))
182 );
183 assert_eq!(config.max_receive_message_size(), 1024 * 1024 * 1024);
184 assert_eq!(config.stream_window_size(), Some(16 * 1024 * 1024));
185 assert!(config.keep_alive().is_none());
186 }
187
188 #[test]
189 fn section_is_read_from_the_common_file() {
190 let directory = tempfile::tempdir().expect("a temporary directory");
191 std::fs::write(
192 directory.path().join("config.yaml"),
193 "client:\n\
194 \x20 connectTimeout: 3s\n\
195 \x20 streamWindowSize: 1Mi\n\
196 \x20 keepAlive:\n\
197 \x20 pingDelay: 2m\n\
198 \x20 policy: whileIdle\n",
199 )
200 .expect("the configuration file");
201 let loaded = load::<ClientConfig>(&Source::directory(directory.path()))
202 .expect("the configuration is accepted");
203 let value = loaded.value;
204 assert_eq!(
205 value.connect_timeout(),
206 Some(std::time::Duration::from_secs(3))
207 );
208 assert_eq!(value.stream_window_size(), Some(1024 * 1024));
209 let keep_alive = value.keep_alive().expect("keepalive is on");
210 assert_eq!(keep_alive.policy(), KeepAlivePolicy::WhileIdle);
211 }
212
213 #[test]
214 fn too_narrow_a_window_is_rejected() {
215 let directory = tempfile::tempdir().expect("a temporary directory");
216 std::fs::write(
217 directory.path().join("config.yaml"),
218 "client:\n streamWindowSize: 1ki\n",
219 )
220 .expect("the configuration file");
221 let problems = load::<ClientConfig>(&Source::directory(directory.path()))
222 .expect_err("the configuration is rejected");
223 assert!(
224 problems.to_string().contains("streamWindowSize"),
225 "{problems}"
226 );
227 }
228}