question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React to a policy state change from outside

See original GitHub issue

Summary: What are you wanting to achieve? We are trying to implement something similar to what is mentioned in #287, taking into account Polly’s circuit breakers to reach a quorum on a system uptime. Our plan was to use the existing Circuit Breakers, wrapping them in a new CB policy that we could add OnBreak callbacks and react accordingly. This is because we want to let the application create their own circuit breakers and pass them in.

What code or approach do you have so far?
We have two parts, an application with a connection to a system, using a circuit breaker:

        var delay = Backoff.DecorrelatedJitterBackoffV2(settings.MedianFirstRetryDelay, settings.RetryCount);
        var retry = RetryPolicyBuilder(settings.ErrorCodes).WaitAndRetryAsync(delay, onRetry: (ex, time) => Log.Warning(ex, "Error processing request. Waiting for {RetryTimeSpan} before the next retry", time));

        var breaker = CircuitBreakerPolicyBuilder(settings.ErrorCodes).CircuitBreakerAsync(settings.ExceptionsAllowedBeforeBreaking, settings.DurationOfBreak,
            onBreak: (_, duration) => Log.Warning("Circuit breaker tripped. Circuit is open and requests won't be allowed through for {DurationOfBreak}", duration),
            onReset: () => Log.Information("Circuit closed. Requests are now allowed through"),
            onHalfOpen: () => Log.Information("Circuit is now half-opened and will test the service with the next request")
        );

        var policy = breaker.WrapAsync(retry);

        // Somewhere else
        await policy.ExecuteAsync(DoSomething)

And a library that receives a policy per system, and wraps it in a new policy updating the quorum

public void WithCircuitBreaker(Dictionary<string, ICircuitBreakerPolicy> policies)
{
    var wrapperPolicies = new Dictionary<string, ICircuitBreakerPolicy>();
    foreach (var policy in policies)
    {
        var wrapperPolicy = Policy.Handle<Exception>()
                                                .CircuitBreakerAsync(1, TimeSpan.FromMilliseconds(500), 
                                                    onBreak: IncreaseBrokenCount);
        var p = wrapperPolicy.Wrap(policy.Value);

        wrapperPolicies.Add(policy.Key, policy.Value);
    }
}

Doing this, however, the application retains the original policy, and the wrapper policy is never executed.

Is there any way of reacting to state changes in a Circuit Breaker policy, without creating a new policy wrapping it? Or to modify the onBreak callbacks to add extra behaviour to them?

Regards

Issue Analytics

  • State:open
  • Created 3 months ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
PeterCsalaHbocommented, Jun 26, 2023

@vguzmanp Unfortunately I’m unaware of any other simpler solution 😦

1reaction
PeterCsalaHbocommented, Jun 26, 2023

In case of V7 all onXYZ callbacks of CB are NOT allowing you to do state change. You can however manually control the CB’s state outside of the policy via the Isolate and Reset calls.

If you want to have the above described functionality then you could try the following:

  • Each individual CB should register itself into a singleton component
  • Individual CB’s onBreak should call into the component to indicate a failure
    • When the quorum is reached the singleton component should call the Isolate method on each CB
      • Please bear in mind the normal CB counts consecutive failures << this is pretty hard to do in the singleton

The problematic part here is the Half-Open state:

  • Either you have Open and Closed states (after a predefined period of time the singleton calls Reset on each CB)
  • Or you have to choose randomly a CB, call Reset on it and hope that instance will be used
    • All CBs’ onReset should call into the singleton component to reset the rest of the CBs

The solution is complex and fragile. If possible I would try to avoid to introduce this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set React component state from outside of component
By definition, state is not accessible from outside the component. And always copying props to state is not recommended.
Read more >
Reacting to Azure Policy state change events
See Route policy state change events to Event Grid with Azure CLI for a full tutorial.
Read more >
How to Send State of Current Component as a Parameter ...
In React, the state of the component is an object that contains some data. The state may change over the lifecycle of the...
Read more >
Testing state changes in React functional components
This tutorial demonstrates how to test your React functional components and the state changes for components that use hooks, with Jest and ...
Read more >
React.js for Beginners — Props and State Explained
A change in the state happens based on user-input, triggering an event, and so on. Also, React components (with state) are rendered based...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found