RetryClient constructor

RetryClient(Client _inner, { int retries, bool when(BaseResponse response), bool whenError(dynamic error, StackTrace stackTrace), Duration delay(int retryCount), void onRetry(BaseRequest request, BaseResponse response, 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,
    bool when(BaseResponse response),
    bool whenError(error, StackTrace stackTrace),
    Duration delay(int retryCount),
    void onRetry(BaseRequest request, BaseResponse response, int retryCount)})
    : _retries = retries ?? 3,
      _when = when ?? ((response) => response.statusCode == 503),
      _whenError = whenError ?? ((_, __) => false),
      _delay = delay ??
          ((retryCount) =>
              new Duration(milliseconds: 500) * math.pow(1.5, retryCount)),
      _onRetry = onRetry {
  RangeError.checkNotNegative(_retries, "retries");
}