-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCircuitStateController.cs
358 lines (295 loc) · 11.7 KB
/
CircuitStateController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using Polly.Telemetry;
namespace Polly.CircuitBreaker;
/// <summary>
/// Thread-safe controller that holds and manages the circuit breaker state transitions.
/// </summary>
internal sealed class CircuitStateController<T> : IDisposable
{
private readonly object _lock = new();
private readonly ScheduledTaskExecutor _executor = new();
private readonly Func<OnCircuitOpenedArguments<T>, ValueTask>? _onOpened;
private readonly Func<OnCircuitClosedArguments<T>, ValueTask>? _onClosed;
private readonly Func<OnCircuitHalfOpenedArguments, ValueTask>? _onHalfOpen;
private readonly TimeProvider _timeProvider;
private readonly ResilienceStrategyTelemetry _telemetry;
private readonly CircuitBehavior _behavior;
private readonly TimeSpan _breakDuration;
private readonly Func<BreakDurationGeneratorArguments, ValueTask<TimeSpan>>? _breakDurationGenerator;
private DateTimeOffset _blockedUntil;
private CircuitState _circuitState = CircuitState.Closed;
private Outcome<T>? _lastOutcome;
private Exception? _breakingException;
private bool _disposed;
private int _halfOpenAttempts;
#pragma warning disable S107
public CircuitStateController(
TimeSpan breakDuration,
Func<OnCircuitOpenedArguments<T>, ValueTask>? onOpened,
Func<OnCircuitClosedArguments<T>, ValueTask>? onClosed,
Func<OnCircuitHalfOpenedArguments, ValueTask>? onHalfOpen,
CircuitBehavior behavior,
TimeProvider timeProvider,
ResilienceStrategyTelemetry telemetry,
Func<BreakDurationGeneratorArguments, ValueTask<TimeSpan>>? breakDurationGenerator)
#pragma warning restore S107
{
_breakDuration = breakDuration;
_onOpened = onOpened;
_onClosed = onClosed;
_onHalfOpen = onHalfOpen;
_behavior = behavior;
_timeProvider = timeProvider;
_telemetry = telemetry;
_breakDurationGenerator = breakDurationGenerator;
}
public CircuitState CircuitState
{
get
{
EnsureNotDisposed();
lock (_lock)
{
return _circuitState;
}
}
}
public Exception? LastException
{
get
{
EnsureNotDisposed();
lock (_lock)
{
return _lastOutcome?.Exception;
}
}
}
public Outcome<T>? LastHandledOutcome
{
get
{
EnsureNotDisposed();
lock (_lock)
{
return _lastOutcome;
}
}
}
public ValueTask IsolateCircuitAsync(ResilienceContext context)
{
EnsureNotDisposed();
context.Initialize<T>(isSynchronous: false);
Task? task;
lock (_lock)
{
var exception = new IsolatedCircuitException();
_telemetry.SetTelemetrySource(exception);
SetLastHandledOutcome_NeedsLock(Outcome.FromException<T>(exception));
OpenCircuitFor_NeedsLock(Outcome.FromResult<T>(default), TimeSpan.MaxValue, manual: true, context, out task);
_circuitState = CircuitState.Isolated;
}
return ExecuteScheduledTaskAsync(task, context);
}
public ValueTask CloseCircuitAsync(ResilienceContext context)
{
EnsureNotDisposed();
context.Initialize<T>(isSynchronous: false);
Task? task;
lock (_lock)
{
CloseCircuit_NeedsLock(Outcome.FromResult<T>(default), manual: true, context, out task);
}
return ExecuteScheduledTaskAsync(task, context);
}
public async ValueTask<Outcome<T>?> OnActionPreExecuteAsync(ResilienceContext context)
{
EnsureNotDisposed();
BrokenCircuitException? exception = null;
bool isHalfOpen = false;
Task? task = null;
lock (_lock)
{
// check if circuit can be half-opened
if (_circuitState == CircuitState.Open && PermitHalfOpenCircuitTest_NeedsLock())
{
_halfOpenAttempts++;
_circuitState = CircuitState.HalfOpen;
_telemetry.Report(new(ResilienceEventSeverity.Warning, CircuitBreakerConstants.OnHalfOpenEvent), context, new OnCircuitHalfOpenedArguments(context));
isHalfOpen = true;
}
exception = _circuitState switch
{
CircuitState.Open => CreateBrokenCircuitException(),
CircuitState.HalfOpen when !isHalfOpen => CreateBrokenCircuitException(),
CircuitState.Isolated => new IsolatedCircuitException(),
_ => null
};
if (isHalfOpen && _onHalfOpen is not null)
{
task = ScheduleHalfOpenTask(context);
}
}
await ExecuteScheduledTaskAsync(task, context).ConfigureAwait(context.ContinueOnCapturedContext);
if (exception is not null)
{
_telemetry.SetTelemetrySource(exception);
return Outcome.FromException<T>(exception);
}
return null;
}
public ValueTask OnUnhandledOutcomeAsync(Outcome<T> outcome, ResilienceContext context)
{
EnsureNotDisposed();
Task? task = null;
lock (_lock)
{
_behavior.OnActionSuccess(_circuitState);
// Circuit state handling:
//
// HalfOpen - close the circuit
// Closed - do nothing
// Open, Isolated - A successful call result may arrive when the circuit is open, if it was placed before the circuit broke.
// We take no special action; only time passing governs transitioning from Open to HalfOpen state.
if (_circuitState == CircuitState.HalfOpen)
{
CloseCircuit_NeedsLock(outcome, manual: false, context, out task);
}
}
return ExecuteScheduledTaskAsync(task, context);
}
public ValueTask OnHandledOutcomeAsync(Outcome<T> outcome, ResilienceContext context)
{
EnsureNotDisposed();
Task? task = null;
lock (_lock)
{
SetLastHandledOutcome_NeedsLock(outcome);
_behavior.OnActionFailure(_circuitState, out var shouldBreak);
// Circuit state handling
// HalfOpen - open the circuit again
// Closed - break the circuit if the behavior indicates it
// Open, Isolated - a failure call result may arrive when the circuit is open,
// if it was placed before the circuit broke. We take no action beyond tracking
// the metric; we do not want to duplicate-signal onBreak; we do not want to extend time for which the circuit is broken.
// We do not want to mask the fact that the call executed (as replacing its result with a Broken/IsolatedCircuitException would do).
if (_circuitState == CircuitState.HalfOpen || (_circuitState == CircuitState.Closed && shouldBreak))
{
OpenCircuit_NeedsLock(outcome, manual: false, context, out task);
}
}
return ExecuteScheduledTaskAsync(task, context);
}
public void Dispose()
{
_executor.Dispose();
_disposed = true;
}
internal static async ValueTask ExecuteScheduledTaskAsync(Task? task, ResilienceContext context)
{
if (task is not null)
{
if (context.IsSynchronous)
{
#pragma warning disable CA1849 // Call async methods when in an async method
// because this is synchronous execution we need to block
task.GetAwaiter().GetResult();
#pragma warning restore CA1849 // Call async methods when in an async method
}
else
{
await task.ConfigureAwait(context.ContinueOnCapturedContext);
}
}
}
private static bool IsDateTimeOverflow(DateTimeOffset utcNow, TimeSpan breakDuration)
{
TimeSpan maxDifference = DateTimeOffset.MaxValue - utcNow;
// stryker disable once equality : no means to test this
return breakDuration > maxDifference;
}
#if NET8_0_OR_GREATER
private void EnsureNotDisposed()
=> ObjectDisposedException.ThrowIf(_disposed, this);
#else
private void EnsureNotDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(CircuitStateController<T>));
}
}
#endif
private void CloseCircuit_NeedsLock(Outcome<T> outcome, bool manual, ResilienceContext context, out Task? scheduledTask)
{
scheduledTask = null;
_blockedUntil = DateTimeOffset.MinValue;
_lastOutcome = null;
_halfOpenAttempts = 0;
CircuitState priorState = _circuitState;
_circuitState = CircuitState.Closed;
_behavior.OnCircuitClosed();
if (priorState != CircuitState.Closed)
{
var args = new OnCircuitClosedArguments<T>(context, outcome, manual);
_telemetry.Report<OnCircuitClosedArguments<T>, T>(new(ResilienceEventSeverity.Information, CircuitBreakerConstants.OnCircuitClosed), args);
if (_onClosed is not null)
{
_executor.ScheduleTask(() => _onClosed(args).AsTask(), context, out scheduledTask);
}
}
}
private bool PermitHalfOpenCircuitTest_NeedsLock()
{
var now = _timeProvider.GetUtcNow();
if (now >= _blockedUntil)
{
_blockedUntil = now + _breakDuration;
return true;
}
return false;
}
private void SetLastHandledOutcome_NeedsLock(Outcome<T> outcome)
{
_lastOutcome = outcome;
_breakingException = outcome.Exception;
}
private BrokenCircuitException CreateBrokenCircuitException()
{
TimeSpan retryAfter = _blockedUntil - _timeProvider.GetUtcNow();
var exception = _breakingException switch
{
Exception ex => new BrokenCircuitException(BrokenCircuitException.DefaultMessage, retryAfter, ex),
_ => new BrokenCircuitException(BrokenCircuitException.DefaultMessage, retryAfter)
};
_telemetry.SetTelemetrySource(exception);
return exception;
}
private void OpenCircuit_NeedsLock(Outcome<T> outcome, bool manual, ResilienceContext context, out Task? scheduledTask)
=> OpenCircuitFor_NeedsLock(outcome, _breakDuration, manual, context, out scheduledTask);
private void OpenCircuitFor_NeedsLock(Outcome<T> outcome, TimeSpan breakDuration, bool manual, ResilienceContext context, out Task? scheduledTask)
{
scheduledTask = null;
var utcNow = _timeProvider.GetUtcNow();
if (_breakDurationGenerator is not null)
{
#pragma warning disable CA2012
#pragma warning disable S1226
breakDuration = _breakDurationGenerator(new(_behavior.FailureRate, _behavior.FailureCount, context, _halfOpenAttempts)).GetAwaiter().GetResult();
#pragma warning restore S1226
#pragma warning restore CA2012
}
_blockedUntil = IsDateTimeOverflow(utcNow, breakDuration) ? DateTimeOffset.MaxValue : utcNow + breakDuration;
_circuitState = CircuitState.Open;
var args = new OnCircuitOpenedArguments<T>(context, outcome, breakDuration, manual);
_telemetry.Report<OnCircuitOpenedArguments<T>, T>(new(ResilienceEventSeverity.Error, CircuitBreakerConstants.OnCircuitOpened), args);
if (_onOpened is not null)
{
_executor.ScheduleTask(() => _onOpened(args).AsTask(), context, out scheduledTask);
}
}
private Task ScheduleHalfOpenTask(ResilienceContext context)
{
_executor.ScheduleTask(() => _onHalfOpen!(new OnCircuitHalfOpenedArguments(context)).AsTask(), context, out var task);
return task;
}
}