Retry Generator
The retry generator creates a strongly typed RetryPolicy<TResult> factory from declarative attributes. It is useful when a team wants retry rules to live beside the operation contract while still producing the same runtime policy as the fluent API.
[GenerateRetryPolicy(
typeof(InventoryResponse),
FactoryMethodName = nameof(CreateGeneratedPolicy),
PolicyName = "inventory-availability",
MaxAttempts = 3,
InitialDelayMilliseconds = 25,
BackoffFactor = 2)]
public static partial class InventoryRetryPolicy
{
[RetryResultPredicate]
private static bool ShouldRetry(InventoryResponse response)
=> response.StatusCode == 408 || response.StatusCode == 429 || response.StatusCode >= 500;
[RetryExceptionPredicate]
private static bool ShouldRetry(Exception exception)
=> exception is TimeoutException;
}
The generated factory returns PatternKit.Cloud.Retry.RetryPolicy<InventoryResponse> and applies any declared result and exception predicates.
Rules
- The host type must be
partial. MaxAttemptsmust be at least1.InitialDelayMillisecondsmust be non-negative.BackoffFactormust be at least1.- Result predicates must be
static boolmethods with oneTResultparameter. - Exception predicates must be
static boolmethods with oneExceptionparameter.
Diagnostics use the PKRET prefix.