App-vNext / Polly

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.

Home Page:https://www.thepollyproject.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question]: Primary Hedging Resiliency Context not being propagated to Action Context?

rbarbosapere opened this issue · comments

What are you wanting to achieve?

Hello,
I am trying to extend the resilience strategy to rate limit secondary attempts (from either retry and hedging).
I am taking advantage of the ResiliencyContext to differentiate between a primary and a secondary request.

I am successfully able to do that with the RetryStrategy since it reuses the ResiliencyContext on every attempt.

However, for the HedgingStrategy for some reason the PrimaryContext is not propagated onto ActionContext. So if I modify the context on the primary request, it doesn't reflect on following hedging attempts.

I am wondering if this is the expected behavior?

Looking at this piece of code, looks like we do want to copy properties from primary to active context?

What code or approach do you have so far?

I pushed my draft to this repo: https://github.com/rbarbosapere/hedged-retries-rate-limited
And this is the failing test: "Execute_PipelineWithHedgingStrategy_SecondaryAttemptRatioReached_ShouldRejectSecondaryAttempt"

Additional context

This can also be seem on this Polly repo test. The test will fail if you try to check if ActionContext has the same key and dumb value.

However, for the HedgingStrategy for some reason the PrimaryContext is not propagated onto ActionContext. So if I modify the context on the primary request, it doesn't reflect on following hedging attempts.

The important detail to remember is that primary context is not modified during execution. So if you set properties to action context, another hedged action won't see these changes.

However, what you can do is to store some state into primary context, that represents mutable and thread safe handle. You set this to primary context and then it gets copied to all secondary ones. Then the secondary context can access/modify this state and use it for hedging decisions. It's important to keep the same instance of this state.

See the docs about the behavior of contexts in hedging:
https://www.pollydocs.org/strategies/hedging.html#hedging-and-resilience-context