Introduce Hedging Strategy
See original GitHub issueWe are working on the development of a generic HedgingPolicy
to use as part of our HttpClient
Resilience pipeline.
Since the hedging as a policy is not yet supported by Polly we consider setting up a new package dedicated to the generic implementation of such policy. Please let us know if you are interested in having this and how we could proceed further.
Summary The hedging policy will allow the sequential and delayed execution of a set of tasks, until one of them completes successfully or until all of them are completed. First task that is successfully completed triggers the cancellation and disposal of all other tasks and/or adjacent allocated resources.
General structure of polly policy:
- The
Async<PolicyName>Syntax
exposes extension method for thePolicyBuilder
which builds new instances of theAsync<PolicyName>
:- e.g. for Hedging:
AsyncHedgingSyntax
exposes methods to createAsyncHedgingPolicy
instances.
- e.g. for Hedging:
- The generic
Async<PolicyName>Policy
is the actual policy, which:- Inherits the
AsyncPolicy<TResult>
base class - Exposes internal constructor and it is created only via the
Async<PolicyName>Syntax
extensions. - Implements the abstract method of the base class,
Async<PolicyName>Syntax
which contains the actual implementation, i.e.Task<TResult> ImplementationAsync( Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
- e.g. for Hedging:
AsyncHedgingPolicy
- e.g. for Hedging:
- Inherits the
Hedging policy configuration
The base implementation will take:
HedgingDelay
- how long shall one wait before spawning a new hedged request;OnHedgingAsync
- task performed after every spawning a hedged request; [can be used for logging & telemetry]- A list of
hedgedTaskFunctions
- the tasks performed via hedging if the primary execution delegate fails.
Hedging logic
Steps:
- Start executing task [first one first]
- Wait up to HedgingDelay ms
- If the request is:
- A. Completed:
- If result is an exception
- If ShouldHandleException - GOTO [B]
- else - GOTO [4.]
- If result is not exception
- If ShouldHandleResult - GOTO [B]
- else - GOTO [4.] (e.g. result is successful, so return result).
- If result is an exception
- B. Not Completed:
- Next task will be executed - GOTO [1.]
- The result of the execution can be:
- [Success]
- First request to complete successfully triggers the cancellation of all others
- Return successful result.
- [Failure]
- No request completed successfully, and all requests have been executed.
- Return the failure of the first task.
Exposed API for policy creation:
var hedgingPolicy = Policy
.Handle<Exception>()
.OrResult<string>(_ => false)
.AsyncHedgingPolicy(
hedgedTaskFunctions,
defaultHedgingDelay,
onHedgingAsync);
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:10 (7 by maintainers)
Top Results From Across the Web
Beginner's Guide to Hedging: Definition and Example of ...
Hedging is a risk management strategy employed to offset losses in investments by taking an opposite position in a related asset.
Read more >Introduction to Hedging Investments Guide
A hedge is a position that offsets the risk of another position in a portfolio. A balanced and diversified portfolio is considered an...
Read more >Introduction to Hedging
Buying a protective put and reducing the cost by selling an out-of-the-money call. A collar limits upside gain, but reduces the cost of...
Read more >Hedging - Definition, How It Works and Examples of ...
Hedging is an important protection that investors can use to protect their investments from sudden and unforeseen changes in financial markets.
Read more >Hedging & income strategies | Derivatives
Hedging strategies A hedging strategy involves protecting a stock position from risk ... Here's a quick video introduction to this type of option...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have no objections, either. In fact, I find that any contrib project migrating to core is a win 😃
The hedging will be adopted directly in
Polly.Core
for V8. The API will be adjusted to conform to v8 shape.ResilienceStrategyBuilder