| | | 1 | | using Intentum.Runtime.Resilience.Exceptions; |
| | | 2 | | |
| | | 3 | | namespace Intentum.Runtime.Resilience.CircuitBreaker; |
| | | 4 | | |
| | | 5 | | public sealed class MemoryCircuitBreaker : ICircuitBreaker |
| | | 6 | | { |
| | | 7 | | private readonly CircuitBreakerOptions _options; |
| | 8 | 8 | | private readonly object _lock = new(); |
| | 8 | 9 | | private CircuitState _state = CircuitState.Closed; |
| | | 10 | | private int _failureCount; |
| | | 11 | | private int _halfOpenAttempts; |
| | | 12 | | private DateTime _lastFailureTime; |
| | | 13 | | |
| | 8 | 14 | | public MemoryCircuitBreaker(CircuitBreakerOptions options) |
| | 8 | 15 | | { |
| | 8 | 16 | | _options = options; |
| | 8 | 17 | | } |
| | | 18 | | |
| | | 19 | | public CircuitState State |
| | | 20 | | { |
| | | 21 | | get |
| | 6 | 22 | | { |
| | 6 | 23 | | CheckHalfOpenTransition(); |
| | 6 | 24 | | return _state; |
| | 6 | 25 | | } |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<T> ExecuteAsync<T>(Func<Task<T>> operation) |
| | 11 | 29 | | { |
| | 11 | 30 | | CheckHalfOpenTransition(); |
| | | 31 | | |
| | 11 | 32 | | if (_state == CircuitState.Open) |
| | 1 | 33 | | throw new CircuitBreakerOpenException(); |
| | | 34 | | |
| | | 35 | | try |
| | 10 | 36 | | { |
| | 10 | 37 | | var result = await operation(); |
| | 2 | 38 | | OnSuccess(); |
| | 2 | 39 | | return result; |
| | | 40 | | } |
| | 8 | 41 | | catch |
| | 8 | 42 | | { |
| | 8 | 43 | | OnFailure(); |
| | 8 | 44 | | throw; |
| | | 45 | | } |
| | 2 | 46 | | } |
| | | 47 | | |
| | | 48 | | public void Reset() |
| | 1 | 49 | | { |
| | 1 | 50 | | lock (_lock) |
| | 1 | 51 | | { |
| | 1 | 52 | | _state = CircuitState.Closed; |
| | 1 | 53 | | _failureCount = 0; |
| | 1 | 54 | | } |
| | 1 | 55 | | } |
| | | 56 | | |
| | | 57 | | private void OnSuccess() |
| | 2 | 58 | | { |
| | 2 | 59 | | lock (_lock) |
| | 2 | 60 | | { |
| | 2 | 61 | | if (_state == CircuitState.HalfOpen) |
| | 1 | 62 | | { |
| | 1 | 63 | | _state = CircuitState.Closed; |
| | 1 | 64 | | _failureCount = 0; |
| | 1 | 65 | | _halfOpenAttempts = 0; |
| | 1 | 66 | | } |
| | 2 | 67 | | } |
| | 2 | 68 | | } |
| | | 69 | | |
| | | 70 | | private void OnFailure() |
| | 8 | 71 | | { |
| | 8 | 72 | | lock (_lock) |
| | 8 | 73 | | { |
| | 8 | 74 | | _failureCount++; |
| | 8 | 75 | | _lastFailureTime = DateTime.UtcNow; |
| | | 76 | | |
| | 8 | 77 | | if (_state == CircuitState.HalfOpen) |
| | 1 | 78 | | { |
| | 1 | 79 | | _halfOpenAttempts++; |
| | 1 | 80 | | if (_halfOpenAttempts >= _options.HalfOpenMaxAttempts) |
| | 1 | 81 | | _state = CircuitState.Open; |
| | 1 | 82 | | } |
| | 7 | 83 | | else if (_failureCount >= _options.FailureThreshold) |
| | 6 | 84 | | { |
| | 6 | 85 | | _state = CircuitState.Open; |
| | 6 | 86 | | } |
| | 8 | 87 | | } |
| | 8 | 88 | | } |
| | | 89 | | |
| | | 90 | | private void CheckHalfOpenTransition() |
| | 17 | 91 | | { |
| | 28 | 92 | | if (_state != CircuitState.Open) return; |
| | | 93 | | |
| | 6 | 94 | | lock (_lock) |
| | 6 | 95 | | { |
| | 6 | 96 | | if (_state == CircuitState.Open && |
| | 6 | 97 | | DateTime.UtcNow - _lastFailureTime >= _options.DurationOfBreak) |
| | 3 | 98 | | { |
| | 3 | 99 | | _state = CircuitState.HalfOpen; |
| | 3 | 100 | | } |
| | 6 | 101 | | } |
| | 17 | 102 | | } |
| | | 103 | | } |