Repository navigation
Hedging
Package: Carom.Extensions. Starts backup copies of a slow call and takes the first good answer.
using Carom.Extensions;
var masse = Masse.WithAttempts(3)
.After(TimeSpan.FromMilliseconds(100));
var quote = await CaromMasseExtensions.ShotWithHedgingAsync(
ct => pricing.GetQuoteAsync(sku, ct), masse, cancellationToken);| Setting | Default | Notes |
|---|---|---|
WithAttempts(count) |
none, required | Total attempts including the first. At least 1 |
After(delay) |
200 ms | Wait before each extra attempt. Not negative |
WithCancellation(bool) |
true | Cancel the attempts still running once one succeeds |
When(predicate) |
null | Returning true for a result means "not good enough, keep hedging" |
The first attempt starts at once. Each further attempt starts after the delay, even if an earlier attempt already came back with an unsatisfactory result, so hedging never fires faster than the delay allows. Use the Func<CancellationToken, Task<T>> overload so cancelled attempts actually stop.
When nothing succeeds: one failure is rethrown as is, several become an AggregateException, and if every attempt returned a result that When rejected, the last result is returned.
Only hedge calls that are safe to run more than once.
Written from the Carom source. The repository is the source of truth; if a page disagrees with the code, the code wins. Licensed MPL-2.0.
Core
Carom.Extensions
Integrations
Project