question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce Hedging Strategy

See original GitHub issue

We 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 the PolicyBuilder which builds new instances of the Async<PolicyName>:
    • e.g. for Hedging: AsyncHedgingSyntax exposes methods to create AsyncHedgingPolicy instances.
  • 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

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:

  1. Start executing task [first one first]
  2. Wait up to HedgingDelay ms
  3. 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).
  • B. Not Completed:
    • Next task will be executed - GOTO [1.]
  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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
joelhulencommented, Apr 25, 2023

I have no objections, either. In fact, I find that any contrib project migrating to core is a win 😃

1reaction
martintmkcommented, Apr 26, 2023

The hedging will be adopted directly in Polly.Core for V8. The API will be adjusted to conform to v8 shape.

  • Options based
  • Hedging extensions for ResilienceStrategyBuilder
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found