Skip to main content

clicktype_batch/
config.rs

1//! Batch configuration
2
3use std::time::Duration;
4
5/// Batch configuration
6#[derive(Debug, Clone, PartialEq)]
7pub struct BatchConfig {
8    /// Maximum number of rows before automatic flush
9    pub max_rows: usize,
10
11    /// Maximum wait time before flush
12    pub max_wait: Duration,
13
14    /// Initial buffer size in bytes
15    pub initial_buffer_size: usize,
16
17    /// Maximum buffer size before backpressure
18    pub max_buffer_size: usize,
19
20    /// Maximum buffer capacity to retain after flush (prevents memory leaks)
21    /// If capacity exceeds this value, the buffer will be reallocated.
22    /// Defaults to 2 * initial_buffer_size.
23    pub buffer_shrink_threshold: usize,
24
25    /// Maximum number of retries on insert failure
26    pub max_retries: u32,
27
28    /// Base delay between retries (exponential backoff)
29    pub retry_base_delay: Duration,
30
31    /// MPSC channel capacity
32    pub channel_capacity: usize,
33}
34
35impl Default for BatchConfig {
36    fn default() -> Self {
37        Self {
38            max_rows: 50_000,
39            max_wait: Duration::from_secs(5),
40            initial_buffer_size: 1024 * 1024,        // 1 MB
41            max_buffer_size: 64 * 1024 * 1024,       // 64 MB
42            buffer_shrink_threshold: 2 * 1024 * 1024, // 2 MB
43            max_retries: 3,
44            retry_base_delay: Duration::from_millis(100),
45            channel_capacity: 10_000,
46        }
47    }
48}
49
50impl BatchConfig {
51    /// High throughput preset
52    pub fn high_throughput() -> Self {
53        Self {
54            max_rows: 100_000,
55            max_wait: Duration::from_secs(10),
56            initial_buffer_size: 4 * 1024 * 1024,    // 4 MB
57            max_buffer_size: 256 * 1024 * 1024,      // 256 MB
58            buffer_shrink_threshold: 8 * 1024 * 1024, // 8 MB
59            channel_capacity: 100_000,
60            ..Default::default()
61        }
62    }
63
64    /// Low latency preset
65    pub fn low_latency() -> Self {
66        Self {
67            max_rows: 1_000,
68            max_wait: Duration::from_millis(100),
69            initial_buffer_size: 64 * 1024,          // 64 KB
70            max_buffer_size: 16 * 1024 * 1024,       // 16 MB
71            buffer_shrink_threshold: 128 * 1024,     // 128 KB
72            channel_capacity: 1_000,
73            ..Default::default()
74        }
75    }
76}
77
78/// Statistics from a flush operation
79#[derive(Debug, Clone)]
80pub struct FlushStats {
81    /// Number of rows flushed
82    pub rows_flushed: usize,
83
84    /// Bytes sent
85    pub bytes_sent: usize,
86
87    /// Time taken for flush
88    pub duration: Duration,
89
90    /// Age of the batch (time from first row)
91    pub batch_age: Duration,
92}