backoff 1.0.1 copy "backoff: ^1.0.1" to clipboard
backoff: ^1.0.1 copied to clipboard

The exponential backoff algorithm in Dart

example/backoff_example.dart

import 'dart:math';

import 'package:backoff/backoff.dart';

Future<void> main() async {
  final backoff = ExponentialBackOff(); // Use default parameters

  ///** You can customize the parameters during instantiation:
  ///ExponentialBackOff customBackOff = ExponentialBackOff(
  ///   initialIntervalMillis: 1000,
  ///   randomizationFactor: 0.2,
  ///   multiplier: 2.0,
  ///   maxIntervalMillis: 60000,
  ///   maxElapsedTimeMillis: 180000,
  /// ); */
  ///
  ///

  // Define an asynchronous operation that might fail and needs retrying
  Future<void> asyncOperation() async {
    print('Attempting the operation...');
    if (Random().nextBool()) {
      throw Exception('Simulated failure');
    } else {
      print('Operation succeeded!');
    }
  }

  int backOffMillis = backoff.nextBackOffMillis();

  if (backOffMillis == BackOff.STOP) {
    // Do not retry operation
  } else {
    // Sleep for backOffMillis milliseconds and retry operation
    await Future.delayed(Duration(milliseconds: backOffMillis));
    // Retry operation here
    await asyncOperation();
  }

  print("Initial Interval: ${backoff.getInitialIntervalMillis()} milliseconds");
  print("Randomization Factor: ${backoff.getRandomizationFactor()}");
  print("Current Interval: ${backoff.getCurrentIntervalMillis()} milliseconds");
  print("Multiplier: ${backoff.getMultiplier()}");
  print("Max Interval: ${backoff.getMaxIntervalMillis()} milliseconds");
  print("Max Elapsed Time: ${backoff.getMaxElapsedTimeMillis()} milliseconds");
}
3
likes
145
points
151
downloads

Publisher

verified publisheretornam.dev

Weekly Downloads

The exponential backoff algorithm in Dart

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on backoff