Configurable retry with exponential backoff, jitter, and circuit breaker
dart pub add philiprehberger_retryConfigurable retry with exponential backoff, jitter, and circuit breaker
Add to your pubspec.yaml:
dependencies:
philiprehberger_retry: ^0.4.0
Then run:
dart pub get
import 'package:philiprehberger_retry/philiprehberger_retry.dart';
final result = await retry(() => fetchData());
final result = await retry(
() => fetchData(),
maxAttempts: 5,
delay: Duration(milliseconds: 500),
backoffMultiplier: 1.5,
jitter: true,
timeout: Duration(seconds: 10),
);
await retry(
() => fetchData(),
onRetry: (attempt, error, delay) {
print('Attempt $attempt failed: $error. Retrying in $delay...');
},
);
final result = await retryWithBackoff(() => fetchData());
Pre-configured with 5 attempts, 1 second initial delay, 2x backoff multiplier, and jitter enabled.
final result = await retry(
() => fetchData(),
retryIf: (e) => e is SocketException,
);
final breaker = CircuitBreaker(
failureThreshold: 3,
resetTimeout: Duration(seconds: 30),
);
try {
final result = await breaker.execute(() => fetchData());
} on CircuitBreakerOpenException {
print('Circuit is open, skipping call');
}
| Symbol | Description |
|---|---|
retry() | Retry an async operation with configurable backoff |
retryWithBackoff() | Convenience retry with exponential backoff defaults |
onRetry | Optional callback invoked before each retry with attempt number, error, and delay |
CircuitBreaker | Prevents repeated calls to a failing service |
CircuitBreaker.execute() | Execute a function through the circuit breaker |
CircuitBreaker.state | Current circuit state (closed, open, halfOpen) |
CircuitBreaker.reset() | Reset the circuit breaker to closed state |
CircuitState | Enum: closed, open, halfOpen |
CircuitBreakerOpenException | Thrown when executing through an open circuit |
dart pub get
dart analyze --fatal-infos
dart test
If you find this project useful: