pub struct LoadBalancer {
pub balancing_strategy: BalancingStrategy,
pub worker_metrics: HashMap<String, WorkerMetrics>,
pub scheduling_strategy: LoadBalancingStrategy,
pub num_workers: usize,
pub total_tasks: usize,
pub next_worker: usize,
pub pending_tasks: Vec<usize>,
}Expand description
Load balancer
Distributes tasks across a fixed pool of logical workers according to a
LoadBalancingStrategy. This is purely logical scheduling — no real
threads are spawned (actual parallelism is a Phase 2 concern).
Fields§
§balancing_strategy: BalancingStrategy§worker_metrics: HashMap<String, WorkerMetrics>§scheduling_strategy: LoadBalancingStrategyStrategy used by LoadBalancer::assign_task to schedule tasks.
num_workers: usizeNumber of logical workers to distribute tasks across.
total_tasks: usizeTotal number of tasks in the current batch (used by Static partitioning).
next_worker: usizeRound-robin cursor: index of the next worker to assign (used by RoundRobin).
pending_tasks: Vec<usize>Pending task count per worker (used by WorkStealing).
Implementations§
Source§impl LoadBalancer
impl LoadBalancer
Sourcepub fn new(strategy: LoadBalancingStrategy) -> Self
pub fn new(strategy: LoadBalancingStrategy) -> Self
Create a load balancer that uses strategy to assign tasks.
The balancer starts with zero workers; call LoadBalancer::with_workers
(or set LoadBalancer::num_workers directly) before invoking
LoadBalancer::assign_task.
Sourcepub fn with_workers(self, num_workers: usize) -> Self
pub fn with_workers(self, num_workers: usize) -> Self
Builder-style setter for the worker count. Resizes the per-worker pending task vector to match and resets scheduling cursors.
Sourcepub fn set_workers(&mut self, num_workers: usize)
pub fn set_workers(&mut self, num_workers: usize)
Set the worker count, resizing the per-worker pending task vector and resetting scheduling cursors.
Sourcepub fn prepare(&mut self, total_tasks: usize)
pub fn prepare(&mut self, total_tasks: usize)
Prepare the balancer for a fresh batch of total_tasks tasks.
Resets the round-robin cursor and per-worker pending counts, and records
the batch size used by LoadBalancingStrategy::Static partitioning.
ParallelExecutor::execute_parallel calls this before assigning tasks;
callers using LoadBalancer::assign_task directly should call it first
(notably for Static).
Sourcepub fn assign_task(&mut self, task_id: usize) -> usize
pub fn assign_task(&mut self, task_id: usize) -> usize
Return the worker index to assign the next task to.
LoadBalancingStrategy::RoundRobin: cycles through workers0,1,…,N-1,0,1,…regardless oftask_id.LoadBalancingStrategy::WorkStealing: assigns to the worker with the fewest pending tasks (ties broken by lowest index) and increments that worker’s pending count.LoadBalancingStrategy::Static: contiguous equal partition —worker = task_id * num_workers / total_tasks(falls back totask_id % num_workerswhentotal_tasksis unset).
Returns 0 when the balancer has no workers configured.
Trait Implementations§
Source§impl Clone for LoadBalancer
impl Clone for LoadBalancer
Source§fn clone(&self) -> LoadBalancer
fn clone(&self) -> LoadBalancer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for LoadBalancer
impl RefUnwindSafe for LoadBalancer
impl Send for LoadBalancer
impl Sync for LoadBalancer
impl Unpin for LoadBalancer
impl UnsafeUnpin for LoadBalancer
impl UnwindSafe for LoadBalancer
Blanket Implementations§
§impl<S, A> Aggregate<Result<S, Error>> for Awhere
A: Aggregate<S>,
impl<S, A> Aggregate<Result<S, Error>> for Awhere
A: Aggregate<S>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more