WorkerPool class
A fixed set of N EventBusIsolates with one dispatch strategy and supervision.
broadcast fans an event out to every ready worker; every other strategy
selects exactly one. Requests are never broadcast. A request that lost its
worker to a crash fails and is not retried.
A dead worker is restarted with SupervisionPolicy; after SupervisionPolicy.maxRestarts attempts inside the window the pool emits an EventBusError of kind EventBusErrorKind.supervision and becomes failed. close marks the shutdown as intentional and never restarts anything.
Backpressure is a per-worker request credit (maxInFlightPerWorker): a
saturated worker is skipped by the selector. When no worker has a free
credit, fire returns false and request throws BusyException. 4.0.0 has
no pool-level dispatch queue: callers fail fast instead of waiting.
A pool is not an EventBus: broadcast has no request semantics, and model
state is per worker, not aggregated.
Constructors
- WorkerPool({required WorkerInit onInit, int workers = 1, DispatchStrategy dispatch = DispatchStrategy.roundRobin, SupervisionPolicy supervision = const SupervisionPolicy(), int? maxInFlightPerWorker = 64, Object? initialData, Object? initialDataFor(int index)?, bool isModelBus = false, WorkerKeySelector? keySelector})
Properties
- dispatch → DispatchStrategy
-
How an event or a request picks its worker.
final
-
errors
→ Stream<
EventBusError> -
Worker failures plus supervision errors.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialData → Object?
-
Initial data for every worker when initialDataFor is not set.
final
- initialDataFor → Object? Function(int index)?
-
Per-worker initial data, for example a worker index or a role. Takes
precedence over initialData.
final
- isModelBus → bool
-
Whether every worker bus is a model bus.
final
- isReady → bool
-
True when every worker is ready.
no setter
- isUsable → bool
-
True while the pool accepts work: at least one worker is ready and the
pool is neither failed nor closed.
no setter
- keySelector → WorkerKeySelector?
-
The key used by DispatchStrategy.keyed; null means the event address.
final
- maxInFlightPerWorker → int?
-
How many requests one worker may have in flight before the pool treats it
as saturated and stops routing new work to it. Null means unlimited.
final
- metrics → WorkerPoolMetrics
-
A snapshot of the counters.
no setter
-
onInit
→ FutureOr<
void> Function(EventBus bus, Object? initialData) -
The init function run in every worker.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → WorkerPoolState
-
The current lifecycle state.
no setter
- supervision → SupervisionPolicy
-
Restart limits and backoff.
final
-
waitInit
→ Future<
void> -
Completes when every worker has finished its
onInit.no setter - workers → int
-
How many isolates the pool owns.
final
Methods
-
close(
) → Future< void> - Stops the pool: cancels timers and error listeners, disposes every worker and closes errors. Intentionally closed workers are never restarted. Safe to call twice.
-
dispose(
) → Future< void> - An alias for close.
-
fire<
T> (EventKey< T> key, T data, {Map<String, String> ? arguments, String? correlationId}) → bool - Sends an event on an exact E key according to dispatch.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
request<
T> (EventKey< T> key, T data, {Map<String, String> ? arguments, Duration? timeout, CancelToken? cancelToken, String? correlationId}) → Future - Sends a request on an exact E key to the selected worker.
-
toString(
) → String -
A string representation of this object.
inherited
-
waitReady(
) → Future< void> - Waits until every worker is ready. Fails when the pool becomes failed or closed first.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited