Contrib Package for Hedging Policy
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>Syntaxexposes extension method for thePolicyBuilderwhich builds new instances of theAsync<PolicyName>:- e.g. for Hedging:
AsyncHedgingSyntaxexposes methods to createAsyncHedgingPolicyinstances.
- e.g. for Hedging:
- The generic
Async<PolicyName>Policyis the actual policy, which:- Inherits the
AsyncPolicy<TResult>base class - Exposes internal constructor and it is created only via the
Async<PolicyName>Syntaxextensions. - Implements the abstract method of the base class,
Async<PolicyName>Syntaxwhich 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);
Sorry for the delayed reply.
This sounds interesting, but at this time I don't think this is something we'd have the bandwidth to add to a shipping version of Polly any time soon, but does sound like a candidate for something over in https://github.com/Polly-Contrib.
I don't know what the policies etc. are for dealing with that, I don't have access to that.
Maybe @joelhulen, @DreamOfTheRedChamber, @mebjas or @vany0114 could help you out.
I also apologize for the late reply. I just saw this issue.
I can help you get up and running under Polly-Contrib, which is our officially recognized community projects that extend Polly's core functionality. Any Polly.Contrib NuGet packages are recognized as part of the Polly organization as well. Please reach out to me at joelhulen at gmail.com
Thanks!
@joelhulen : sent you an email, we are interested in this feature as well and can contribute.
@joelhulen I sent you an email last month, not sure if you got it. Please let me know :).
Hey, everyone. Sorry I missed this thread. I have created the new Polly.Contrib project here: https://github.com/Polly-Contrib/Polly.Contrib.RequestHedging and added both Alex and Laura. I will work with both of them to make sure they can successfully publish official Polly.Contrib NuGet packages.
Thanks! Joel
On Thu, Sep 30, 2021 at 7:22 AM Laura @.***> wrote:
@joelhulen https://github.com/joelhulen I sent you an email last month, not sure if you got it. Please let me know :).
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/App-vNext/Polly/issues/876#issuecomment-931232245, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABNMWYGMXWITMVFXE244LETUERB63ANCNFSM5BIWPLRQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.
I have created a new repo generically for Hedging vs. the more specific RequestHedging I had previously created (and archived). @laura-mi, I've added you as an admin to this repo: https://github.com/Polly-Contrib/Polly.Contrib.Hedging per your request.
Since the hedging strategy will be implemented by us as maintainers and it doesn't have any additional dependencies, we were discussing to include hedging directly in the new Polly.Core package. We wouldn't need to setup all the build and test infra and it will also simplify future maintenance.
@joelhulen, @martincostello, @laura-mi any objections?
No objections from me.
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.
- Options based
- Hedging extensions for
ResilienceStrategyBuilder
The hedging will be adopted directly in
Polly.Corefor V8. The API will be adjusted to conform to v8 shape.
- Options based
- Hedging extensions for
ResilienceStrategyBuilder
Is it still possible to use the Contrib.Hedging nuget somehow? I recall it had the ability to issue the hedging request if the duration crossed the duration of some percentile.
I.e. the hedging policy would record what is the pXX and use it as the threshold for issuing the hedging request.
I don't see this in the v8 docs anymore.
Thank you !
I don't think there is any hedging available for Polly V7. It was considered but never published.
I don't think there is any hedging available for Polly V7. It was considered but never published.
are you sure? I remember a repo Polly.Contrib.Hedging, which is actually mentioned here, and I recall the code was implemented there, long before Polly 8 was released.
Is it possible to get access this code somehow?
The repo is here, and it's been archived: https://github.com/Polly-Contrib/Polly.Contrib.RequestHedging
The contrib stuff is maintained independently from the core Polly library.
AFAIK, no contrib libraries have been updated to be compatible with the new APIs in Polly.Core for v8.