run<R, P> method

  1. @override
Future<R> run<R, P>(
  1. FutureOr<R> function(
    1. P argument
    ),
  2. P argument, {
  3. Duration? timeout,
  4. FutureOr<R> onTimeout()?,
  5. int load = 100,
})
override

Execute the command in the currently least loaded isolate.

The optional load parameter represents the load that the command is causing on the isolate where it runs. The number has no fixed meaning, but should be seen as relative to other commands run in the same load balancer. The load must not be negative.

If timeout and onTimeout are provided, they are forwarded to the runner running the function, which will handle a timeout as normal. If the runners are running in other isolates, then the onTimeout function must be a constant function.

Implementation

@override
Future<R> run<R, P>(FutureOr<R> Function(P argument) function, P argument,
    {Duration? timeout, FutureOr<R> Function()? onTimeout, int load = 100}) {
  RangeError.checkNotNegative(load, 'load');
  if (_length == 0) {
    // Can happen if created with zero runners,
    // or after being closed.
    if (_stopFuture != null) {
      throw StateError("Load balancer has been closed");
    }
    throw StateError("No runners in pool");
  }
  var entry = _queue.first;
  entry.load += load;
  _bubbleDown(entry, 0);
  return entry.run(this, load, function, argument, timeout, onTimeout);
}