RetryClient constructor

RetryClient(
  1. Client _inner, {
  2. int retries = 3,
  3. FutureOr<bool> when(
    1. BaseResponse
    ) = _defaultWhen,
  4. FutureOr<bool> whenError(
    1. Object,
    2. StackTrace
    ) = _defaultWhenError,
  5. Duration delay(
    1. int retryCount
    ) = _defaultDelay,
  6. FutureOr<void> onRetry(
    1. BaseRequest,
    2. BaseResponse?,
    3. int retryCount
    )?,
})

Creates a client wrapping _inner that retries HTTP requests.

This retries a failing request retries times (3 by default). Note that n retries means that the request will be sent at most n + 1 times.

By default, this retries requests whose responses have status code 503 Temporary Failure. If when is passed, it retries any request for whose response when returns true. If whenError is passed, it also retries any request that throws an error for which whenError returns true.

By default, this waits 500ms between the original request and the first retry, then increases the delay by 1.5x for each subsequent retry. If delay is passed, it's used to determine the time to wait before the given (zero-based) retry.

If onRetry is passed, it's called immediately before each retry so that the client has a chance to perform side effects like logging. The response parameter will be null if the request was retried due to an error for which whenError returned true.

Implementation

RetryClient(
  this._inner, {
  int retries = 3,
  FutureOr<bool> Function(BaseResponse) when = _defaultWhen,
  FutureOr<bool> Function(Object, StackTrace) whenError = _defaultWhenError,
  Duration Function(int retryCount) delay = _defaultDelay,
  FutureOr<void> Function(BaseRequest, BaseResponse?, int retryCount)?
      onRetry,
})  : _retries = retries,
      _when = when,
      _whenError = whenError,
      _delay = delay,
      _onRetry = onRetry {
  RangeError.checkNotNegative(_retries, 'retries');
}