request<T> method

Future request<T>(
  1. EventKey<T> key,
  2. T data, {
  3. Map<String, String>? arguments,
  4. Duration? timeout,
  5. CancelToken? cancelToken,
  6. String? correlationId,
})

Sends a request on an exact E key to the selected worker.

Implementation

Future<dynamic> request<T>(EventKey<T> key, T data,
    {Map<String, String>? arguments,
    Duration? timeout,
    CancelToken? cancelToken,
    String? correlationId}) async {
  _checkOpen();
  if (dispatch == DispatchStrategy.broadcast) {
    throw UnsupportedError(
        'DispatchStrategy.broadcast cannot route requests: a request needs '
        'exactly one answering worker');
  }
  final dto = EventDTO<T>.withKey(key, data,
      arguments: arguments, correlationId: correlationId);
  final slot = _select(dto);
  if (slot == null || !_available(slot)) {
    _counters.failedRequests++;
    throw BusyException(key.address.value,
        'no ready worker with a free credit for dispatch strategy ${dispatch.name}');
  }
  final bus = slot.bus!;

  _counters.requests++;
  slot.requests++;
  slot.inFlight++;
  try {
    return await bus.request<T>(key, data,
        arguments: arguments,
        timeout: timeout,
        cancelToken: cancelToken,
        correlationId: correlationId);
  } on StateError {
    // the worker died between the readiness check and the send
    _counters.failedRequests++;
    throw BusyException(
        key.address.value, 'worker ${slot.index} is not ready any more');
  } catch (_) {
    _counters.failedRequests++;
    rethrow;
  } finally {
    slot.inFlight--;
  }
}