Skip to main content

ZeroAlloc.Resilience

Source-generated, zero-allocation resilience policies for .NET.

Add [Retry], [Timeout], [RateLimit], and [CircuitBreaker] to an interface; the generator emits a proxy composing all policies in a fixed order, whatever order the attributes are written in, with no heap allocation on the happy path.

Quick example​

[Retry(MaxAttempts = 3, BackoffMs = 200, Jitter = true)]
[Timeout(Ms = 5_000)]
[CircuitBreaker(MaxFailures = 5, ResetMs = 1_000, HalfOpenProbes = 1, Fallback = nameof(FetchFallback))]
public interface IExternalService
{
ValueTask<string> FetchAsync(string id, CancellationToken ct);
ValueTask<string> FetchFallback(string id, CancellationToken ct);
}

// Register — one line
services.AddExternalServiceResilience<ExternalServiceImpl>();

// Inject IExternalService — all policies are wired automatically

Contents​

PageDescription
Getting StartedInstall, annotate an interface, register with DI
Attributes[Retry], [Timeout], [RateLimit], [CircuitBreaker] reference
Source GeneratorWhat the generator emits — annotated proxy example
PerformanceBenchmark results and allocation profile

Core Concepts​

PageDescription
RetryExponential backoff, jitter, per-attempt timeout, exhaustion behaviour
TimeoutTotal timeout vs. per-attempt timeout, CTS lifecycle
Rate LimitToken bucket, lock-free acquire, Shared vs Instance scope
Circuit BreakerState machine, failure tracking, half-open probing, fallback
Execution OrderHow policies compose: RateLimit → CB → Timeout → Retry

Guides​

PageDescription
FallbackDeclare and implement circuit-breaker fallback methods
Method-Level OverridesPer-method policy configuration
Result Return TypesNon-throwing failure path with Result, Result<T> and Result<T, E>
DI RegistrationGenerated extension method, overrides, manual construction

Diagnostics​

IDSeverityDescription
ZR0001ErrorFallback method not found or signature mismatch
ZR0002WarningTimeout configured but method has no CancellationToken
ZR0003ErrorPolicy cannot build a failure for this Result<T, E> error type
ZR0004ErrorInvalid policy attribute value
ZR0006WarningPolicy not applied to method
ZR0007ErrorInterface shape not supported by the resilience generator
ZR0008ErrorInvalid ZeroAllocGeneratedAccessibility value
ZR0009ErrorRetry member not found or signature mismatch
ZR0010Error / WarningResult-aware retry cannot apply to method
ZR0011Warning[RetryAttempt] has no effect without [Retry]
ZR0012Error[RetryAttempt] parameter type not supported
ZR0013WarningRethrowDeclined has no effect
ZR0014ErrorPolicy not supported on a stream method
ZR0015WarningPolicy not applied to a stream method