private void SetCurrentState(CircuitBreakerStateDescriptor newState) { if (stateDescriptor != newState) { stateDescriptor = newState; logger.Event("CircuitBreakerTransition", ("circuitBreakerType", key), ("circuitBreakerState", newState)); } }
public Task <CircuitBreakerStateDescriptor> GetLastStateAsync() { if (stateDescriptor == null) { stateDescriptor = new CircuitBreakerStateDescriptor(CircuitBreakerState.Closed, Clock.Current.Now()); } return(Task.FromResult(stateDescriptor)); }
public async Task SignalSuccessAsync() { await Task.Yield(); if (stateDescriptor?.State != CircuitBreakerState.Closed) { var newState = new CircuitBreakerStateDescriptor(stateDescriptor?.State == CircuitBreakerState.Open ? CircuitBreakerState.HalfOpen : CircuitBreakerState.Closed, Clock.Current.Now()); SetCurrentState(newState); } NotifyState(); }
public async Task SignalFailureAsync(TimeSpan expiry) { await Task.Yield(); var open = new CircuitBreakerStateDescriptor(CircuitBreakerState.Open, Clock.Current.Now(), expiry); Clock.Current.Schedule(_ => { if (ReferenceEquals(stateDescriptor, open)) { var halfOpen = new CircuitBreakerStateDescriptor(CircuitBreakerState.HalfOpen, Clock.Current.Now()); SetCurrentState(halfOpen); NotifyState(); } }, expiry); SetCurrentState(open); NotifyState(); }
public CircuitBreakerStoragePartition(string key) { this.key = key; subscribers = new ConcurrentSet <CircuitBreakerBrokerSubscriber>(); stateDescriptor = new CircuitBreakerStateDescriptor(CircuitBreakerState.Closed, Clock.Current.Now(), TimeSpan.FromMinutes(2)); }
private void StateChanged(CircuitBreakerStateDescriptor descriptor) => stateDescriptor = descriptor;