在Polly中获取上下文.政策"onHalfOpen"



似乎没有任何AdvancedCircuitBreakerAsync的过载将Context传递到onHalfOpen行动,所以我想知道是否有任何解决方案。

var cB = Policy
.Handle<Exception>()
.AdvancedCircuitBreakerAsync(
getFailureThreshold(),
getSamplingDuration(),
getMinimumThroughput(),
getDurationOfBreak(),
onBreak: (exception, timeSpan, context) =>
{
var logger = context.GetLogger();
logger.LogWarning("In Break");
},
onReset: (context) =>
{
var logger = context.GetLogger();
Logger.LogWarning("In Reset");
},
onHalfOpen: () =>
{
// How do I get the context here?
});

可悲的事实是你不能。如果您查看相关的源代码,那么您可以看到所有可以接受onHalfOpen委托的CircuitBreakerAsync重载都以onHalfOpenAction的方式定义:

public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan> onBreak, Action onReset, Action onHalfOpen)
public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
public static AsyncCircuitBreakerPolicy CircuitBreakerAsync(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, CircuitState, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)

遗憾的是,AdvancedCircuitBreakerAsync也是如此

public static CircuitBreakerPolicy<TResult> AdvancedCircuitBreaker<TResult>(this PolicyBuilder<TResult> policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<DelegateResult<TResult>, TimeSpan> onBreak, Action onReset, Action onHalfOpen)
public static CircuitBreakerPolicy<TResult> AdvancedCircuitBreaker<TResult>(this PolicyBuilder<TResult> policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<DelegateResult<TResult>, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
public static CircuitBreakerPolicy<TResult> AdvancedCircuitBreaker<TResult>(this PolicyBuilder<TResult> policyBuilder, double failureThreshold, TimeSpan samplingDuration, int minimumThroughput, TimeSpan durationOfBreak, Action<DelegateResult<TResult>, CircuitState, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)

有人在2021年提出了同样的问题。请求更新可能是有意义的。


这里有一些同样关注的封闭问题:1,2,…

相关内容

  • 没有找到相关文章

最新更新