retry 3.0.0+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 95

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(maxAttempts: 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);

v3.0.0+1 #

  • Update the example in README.md.

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 #

example/main.dart

// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:async';
import 'dart:io';
import 'package:retry/retry.dart';

Future<void> main() async {
  // Create an HttpClient.
  final client = HttpClient();

  try {
    // Get statusCode by retrying a function
    final statusCode = await retry(
      () async {
        // Make a HTTP request and return the status code.
        final request = await client
            .getUrl(Uri.parse('https://www.google.com'))
            .timeout(Duration(seconds: 5));
        final response = await request.close().timeout(Duration(seconds: 5));
        await response.drain();
        return response.statusCode;
      },
      // Retry on SocketException or TimeoutException
      retryIf: (e) => e is SocketException || e is TimeoutException,
    );

    // Print result from status code
    if (statusCode == 200) {
      print('google.com is running');
    } else {
      print('google.com is not availble...');
    }
  } finally {
    // Always close an HttpClient from dart:io, to close TCP connections in the
    // connection pool. Many servers has keep-alive to reduce round-trip time
    // for additional requests and avoid that clients run out of port and
    // end up in WAIT_TIME unpleasantries...
    client.close();
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  retry: ^3.0.0+1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:retry/retry.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
90
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
95
Learn more about scoring.

We analyzed this package on Dec 10, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.1+4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
pedantic ^1.4.0
test ^1.5.1