Echo/Scheduler/SchedulerBuilder.rs
1//! # SchedulerBuilder
2//!
3//! Defines the fluent builder for creating and configuring a `Scheduler`.
4
5#![allow(non_snake_case, non_camel_case_types)]
6
7use std::collections::HashMap;
8
9use log::warn;
10
11use crate::Scheduler::Scheduler::Scheduler;
12
13/// Defines concurrency limits for named queues (for future use).
14#[derive(Debug, Clone, Copy)]
15pub enum Concurrency {
16 /// Specifies a maximum number of concurrent tasks for a queue.
17 Limit(usize),
18
19 /// Allows an unlimited number of concurrent tasks for a queue.
20 Unlimited,
21}
22
23/// A fluent builder for creating a `Scheduler` instance.
24///
25/// This pattern provides a clear and readable API for configuring the scheduler
26/// before it is constructed. It is the primary entry point for using the `Echo`
27/// library.
28pub struct SchedulerBuilder {
29 /// The number of worker threads to be spawned in the scheduler's pool.
30 Count:usize,
31
32 /// Configuration for named queues with concurrency limits (for future
33 /// use).
34 Configuration:HashMap<String, Concurrency>,
35}
36
37impl SchedulerBuilder {
38 /// Creates a new `SchedulerBuilder` with default settings.
39 ///
40 /// By default, the worker count is set to the number of logical CPUs on the
41 /// system, with a minimum of two workers to ensure work-stealing is
42 /// viable.
43 pub fn Create() -> Self {
44 let Default = num_cpus::get().max(2);
45
46 Self { Count:Default, Configuration:HashMap::new() }
47 }
48
49 /// Sets the total number of worker threads for the scheduler pool.
50 ///
51 /// If `Count` is `0`, it defaults to the number of logical CPUs.
52 pub fn WithWorkerCount(mut self, Count:usize) -> Self {
53 if Count == 0 {
54 warn!("[SchedulerBuilder] Worker count of 0 is invalid. Defaulting to logical CPUs.");
55
56 self.Count = num_cpus::get().max(2);
57 } else {
58 self.Count = Count;
59 }
60
61 self
62 }
63
64 /// Configures a named queue with a specific concurrency limit (for future
65 /// use).
66 pub fn WithQueue(mut self, Name:&str, Limit:Concurrency) -> Self {
67 self.Configuration.insert(Name.to_string(), Limit);
68
69 self
70 }
71
72 /// Builds and starts the `Scheduler` with the specified configuration.
73 ///
74 /// This method consumes the builder and returns a new, running `Scheduler`.
75 pub fn Build(self) -> Scheduler { Scheduler::Create(self.Count, self.Configuration) }
76}
77
78impl Default for SchedulerBuilder {
79 /// Provides a default `SchedulerBuilder` instance.
80 fn default() -> Self { Self::Create() }
81}