crossio_core/
config.rs

1//! Backend configuration knobs shared across platforms.
2use std::time::Duration;
3
4/// Configuration passed to every backend on creation.
5#[derive(Debug, Clone)]
6pub struct BackendConfig {
7    queue_depth: usize,
8    event_batch_size: usize,
9    spin_before_park: Duration,
10    default_timeout: Option<Duration>,
11}
12
13impl BackendConfig {
14    /// Returns a configuration with sensible defaults for most workloads.
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    /// Maximum number of descriptors that can be registered with the backend.
20    pub fn queue_depth(&self) -> usize {
21        self.queue_depth
22    }
23
24    /// Number of events a backend should attempt to process per poll call.
25    pub fn event_batch_size(&self) -> usize {
26        self.event_batch_size
27    }
28
29    /// Duration the backend may spin prior to parking the thread.
30    pub fn spin_before_park(&self) -> Duration {
31        self.spin_before_park
32    }
33
34    /// Default timeout applied when callers pass `None` to [`Backend::poll`](crate::Backend::poll).
35    pub fn default_timeout(&self) -> Option<Duration> {
36        self.default_timeout
37    }
38
39    /// Overrides the queue depth.
40    pub fn with_queue_depth(mut self, queue_depth: usize) -> Self {
41        self.queue_depth = queue_depth.max(1);
42        self
43    }
44
45    /// Overrides the event batch size.
46    pub fn with_event_batch_size(mut self, batch: usize) -> Self {
47        self.event_batch_size = batch.max(1);
48        self
49    }
50
51    /// Overrides the spin duration.
52    pub fn with_spin_before_park(mut self, duration: Duration) -> Self {
53        self.spin_before_park = duration;
54        self
55    }
56
57    /// Overrides the default timeout value.
58    pub fn with_default_timeout(mut self, timeout: Option<Duration>) -> Self {
59        self.default_timeout = timeout;
60        self
61    }
62}
63
64impl Default for BackendConfig {
65    fn default() -> Self {
66        Self {
67            queue_depth: 4096,
68            event_batch_size: 512,
69            spin_before_park: Duration::from_micros(50),
70            default_timeout: None,
71        }
72    }
73}