polly_dart library

A Dart port of Polly (.NET resilience library) providing strategies like Retry, Circuit Breaker, Timeout, Rate Limiter, Hedging, and Fallback.

This library provides resilience and transient-fault-handling capabilities for Dart applications, allowing developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent manner.

Quick Start

import 'package:polly_dart/polly_dart.dart';

// Create a resilience pipeline
final pipeline = ResiliencePipelineBuilder()
    .addRetry(RetryStrategyOptions(maxRetryAttempts: 3))
    .addTimeout(Duration(seconds: 10))
    .build();

// Execute with resilience
final result = await pipeline.execute((context) async {
  // Your code here
  return await someAsyncOperation();
});

Classes

BreakDurationGeneratorArguments<T>
Arguments passed to break duration generator functions.
CircuitBreakerManualControl
Provides manual control over the circuit breaker.
CircuitBreakerStateProvider
Provides access to the current circuit breaker state.
CircuitBreakerStrategy
Circuit breaker resilience strategy implementation.
CircuitBreakerStrategyOptions<T>
Configuration options for the circuit breaker strategy.
DelayGeneratorArguments<T>
Arguments passed to delay generator functions.
FallbackActionArguments<T>
Arguments passed to fallback action functions.
FallbackActionHelper
Helper methods for creating common fallback actions.
FallbackStrategy
Fallback resilience strategy implementation.
FallbackStrategyOptions<T>
Configuration options for the fallback strategy.
HedgingActionGeneratorArguments<T>
Arguments passed to hedging action generator functions.
HedgingDelayGeneratorArguments<T>
Arguments passed to hedging delay generator functions.
HedgingStrategy
Hedging resilience strategy implementation.
HedgingStrategyOptions<T>
Configuration options for the hedging strategy.
OnCircuitClosedArguments<T>
OnCircuitHalfOpenedArguments<T>
OnCircuitOpenedArguments<T>
Arguments for circuit state change callbacks.
OnFallbackArguments<T>
Arguments passed to fallback callback functions.
OnHedgingArguments<T>
Arguments passed to hedging callback functions.
OnRateLimiterRejectedArguments
Arguments passed to rate limiter rejection callbacks.
OnRetryArguments<T>
Arguments passed to retry callback functions.
OnTimeoutArguments
Arguments passed to timeout callback functions.
Outcome<T>
Represents the outcome of an operation, either a successful result or an exception.
PredicateBuilder<T>
A helper class for building predicates that determine whether a strategy should handle specific outcomes.
PredicateHelper
Helper methods for creating common predicates.
RateLimiterStrategy
Rate limiter resilience strategy implementation.
RateLimiterStrategyOptions
Configuration options for the rate limiter strategy.
ResilienceContext
Context that carries metadata through resilience pipeline execution.
ResiliencePipeline
A resilience pipeline that combines multiple resilience strategies.
ResiliencePipelineBuilder
Builder for creating resilience pipelines.
ResilienceStrategy
Base interface for all resilience strategies.
RetryStrategy
Retry resilience strategy implementation.
RetryStrategyOptions<T>
Configuration options for the retry strategy.
TimeoutGeneratorArguments
Arguments passed to timeout generator functions.
TimeoutStrategy
Timeout resilience strategy implementation.
TimeoutStrategyOptions
Configuration options for the timeout strategy.
TypedResiliencePipeline<T>
A generic resilience pipeline that is typed for a specific result type.
TypedResiliencePipelineBuilder<T>
Generic builder for creating typed resilience pipelines.

Enums

CircuitState
Possible states of the circuit breaker.
DelayBackoffType
Types of delay backoff strategies.
RateLimiterType
Types of rate limiters.

Typedefs

BreakDurationGenerator<T> = Future<Duration> Function(BreakDurationGeneratorArguments<T> args)
Signature for break duration generator functions.
DelayGenerator<T> = Future<Duration?> Function(DelayGeneratorArguments<T> args)
Signature for delay generator functions.
FallbackAction<T> = Future<Outcome<T>> Function(FallbackActionArguments<T> args)
Signature for fallback action functions.
HedgingActionGenerator<T> = Future<T> Function(HedgingActionGeneratorArguments<T> args)
Signature for hedging action generator functions.
HedgingDelayGenerator<T> = Future<Duration> Function(HedgingDelayGeneratorArguments<T> args)
Signature for hedging delay generator functions.
OnCircuitClosed<T> = Future<void> Function(OnCircuitClosedArguments<T> args)
OnCircuitHalfOpened<T> = Future<void> Function(OnCircuitHalfOpenedArguments<T> args)
OnCircuitOpened<T> = Future<void> Function(OnCircuitOpenedArguments<T> args)
Callback signatures for circuit state changes.
OnFallbackCallback<T> = Future<void> Function(OnFallbackArguments<T> args)
Signature for fallback callback functions.
OnHedgingCallback<T> = Future<void> Function(OnHedgingArguments<T> args)
Signature for hedging callback functions.
OnRateLimiterRejected = Future<void> Function(OnRateLimiterRejectedArguments args)
Signature for rate limiter rejection callback.
OnRetryCallback<T> = Future<void> Function(OnRetryArguments<T> args)
Signature for retry callback functions.
OnTimeoutCallback = Future<void> Function(OnTimeoutArguments args)
Signature for timeout callback functions.
ResilienceCallback<T> = Future<T> Function(ResilienceContext context)
Represents a callback that can be executed by a resilience strategy.
ShouldHandlePredicate<T> = bool Function(Outcome<T> outcome)
Represents a predicate that determines whether a strategy should handle a specific outcome.
TimeoutGenerator = Future<Duration> Function(TimeoutGeneratorArguments args)
Signature for timeout generator functions.

Exceptions / Errors

CircuitBreakerRejectedException
Exception thrown when the circuit breaker is open.
OperationCancelledException
Exception thrown when an operation is cancelled.
RateLimiterRejectedException
Exception thrown when rate limiter rejects an execution.
TimeoutRejectedException
Exception thrown when an operation times out.