1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::time::Duration;
use crate::http_breakpoint::BreakpointDownloadHttpConfig;
/// Runtime and transport configuration for [`crate::MeowClient`].
///
/// This type is immutable after client creation. Build it with `new()` or
/// `Default::default()`, then chain `with_*` methods.
///
/// # Recommended workflow
///
/// 1. Start with `MeowConfig::default()` for safe baseline values.
/// 2. Tune concurrency and queue capacities according to workload.
/// 3. Set HTTP timeout and keepalive for your network environment.
/// 4. Optionally inject a preconfigured `reqwest::Client`.
///
/// # Value constraints
///
/// - Concurrency values should be `>= 1`.
/// - Queue capacities should be `>= 1`.
/// - `http_timeout` and `tcp_keepalive` should be positive durations.
/// - Zero or extremely small durations may cause request failures.
#[derive(Debug, Clone)]
pub struct MeowConfig {
/// Maximum number of upload groups processed concurrently.
///
/// Recommended range: `1..=64`.
max_upload_concurrency: usize,
/// Maximum number of download groups processed concurrently.
///
/// Recommended range: `1..=64`.
max_download_concurrency: usize,
/// HTTP-level settings used by range download requests.
breakpoint_download_http: BreakpointDownloadHttpConfig,
/// Optional custom HTTP client used by transfer requests.
///
/// If `None`, the library builds an internal `reqwest::Client`.
http_client: Option<reqwest::Client>,
/// Per-request timeout used when building internal HTTP clients.
///
/// Recommended range: `1s..=120s`.
http_timeout: Duration,
/// TCP keepalive duration for internal HTTP clients.
///
/// Recommended range: `10s..=5min`.
tcp_keepalive: Duration,
/// Capacity of the control-plane command queue.
///
/// Recommended range: `16..=4096`.
command_queue_capacity: usize,
/// Capacity of the worker event queue (progress/state events).
///
/// Recommended range: `32..=8192`.
worker_event_queue_capacity: usize,
}
impl Default for MeowConfig {
fn default() -> Self {
Self {
max_upload_concurrency: 2,
max_download_concurrency: 2,
breakpoint_download_http: BreakpointDownloadHttpConfig::default(),
http_client: None,
http_timeout: Duration::from_secs(5),
tcp_keepalive: Duration::from_secs(30),
command_queue_capacity: 128,
worker_event_queue_capacity: 256,
}
}
}
impl MeowConfig {
/// Creates a new config with explicit upload/download concurrency.
///
/// Other fields use the same defaults as [`Default::default`].
///
/// # Parameters
///
/// - `max_upload_concurrency`: Recommended `>= 1`.
/// - `max_download_concurrency`: Recommended `>= 1`.
///
/// # Usage rules
///
/// Prefer values close to CPU and network capacity. Very high values can
/// increase memory pressure and request contention.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::new(4, 4);
/// assert_eq!(config.max_upload_concurrency(), 4);
/// assert_eq!(config.max_download_concurrency(), 4);
/// ```
pub fn new(max_upload_concurrency: usize, max_download_concurrency: usize) -> Self {
Self {
max_upload_concurrency,
max_download_concurrency,
breakpoint_download_http: BreakpointDownloadHttpConfig::default(),
http_client: None,
http_timeout: Duration::from_secs(5),
tcp_keepalive: Duration::from_secs(30),
command_queue_capacity: 128,
worker_event_queue_capacity: 256,
}
}
/// Injects a custom HTTP client for all transfer requests.
///
/// Use this when you need custom proxy, TLS, default headers, middleware,
/// or observability behavior.
///
/// # Usage rules
///
/// - The provided client should be reusable and long-lived.
/// - Keep timeout/connection settings aligned with your workload.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let client = reqwest::Client::new();
/// let config = MeowConfig::default().with_http_client(client);
/// let _ = config;
/// ```
pub fn with_http_client(mut self, client: reqwest::Client) -> Self {
self.http_client = Some(client);
self
}
/// Sets request timeout used by internally created HTTP clients.
///
/// # Range guidance
///
/// - Typical: `3s..=60s`.
/// - High-latency network: `30s..=120s`.
/// - Avoid zero duration.
///
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default().with_http_timeout(Duration::from_secs(20));
/// assert_eq!(config.http_timeout(), Duration::from_secs(20));
/// ```
pub fn with_http_timeout(mut self, timeout: Duration) -> Self {
self.http_timeout = timeout;
self
}
/// Sets TCP keepalive for internally created HTTP clients.
///
/// # Range guidance
///
/// - Typical: `15s..=120s`.
/// - Keepalive too small may increase churn.
/// - Keepalive too large may delay broken-connection detection.
///
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default().with_tcp_keepalive(Duration::from_secs(60));
/// assert_eq!(config.tcp_keepalive(), Duration::from_secs(60));
/// ```
pub fn with_tcp_keepalive(mut self, keepalive: Duration) -> Self {
self.tcp_keepalive = keepalive;
self
}
/// Sets command queue capacity for control-plane operations.
///
/// This queue carries operations such as enqueue, pause, resume, cancel,
/// snapshot, and close.
///
/// # Range guidance
///
/// Recommended range: `16..=4096`; must be `>= 1`.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default().with_command_queue_capacity(512);
/// assert_eq!(config.command_queue_capacity(), 512);
/// ```
pub fn with_command_queue_capacity(mut self, command_queue_capacity: usize) -> Self {
self.command_queue_capacity = command_queue_capacity;
self
}
/// Sets worker event queue capacity for runtime task events.
///
/// Events include progress updates and task terminal states.
///
/// # Range guidance
///
/// Recommended range: `32..=8192`; must be `>= 1`.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default().with_worker_event_queue_capacity(1024);
/// assert_eq!(config.worker_event_queue_capacity(), 1024);
/// ```
pub fn with_worker_event_queue_capacity(mut self, worker_event_queue_capacity: usize) -> Self {
self.worker_event_queue_capacity = worker_event_queue_capacity;
self
}
/// Returns maximum upload concurrency.
///
/// Expected effective range: `>= 1`.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::new(3, 2);
/// assert_eq!(config.max_upload_concurrency(), 3);
/// ```
pub fn max_upload_concurrency(&self) -> usize {
self.max_upload_concurrency
}
/// Returns maximum download concurrency.
///
/// Expected effective range: `>= 1`.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::new(3, 2);
/// assert_eq!(config.max_download_concurrency(), 2);
/// ```
pub fn max_download_concurrency(&self) -> usize {
self.max_download_concurrency
}
/// Returns range-download HTTP behavior configuration.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default();
/// let _ = config.breakpoint_download_http();
/// ```
pub fn breakpoint_download_http(&self) -> &BreakpointDownloadHttpConfig {
&self.breakpoint_download_http
}
/// Overrides range-download HTTP behavior configuration.
///
/// Use this to customize request headers (for example `Accept`) for range
/// download chunks.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::{BreakpointDownloadHttpConfig, MeowConfig};
///
/// let config = MeowConfig::default().with_breakpoint_download_http(
/// BreakpointDownloadHttpConfig {
/// range_accept: "application/octet-stream".to_string(),
/// },
/// );
/// let _ = config;
/// ```
pub fn with_breakpoint_download_http(mut self, config: BreakpointDownloadHttpConfig) -> Self {
self.breakpoint_download_http = config;
self
}
/// Returns request timeout used by internal HTTP clients.
///
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default();
/// assert_eq!(config.http_timeout(), Duration::from_secs(5));
/// ```
pub fn http_timeout(&self) -> Duration {
self.http_timeout
}
/// Returns TCP keepalive used by internal HTTP clients.
///
/// # Examples
///
/// ```no_run
/// use std::time::Duration;
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default();
/// assert_eq!(config.tcp_keepalive(), Duration::from_secs(30));
/// ```
pub fn tcp_keepalive(&self) -> Duration {
self.tcp_keepalive
}
/// Returns the injected custom HTTP client, if present.
///
/// This is an internal accessor used by runtime components.
pub(crate) fn http_client_ref(&self) -> Option<&reqwest::Client> {
self.http_client.as_ref()
}
/// Returns control-plane command queue capacity.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default();
/// assert_eq!(config.command_queue_capacity(), 128);
/// ```
pub fn command_queue_capacity(&self) -> usize {
self.command_queue_capacity
}
/// Returns worker event queue capacity.
///
/// # Examples
///
/// ```no_run
/// use rusty_cat::api::MeowConfig;
///
/// let config = MeowConfig::default();
/// assert_eq!(config.worker_event_queue_capacity(), 256);
/// ```
pub fn worker_event_queue_capacity(&self) -> usize {
self.worker_event_queue_capacity
}
}