retry_plus 0.1.1 copy "retry_plus: ^0.1.1" to clipboard
retry_plus: ^0.1.1 copied to clipboard

Composable retry policies for Dart with exception and result predicates, backoff, jitter, hooks, and cancellation.

example/retry_plus_example.dart

import 'package:retry_plus/retry_plus.dart';

Future<void> main() async {
  var attempts = 0;

  final policy = Retry<String>(
    delay: DelayPolicy.exponential(
      initial: const Duration(milliseconds: 100),
      max: const Duration(seconds: 1),
      jitter: Jitter.full(),
    ),
    maxRetries: 2,
    onRetry: (event) {
      print('attempt ${event.attemptNumber} failed; retrying');
    },
    timeout: TimeoutStrategy(const Duration(seconds: 2)),
    fallback: FallbackStrategy.value('fallback value'),
  );

  final result = await policy.execute(() async {
    attempts++;
    if (attempts < 3) {
      throw StateError('temporary failure');
    }
    return 'success';
  });

  print(result);

  final breaker = CircuitBreaker(
    failureThreshold: 2,
    recoveryDuration: const Duration(seconds: 30),
  );

  final guarded = Retry<String>(
    circuitBreaker: breaker,
    fallback: FallbackStrategy.value(
      'cached value',
      fallbackIf:
          FallbackPredicate.exceptionType<CircuitOpenException, String>(),
    ),
  );

  print(await guarded.execute(() async => 'healthy'));
}
1
likes
130
points
31
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Composable retry policies for Dart with exception and result predicates, backoff, jitter, hooks, and cancellation.

License

BSD-3-Clause (license)

Dependencies

clock

More

Packages that depend on retry_plus