Skip to main content

moirai_core/channel/
config.rs

1//! Configuration for unified channels.
2
3/// Capacity used by every channel constructor that does not take one.
4///
5/// Channels are bounded by default: an unbounded queue lets a producer that
6/// outruns its consumer convert backpressure into unbounded memory growth,
7/// which is a liveness failure under adversarial or merely bursty load. The
8/// default therefore has to be a number, and this is it.
9///
10/// Derivation: 1024 slots is large enough that a producer burst spanning a
11/// scheduler quantum never blocks on an otherwise-keeping-up consumer (the
12/// runtime's own queue capacity is 256 jobs per worker), and small enough that
13/// the resident bound is one pointer-sized slot array — 8 KiB for a
14/// word-sized payload — per channel rather than a function of producer speed.
15/// Power of two so the bounded MPMC ring uses it without rounding up.
16///
17/// Callers whose producer/consumer rates are known should pass an explicit
18/// capacity instead of inheriting this one.
19pub const DEFAULT_CHANNEL_CAPACITY: usize = 1024;
20
21/// Channel configuration for unified memory management
22#[derive(Debug, Clone)]
23pub struct ChannelConfig {
24    /// Buffer capacity (will be rounded to power of 2)
25    pub capacity: usize,
26    /// Whether to use memory pooling for overflow
27    pub enable_pooling: bool,
28    /// Maximum pool size for overflow handling
29    pub max_pool_size: usize,
30    /// Whether to enable batch operations
31    pub enable_batching: bool,
32    /// Batch size for bulk operations
33    pub batch_size: usize,
34}
35
36impl Default for ChannelConfig {
37    fn default() -> Self {
38        Self {
39            capacity: DEFAULT_CHANNEL_CAPACITY,
40            enable_pooling: true,
41            max_pool_size: DEFAULT_CHANNEL_CAPACITY * 2,
42            enable_batching: false,
43            batch_size: 64,
44        }
45    }
46}