stop method
Stop idle pool workers matching the predicate
.
If predicate
is null or not provided, all workers will be stopped.
Stopping a worker does not interrupt or cancel processing. Workers will
complete pending tasks before shutting down. In the meantime, they will
not receive any new workload.
Returns the number of workers that have been stopped.
Implementation
int stop([bool Function(W worker)? predicate]) {
List<PoolWorker<W>> targets;
bool force = (predicate == null);
if (force) {
// kill workers while keeping enough workers alive to process pending tasks
targets = _workers.skip(_queue.length).toList();
_stopped = true;
} else {
// kill workers that are idle and satisfy the predicate
targets = _workers.where((w) => w.isIdle && predicate(w.worker)).toList();
}
var stopped = 0;
for (var poolWorker in targets) {
stopped += _removeWorker(poolWorker, force);
}
return stopped;
}