Skip to content

Conversation

DavidBoike
Copy link
Member

@DavidBoike DavidBoike commented Sep 30, 2024

The circuit breaker's Success() method must be as close to a no-op as possible on the happy path, but when disarming in a multi-threaded environment still needs to run the disarm logic only once.

Previously, while querying values was done in a thread-safe way, a thread on the Failure method could pause while a Success() call ran to entirety, setting the failure count to 0, and ensuring that further calls to Success() would see a failure count of 0 and take no action, even though the arming action had been executed most recently.

This resulted in a scenario where no number of Success calls could undo the armedAction, unless another Failure call went first.

@DavidBoike DavidBoike self-assigned this Sep 30, 2024

if (oldValue == 0)
// If the failure count was already zero, replace it with zero (no change) and then return original
if (Interlocked.CompareExchange(ref failureCount, 0, 0) == 0)
Copy link
Contributor

@awright18 awright18 Sep 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it would solve both problems.

//if there have been no failures previously then return
if(Interlocked.Read(ref failureCount) == 0 )
{
	return;
}
else //if there were previous failures then it's ok to disarm now.
{
   if(Interlocked.Read(ref failureCount) > 0) //not sure if this check is necessary or not.
   {
	  Interlocked.Exchange(ref failureCount, 0);
   }
}

@danielmarbach
Copy link
Contributor

Here is a hopefully simplified version #1057

@DavidBoike
Copy link
Member Author

Closing in favor of #1057

@DavidBoike DavidBoike closed this Oct 1, 2024
@DavidBoike DavidBoike deleted the circuit-breaker branch October 1, 2024 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants