philiprehberger_retry 0.4.0
philiprehberger_retry: ^0.4.0 copied to clipboard
Configurable retry with exponential backoff, jitter, and circuit breaker
philiprehberger_retry #
Configurable retry with exponential backoff, jitter, and circuit breaker
Requirements #
- Dart >= 3.8
Installation #
Add to your pubspec.yaml:
dependencies:
philiprehberger_retry: ^0.4.0
Then run:
dart pub get
Usage #
import 'package:philiprehberger_retry/philiprehberger_retry.dart';
final result = await retry(() => fetchData());
Retry with Custom Options #
final result = await retry(
() => fetchData(),
maxAttempts: 5,
delay: Duration(milliseconds: 500),
backoffMultiplier: 1.5,
jitter: true,
timeout: Duration(seconds: 10),
);
Retry with Callback #
await retry(
() => fetchData(),
onRetry: (attempt, error, delay) {
print('Attempt $attempt failed: $error. Retrying in $delay...');
},
);
Retry with Backoff (Convenience) #
final result = await retryWithBackoff(() => fetchData());
Pre-configured with 5 attempts, 1 second initial delay, 2x backoff multiplier, and jitter enabled.
Retry Only Specific Exceptions #
final result = await retry(
() => fetchData(),
retryIf: (e) => e is SocketException,
);
Circuit Breaker #
final breaker = CircuitBreaker(
failureThreshold: 3,
resetTimeout: Duration(seconds: 30),
);
try {
final result = await breaker.execute(() => fetchData());
} on CircuitBreakerOpenException {
print('Circuit is open, skipping call');
}
API #
| 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 |
Development #
dart pub get
dart analyze --fatal-infos
dart test
Support #
If you find this project useful: