Skip to content

Hedging

Arnel Robles edited this page Sep 29, 2026 · 1 revision

Hedging (Masse)

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);

Settings

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"

Behaviour

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.

Clone this wiki locally