Polly icon indicating copy to clipboard operation
Polly copied to clipboard

Unable to pass context to OnHalfOpen in AsyncAdvancedCircuitBreakerTResultSyntax

Open hikarunoryoma opened this issue 4 years ago • 1 comments

Summary: I am trying to pass metadata through my Polly Context in OnHalfOpen, however I find that there is no valid method for this. I would expect that I would be able to pass context through m OnHalfOpen action method.

Expected behavior: I would expect that I would be able to pass context through m OnHalfOpen action method.

Actual behaviour:

Get an error stating: 'PolicyBuilder<HttpResponseMessage>' does not contain a definition for 'AdvancedCircuitBreakerAsync' and the best extension method overload 'AsyncAdvancedCircuitBreakerSyntax.AdvancedCircuitBreakerAsync(PolicyBuilder, double, TimeSpan, int, TimeSpan, Action<Exception, TimeSpan>, Action, Action)' requires a receiver of type 'PolicyBuilder'

Steps / Code to reproduce the problem: The following won't compile, but I think it should

var circuitBreakerPolicy = Policy
	.HandleResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
	.Or<HttpRequestException>()
	.AdvancedCircuitBreakerAsync(
		failureThreshold: 1,
		samplingDuration: TimeSpan.FromSeconds(1),
		minimumThroughput: 1,
		durationOfBreak: TimeSpan.FromSeconds(1)
		onReset: context => DoSomething(context),
		onHalfOpen: context => DoSomething(context),
		onBreak: (result, breakInterval, context) => DoSomething(context)
	);

I'll note that the following is Valid so pretty sure it's something to do with onHalfOpen

var circuitBreakerPolicy = Policy
	.HandleResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
	.Or<HttpRequestException>()
	.AdvancedCircuitBreakerAsync(
		failureThreshold: 1,
		samplingDuration: TimeSpan.FromSeconds(1),
		minimumThroughput: 1,
		durationOfBreak: TimeSpan.FromSeconds(1)
		onReset: context => DoSomething(context),
		onHalfOpen: () => DoNothing(),
		onBreak: (result, breakInterval, context) => DoSomething(context)
	);

hikarunoryoma avatar Aug 20 '21 15:08 hikarunoryoma

I had a quick look at this (see #883). As well as requiring a lot of new overloads to be added for consistency, the current design of the circuit breaker doesn't appear to have a way to pass the Context through to the code the calls the delegate, so would need the implementation to also be refactored.

The request to be able to do this seems reasonable, but I'm not how practical adding it to the v7 code base would be.

I might have another tinker with it in the next few days, but I'd be hesitant to have to poke a lot of holes in the current design to get this working, which might also be why this isn't currently supported.

martincostello avatar Aug 20 '21 16:08 martincostello

@martintmk Is this use case catered for in v8?

martincostello avatar Jun 16 '23 15:06 martincostello

Yes, this is already supported.

martintmk avatar Jun 16 '23 16:06 martintmk