build method

FetchStrategy build()

Builds a FetchStrategy that operates using the properties of this builder.

Implementation

FetchStrategy build() {
  return (Uri uri, FetchFailure? failure) async {
    if (failure == null) {
      // First attempt. Just load.
      return FetchInstructions.attempt(
        uri: uri,
        timeout: timeout,
      );
    }

    final bool isRetriableFailure = (failure.httpStatusCode != null &&
            transientHttpStatusCodePredicate(failure.httpStatusCode!)) ||
        failure.originalException is io.SocketException;

    // If cannot retry, give up.
    if (!isRetriableFailure || // retrying will not help
        failure.totalDuration > totalFetchTimeout || // taking too long
        failure.attemptCount > maxAttempts) {
      // too many attempts
      return FetchInstructions.giveUp(uri: uri);
    }

    // Exponential back-off.
    final Duration pauseBetweenRetries = initialPauseBetweenRetries *
        math.pow(exponentialBackoffMultiplier, failure.attemptCount - 1);
    await Future<void>.delayed(pauseBetweenRetries);

    // Retry.
    return FetchInstructions.attempt(
      uri: uri,
      timeout: timeout,
    );
  };
}