Circuit Breaker Generator
The circuit breaker generator creates a strongly typed CircuitBreakerPolicy<TResult> factory from declarative attributes. It is useful when resilience rules should live next to the operation contract while still producing the same runtime policy as the fluent API.
[GenerateCircuitBreakerPolicy(
typeof(FulfillmentResponse),
FactoryMethodName = "CreateGeneratedPolicy",
PolicyName = "fulfillment-gateway",
FailureThreshold = 2,
BreakDurationMilliseconds = 30000)]
public static partial class FulfillmentCircuitBreakerPolicy
{
[CircuitBreakerResultPredicate]
private static bool ShouldOpen(FulfillmentResponse response)
=> response.StatusCode == 408 || response.StatusCode == 429 || response.StatusCode >= 500;
[CircuitBreakerExceptionPredicate]
private static bool ShouldOpen(Exception exception)
=> exception is TimeoutException;
}
The generated factory returns PatternKit.Cloud.CircuitBreaker.CircuitBreakerPolicy<FulfillmentResponse> and applies any declared result and exception predicates.
Rules
- The host type must be
partial. FailureThresholdmust be at least1.BreakDurationMillisecondsmust be non-negative.- Result predicates must be
static boolmethods with oneTResultparameter. - Exception predicates must be
static boolmethods with oneExceptionparameter.
Diagnostics use the PKCB prefix.