Viewing version 3.0.0. Open the latest package page.

retry

3.0.0

Utility for wrapping an asynchronous function in automatic retry logic with exponential back-off, useful when making requests over network.

Download archive

Readme

Retry for dart

This package provides an easy way to retry asynchronous functions. This is often useful to avoid crashing on intermittent errors such as broken connections or temporarily overloaded servers.

Disclaimer: This is not an officially supported Google product.

Using retry

For simple retry logic with exponential back-off use the retry function provided by this package.

import 'package:retry/retry.dart';

final response = await retry(
  // Make a GET request
  () => http.get('https://google.com').timeout(Duration(seconds: 5)),
  // Retry on SocketException or TimeoutException
  retryIf: (e) => e is SocketException || e is TimeoutException,
);
print(response.body);

Defaults to 8 attempts, sleeping as following after 1st, 2nd, 3rd, ..., 7th attempt:

  1. 400 ms +/- 25%
  2. 800 ms +/- 25%
  3. 1600 ms +/- 25%
  4. 3200 ms +/- 25%
  5. 6400 ms +/- 25%
  6. 12800 ms +/- 25%
  7. 25600 ms +/- 25%

Using RetryOptions

This package provides RetryOptions which defined how many times to retry an function and how long to sleep between retries.

final r = RetryOptions(attempts: 8);
final response = await r.retry(
  // Make a GET request
  () => http.get('https://google.com').timeout(Duration(seconds: 5)),
  // Retry on SocketException or TimeoutException
  retryIf: (e) => e is SocketException || e is TimeoutException,
);
print(response.body);
Changelog

v3.0.0

  • When retryIf is not given, we default to retry any Exception thrown. This is breaking, but provides a more useful default behavior.

v2.0.0

  • Expect complete API break.
  • Initial release started from scratch, lifted from code in github.com/dart-lang/pub-dartlang-dart.
  • Credits to Tim Kluge for transferring ownership.
  • Dart 2.0 support.

v1.0.0

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'retry:3.0.0'
pubspec.yaml
name: retry
version: 3.0.0
authors:
  - Jonas Finnemann Jensen <jonasfj@google.com>
description: |
  Utility for wrapping an asynchronous function in automatic retry logic with
  exponential back-off, useful when making requests over network.
homepage: https://github.com/google/dart-neats/tree/master/retry
repository: https://github.com/google/dart-neats.git
issue_tracker: https://github.com/google/dart-neats/labels/pkg:retry
dev_dependencies:
  test: ^1.5.1
  pedantic: ^1.4.0
environment:
  sdk: '>=2.0.0 <3.0.0'

Version history

VersionPublishedArchiveSHA-256
1.0.0Cached · Upstream SHA-256 matchedDownload4d918240…989abc
2.0.0Cached · Upstream SHA-256 matchedDownload08c6fa9b…31341a
3.0.0Cached · Upstream SHA-256 matchedDownload7dec5e83…6e25f7
3.0.0+1Cached · Upstream SHA-256 matchedDownload0719943a…bfb1c9
3.0.1-null-safety.0Cached · Upstream SHA-256 matchedDownloaddda0aaa6…110023
3.0.1Cached · Upstream SHA-256 matchedDownload6d0cc7ca…811440
3.1.0-null-safety.0Cached · Upstream SHA-256 matchedDownload64d27479…72da79
3.1.0Cached · Upstream SHA-256 matchedDownload45dfeeba…afdbab
3.1.1Cached · Upstream SHA-256 matchedDownloada8a1e475…ce7aea
3.1.2Cached · Upstream SHA-256 matchedDownload822e118d…ba7efc

Dependencies

No direct dependencies declared.