Skip to main content

rskit_worker/
dispatch.rs

1use std::sync::Arc;
2use std::sync::atomic::{AtomicUsize, Ordering};
3
4/// Strategy used to pick a worker slot in the pool.
5#[derive(Debug, Clone, Default)]
6#[non_exhaustive]
7pub enum DispatchStrategy {
8    /// Assign tasks round-robin across available worker slots.
9    #[default]
10    RoundRobin,
11    /// Prefer slots with the lowest active task count (future extension).
12    LeastLoaded,
13}
14
15/// Simple round-robin counter shared across the pool.
16#[derive(Clone)]
17pub struct RoundRobinDispatcher {
18    counter: Arc<AtomicUsize>,
19    slots: usize,
20}
21
22impl RoundRobinDispatcher {
23    /// Create a new dispatcher that distributes work across `slots` worker slots.
24    pub fn new(slots: usize) -> Self {
25        Self {
26            counter: Arc::new(AtomicUsize::new(0)),
27            slots,
28        }
29    }
30
31    /// Return the next worker index.
32    pub fn next(&self) -> usize {
33        if self.slots == 0 {
34            return 0;
35        }
36        self.counter.fetch_add(1, Ordering::Relaxed) % self.slots
37    }
38}