Skip to main content

spectra_runtime/
persist_config.rs

1//! Builder-facing persist queue and batch settings.
2
3use std::time::Duration;
4
5/// Behavior when the L2 persist queue is full.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum PersistOverflow {
8    /// Non-blocking `try_send`; drop the job and increment `persist_queue_drops` (default).
9    #[default]
10    Drop,
11    /// Apply backpressure: block the calling thread until the job is enqueued (or the queue closes).
12    ///
13    /// When called from a Tokio worker thread, uses `block_in_place` so the runtime can schedule
14    /// other work while waiting. Prefer this for scripts that must not lose emits under load.
15    Block,
16}
17
18/// Queue and batch settings for [`crate::StoragePersistSink`].
19///
20/// Set via [`crate::SpectraBuilder::persist`]. Defaults match the previous in-process
21/// behavior (batching on, overflow drop). Callers raise `batch_max` for high-throughput DW ingest.
22#[derive(Debug, Clone)]
23pub struct PersistConfig {
24    /// Max queued persist jobs before overflow policy applies. Default `8192`.
25    pub queue_max: usize,
26    /// Max jobs collected into one batch flush. Default `32`.
27    pub batch_max: usize,
28    /// Extra wait when the first collected job is alone, to coalesce. Default `5ms`.
29    pub batch_wait: Duration,
30    /// When `true`, use `record_metrics_batch` / `append_rows_batch`. Default `true`.
31    pub batch_enabled: bool,
32    /// Overflow policy when `queue_max` is reached. Default [`PersistOverflow::Drop`].
33    pub overflow: PersistOverflow,
34}
35
36impl Default for PersistConfig {
37    fn default() -> Self {
38        Self {
39            queue_max: 8192,
40            batch_max: 32,
41            batch_wait: Duration::from_millis(5),
42            batch_enabled: true,
43            overflow: PersistOverflow::Drop,
44        }
45    }
46}
47
48impl PersistConfig {
49    pub(crate) fn normalized(self) -> Self {
50        Self {
51            queue_max: self.queue_max.max(1),
52            batch_max: self.batch_max.max(1),
53            batch_wait: self.batch_wait,
54            batch_enabled: self.batch_enabled,
55            overflow: self.overflow,
56        }
57    }
58}