Class ShieldBuilder<TResult>
- Namespace
- Kevlar
- Assembly
- Kevlar.dll
An immutable exception and result handling clause under construction for a
Shield<TResult> chain. Obtained via the When/WhenResult methods on
Shield<TResult> and finished by adding a strategy. The clause becomes the shield's
ambient handling for the strategy added here and reactive strategies chained afterwards.
public sealed class ShieldBuilder<TResult>
Type Parameters
TResult
- Inheritance
-
ShieldBuilder<TResult>
- Inherited Members
Remarks
Start a clause with When… on a shield, then continue it with Or… on this builder:
Shield.For<T>().When<A>().OrResult(r => …).Retry(3).
The builder is immutable. Each Or… returns a new builder holding the terms
accumulated so far plus the one just added, and leaves the builder it was called on untouched.
A builder held in a variable can therefore be branched into two chains safely: each branch gets
only its own terms. The corollary is that code must use the builder each Or…
returns — calling Or… and discarding the result adds nothing to anything.
Adding a strategy freezes the clause of that builder, so a shield already built is never
changed by further chaining either.
One strategy can opt out of the ambient clause: setting HandlesException or
HandlesResult on its options (RetryOptions<TResult>,
CircuitBreakerOptions<TResult>, HedgeOptions<TResult>,
FallbackOptions<TResult>) makes that strategy ignore the clause and handle only
what its own predicates select. Every other strategy in the chain keeps using the ambient clause.
Methods
CircuitBreaker(Action<CircuitBreakerOptions<TResult>>)
Adds a circuit breaker strategy configured via configure.
public Shield<TResult> CircuitBreaker(Action<CircuitBreakerOptions<TResult>> configure)
Parameters
configureAction<CircuitBreakerOptions<TResult>>
Returns
- Shield<TResult>
CircuitBreaker(int, TimeSpan)
Breaks the circuit for breakDuration after consecutiveFailures consecutive handled outcomes.
public Shield<TResult> CircuitBreaker(int consecutiveFailures, TimeSpan breakDuration)
Parameters
Returns
- Shield<TResult>
ConcurrencyLimit(Action<ConcurrencyLimitOptions>)
Adds a configured concurrency limit. The handling clauses remain ambient for later strategies.
public Shield<TResult> ConcurrencyLimit(Action<ConcurrencyLimitOptions> configure)
Parameters
configureAction<ConcurrencyLimitOptions>
Returns
- Shield<TResult>
ConcurrencyLimit(int, int)
Caps concurrency. The handling clauses remain ambient for later strategies.
public Shield<TResult> ConcurrencyLimit(int maxConcurrency, int queueLimit = 0)
Parameters
Returns
- Shield<TResult>
Fallback(Func<Outcome<TResult>, CancellationToken, ValueTask<TResult>>)
Replaces handled outcomes with the result of fallback, which receives the handled outcome.
public Shield<TResult> Fallback(Func<Outcome<TResult>, CancellationToken, ValueTask<TResult>> fallback)
Parameters
fallbackFunc<Outcome<TResult>, CancellationToken, ValueTask<TResult>>
Returns
- Shield<TResult>
Fallback(Func<Outcome<TResult>, CancellationToken, ValueTask<TResult>>, Action<FallbackOptions<TResult>>)
Replaces handled outcomes with the result of fallback, which receives
the handled outcome, and configures notifications.
public Shield<TResult> Fallback(Func<Outcome<TResult>, CancellationToken, ValueTask<TResult>> fallback, Action<FallbackOptions<TResult>> configure)
Parameters
fallbackFunc<Outcome<TResult>, CancellationToken, ValueTask<TResult>>configureAction<FallbackOptions<TResult>>
Returns
- Shield<TResult>
Fallback(Func<CancellationToken, ValueTask<TResult>>)
Replaces handled outcomes with the result of fallback.
public Shield<TResult> Fallback(Func<CancellationToken, ValueTask<TResult>> fallback)
Parameters
fallbackFunc<CancellationToken, ValueTask<TResult>>
Returns
- Shield<TResult>
Fallback(Func<CancellationToken, ValueTask<TResult>>, Action<FallbackOptions<TResult>>)
Replaces handled outcomes with the result of fallback and configures notifications.
public Shield<TResult> Fallback(Func<CancellationToken, ValueTask<TResult>> fallback, Action<FallbackOptions<TResult>> configure)
Parameters
fallbackFunc<CancellationToken, ValueTask<TResult>>configureAction<FallbackOptions<TResult>>
Returns
- Shield<TResult>
FallbackTo(TResult)
Replaces handled outcomes with fallbackValue.
public Shield<TResult> FallbackTo(TResult fallbackValue)
Parameters
fallbackValueTResult
Returns
- Shield<TResult>
FallbackTo(TResult, Action<FallbackOptions<TResult>>)
Replaces handled outcomes with fallbackValue and configures notifications.
public Shield<TResult> FallbackTo(TResult fallbackValue, Action<FallbackOptions<TResult>> configure)
Parameters
fallbackValueTResultconfigureAction<FallbackOptions<TResult>>
Returns
- Shield<TResult>
Hedge(Action<HedgeOptions<TResult>>)
Adds a hedging strategy configured via configure.
public Shield<TResult> Hedge(Action<HedgeOptions<TResult>> configure)
Parameters
configureAction<HedgeOptions<TResult>>
Returns
- Shield<TResult>
Hedge(int, TimeSpan)
Races concurrent attempts; a handled outcome launches the next attempt immediately.
public Shield<TResult> Hedge(int maxHedgedAttempts, TimeSpan delay)
Parameters
Returns
- Shield<TResult>
Or(Func<Exception, bool>)
Returns a new builder that also handles exceptions matching predicate, whatever their type.
public ShieldBuilder<TResult> Or(Func<Exception, bool> predicate)
Parameters
Returns
- ShieldBuilder<TResult>
OrContext(Func<HandlingEvent<TResult>, bool>)
Returns a new builder that also handles outcomes selected using execution context.
public ShieldBuilder<TResult> OrContext(Func<HandlingEvent<TResult>, bool> predicate)
Parameters
predicateFunc<HandlingEvent<TResult>, bool>
Returns
- ShieldBuilder<TResult>
OrInner<TException>()
Returns a new builder that also handles exceptions containing an exception of type TException.
public ShieldBuilder<TResult> OrInner<TException>() where TException : Exception
Returns
- ShieldBuilder<TResult>
Type Parameters
TException
Remarks
The outer exception, ordinary inner-exception chains, and every branch of an AggregateException are searched.
OrInner<TException>(Func<TException, bool>)
Returns a new builder that also handles exceptions containing an exception of type TException matching predicate.
public ShieldBuilder<TResult> OrInner<TException>(Func<TException, bool> predicate) where TException : Exception
Parameters
Returns
- ShieldBuilder<TResult>
Type Parameters
TException
Remarks
The outer exception, ordinary inner-exception chains, and every branch of an AggregateException are searched.
OrResult(Func<TResult, bool>)
Returns a new builder that also handles results matching predicate.
public ShieldBuilder<TResult> OrResult(Func<TResult, bool> predicate)
Parameters
Returns
- ShieldBuilder<TResult>
OrResultContext(Func<HandlingEvent<TResult>, bool>)
Returns a new builder that also handles results selected using execution context.
public ShieldBuilder<TResult> OrResultContext(Func<HandlingEvent<TResult>, bool> predicate)
Parameters
predicateFunc<HandlingEvent<TResult>, bool>
Returns
- ShieldBuilder<TResult>
OrResultEquals(TResult)
Returns a new builder that also handles results equal to result.
public ShieldBuilder<TResult> OrResultEquals(TResult result)
Parameters
resultTResult
Returns
- ShieldBuilder<TResult>
OrResultIsDefault()
Returns a new builder that also handles results equal to default(TResult) — null for reference types.
public ShieldBuilder<TResult> OrResultIsDefault()
Returns
- ShieldBuilder<TResult>
Remarks
For a reference type prefer OrResultIsNull<TResult>(ShieldBuilder<TResult>),
which says what it matches. This overload stays for value types and generic code, where
default(TResult) — 0, false — may or may not be a failure.
Or<TException>()
Returns a new builder that also handles exceptions of type TException.
public ShieldBuilder<TResult> Or<TException>() where TException : Exception
Returns
- ShieldBuilder<TResult>
Type Parameters
TException
Or<TException>(Func<TException, bool>)
Returns a new builder that also handles exceptions of type TException matching predicate.
public ShieldBuilder<TResult> Or<TException>(Func<TException, bool> predicate) where TException : Exception
Parameters
Returns
- ShieldBuilder<TResult>
Type Parameters
TException
RateLimit(Action<RateLimitOptions>)
Adds a configured rate limit. The handling clauses remain ambient for later strategies.
public Shield<TResult> RateLimit(Action<RateLimitOptions> configure)
Parameters
configureAction<RateLimitOptions>
Returns
- Shield<TResult>
RateLimit(int, TimeSpan)
Limits throughput. The handling clauses remain ambient for later strategies.
public Shield<TResult> RateLimit(int permits, TimeSpan perWindow)
Parameters
Returns
- Shield<TResult>
Retry(Action<RetryOptions<TResult>>)
Adds a retry strategy configured via configure.
public Shield<TResult> Retry(Action<RetryOptions<TResult>> configure)
Parameters
configureAction<RetryOptions<TResult>>
Returns
- Shield<TResult>
Remarks
MaxRetries counts retries, not attempts:
MaxRetries = 3 makes up to 4 total attempts — the initial call plus 3 retries.
Retry(int)
Retries handled outcomes up to maxRetries times with the default exponential jittered backoff.
public Shield<TResult> Retry(int maxRetries = 3)
Parameters
maxRetriesintThe number of retries, not the number of attempts:
Retry(3)makes up to 4 total attempts — the initial call plus 3 retries.
Returns
- Shield<TResult>
Retry(int, Backoff)
Retries handled outcomes up to maxRetries times with the given backoff.
public Shield<TResult> Retry(int maxRetries, Backoff backoff)
Parameters
maxRetriesintThe number of retries, not the number of attempts:
Retry(3)makes up to 4 total attempts — the initial call plus 3 retries.backoffBackoffThe delay computation applied between attempts.
Returns
- Shield<TResult>
RetryForever()
Retries handled outcomes indefinitely with the default exponential jittered backoff.
public Shield<TResult> RetryForever()
Returns
- Shield<TResult>
RetryForever(Backoff)
Retries handled outcomes indefinitely with the given backoff.
public Shield<TResult> RetryForever(Backoff backoff)
Parameters
backoffBackoffThe delay computation applied between attempts.
Returns
- Shield<TResult>
Timeout(Action<TimeoutOptions>)
Adds a configured timeout. The handling clause remains ambient for later strategies.
public Shield<TResult> Timeout(Action<TimeoutOptions> configure)
Parameters
configureAction<TimeoutOptions>
Returns
- Shield<TResult>
Timeout(TimeSpan)
Cancels executions that exceed timeout. The handling clauses remain ambient for later strategies.
public Shield<TResult> Timeout(TimeSpan timeout)
Parameters
timeoutTimeSpan
Returns
- Shield<TResult>
Use(Strategy)
Appends a custom strategy. The accumulated handling clause remains ambient for later strategies.
public Shield<TResult> Use(Strategy strategy)
Parameters
strategyStrategy
Returns
- Shield<TResult>
Use(Func<HandlingClause, Strategy>)
Creates and appends a custom strategy using the accumulated handling clause.
public Shield<TResult> Use(Func<HandlingClause, Strategy> factory)
Parameters
factoryFunc<HandlingClause, Strategy>
Returns
- Shield<TResult>