Polly-style resilience pipelines for Del ...

Polly-style resilience pipelines for Delphi 10.3 Rio and later

Sep 03, 2026

A resilience pipeline library. Combine Retry, Timeout, Circuit Breaker, Fallback, Rate Limiter, Concurrency Limiter, and Hedging in one fluent pipeline for REST, SOAP, database, and other remote calls.

image

Installation

Add the src folder to your project search path:

uses
  DFResilience;

You can also compile packages/DFResilience.dpk.

Quick start

TDFResiliencePipeline
  .Create
  .AddRetry(3)
  .AddTimeout(5000)
  .AddCircuitBreaker(5, 30000)
  .Execute(
    procedure
    begin
      CallApi;
    end
  );

Full pipeline

Build once and reuse the pipeline when a strategy keeps state, such as a circuit breaker:

var
  Pipeline: TDFResiliencePipeline;
  Response: string;
begin
  Pipeline :=
    TDFResiliencePipeline.Create
      .AddRetry(
         TDFRetryOptions.Create
           .MaxAttempts(5)
           .Delay(1000)
           .Backoff(TDFBackoffType.Exponential)
           .UseJitter(True))
      .AddTimeout(10000)
      .AddCircuitBreaker(5, 60000)
      .Build;
  Response := Pipeline.Execute<string>(
    function: string
    begin
      Result := IdHTTP1.Get('https://api.example.com/health');
    end);
end;

Order matters: the first strategy you add is the outermost. AddRetry then AddTimeout means every retry attempt has its own timeout.

Strategies

StrategyWhat it doesSimple APIRetryRetries transient failures.AddRetry(3)TimeoutRaises EDFTimeoutException when the limit is exceeded.AddTimeout(5000)Circuit BreakerFails fast after a threshold.AddCircuitBreaker(5, 30000)FallbackRuns an alternative path on failure.AddFallback(...)Rate LimiterCaps calls inside a time window.AddRateLimiter(100, 60000)Concurrency LimiterCaps work running at the same time.AddConcurrencyLimiter(10, 20)HedgingStarts a parallel attempt if a call is slow.AddHedging(2, 2000)AddRetry(3) means 3 total attempts (do not confuse this with Polly’s MaxRetryAttempts).

For timeout cancellation, use Context.Token or Context.ThrowIfCancellationRequested inside the callback. If the callback ignores the token, work may continue in the background; the pipeline still raises EDFTimeoutException.

Exceptions

  • EDFTimeoutException

  • EDFBrokenCircuitException (RetryAfterMs)

  • EDFIsolatedCircuitException

  • EDFRateLimitedException (RetryAfterMs)

Retry handles all exceptions except a broken circuit and EAbort. To filter:

.AddRetry(
  TDFRetryOptions.Create
    .MaxAttempts(5)
    .Handle(TDFPredicateBuilder.Create.Handle(EIdHTTPProtocolException).Build))

Requirements

  • Delphi 10.3 Rio or later

  • RTL (System.Threading, System.SyncObjs, generics)

https://github.com/delphifanforum/DFResilience

Ti piace questo post?

Offri un caffè a DelphiFan Forum

Altro da DelphiFan Forum