RetryClient constructor
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');
}