/
CircuitBreakerSyntax.cs
218 lines (209 loc) · 15.5 KB
/
CircuitBreakerSyntax.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
using System;
using Polly.CircuitBreaker;
using Polly.Utilities;
namespace Polly
{
/// <summary>
/// Fluent API for defining a Circuit Breaker <see cref="Policy"/>.
/// </summary>
public static class CircuitBreakerSyntax
{
/// <summary>
/// <para> Builds a <see cref="Policy"/> that will function like a Circuit Breaker.</para>
/// <para>The circuit will break if <paramref name="exceptionsAllowedBeforeBreaking"/>
/// exceptions that are handled by this policy are raised consecutively. </para>
/// <para>The circuit will stay broken for the <paramref name="durationOfBreak"/>. Any attempt to execute this policy
/// while the circuit is broken, will immediately throw a <see cref="BrokenCircuitException"/> containing the exception
/// that broke the circuit.
/// </para>
/// <para>If the first action after the break duration period results in a handled exception, the circuit will break
/// again for another <paramref name="durationOfBreak"/>; if no exception is thrown, the circuit will reset.
/// </para>
/// </summary>
/// <param name="policyBuilder">The policy builder.</param>
/// <param name="exceptionsAllowedBeforeBreaking">The number of exceptions that are allowed before opening the circuit.</param>
/// <param name="durationOfBreak">The duration the circuit will stay open before resetting.</param>
/// <returns>The policy instance.</returns>
/// <remarks>(see "Release It!" by Michael T. Nygard fi)</remarks>
/// <exception cref="ArgumentOutOfRangeException">exceptionsAllowedBeforeBreaking;Value must be greater than zero.</exception>
/// <exception cref="ArgumentNullException">onBreak</exception>
/// <exception cref="ArgumentNullException">onReset</exception>
public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak)
{
Action<Exception, TimeSpan> doNothingOnBreak = (_, __) => { };
Action doNothingOnReset = () => { };
return policyBuilder.CircuitBreaker
(exceptionsAllowedBeforeBreaking,
durationOfBreak,
doNothingOnBreak,
doNothingOnReset
);
}
/// <summary>
/// <para> Builds a <see cref="Policy"/> that will function like a Circuit Breaker.</para>
/// <para>The circuit will break if <paramref name="exceptionsAllowedBeforeBreaking"/>
/// exceptions that are handled by this policy are raised consecutively. </para>
/// <para>The circuit will stay broken for the <paramref name="durationOfBreak"/>. Any attempt to execute this policy
/// while the circuit is broken, will immediately throw a <see cref="BrokenCircuitException"/> containing the exception
/// that broke the circuit.
/// </para>
/// <para>If the first action after the break duration period results in a handled exception, the circuit will break
/// again for another <paramref name="durationOfBreak"/>; if no exception is thrown, the circuit will reset.
/// </para>
/// </summary>
/// <param name="policyBuilder">The policy builder.</param>
/// <param name="exceptionsAllowedBeforeBreaking">The number of exceptions that are allowed before opening the circuit.</param>
/// <param name="durationOfBreak">The duration the circuit will stay open before resetting.</param>
/// <param name="onBreak">The action to call when the circuit transitions to an <see cref="CircuitState.Open"/> state.</param>
/// <param name="onReset">The action to call when the circuit resets to a <see cref="CircuitState.Closed"/> state.</param>
/// <returns>The policy instance.</returns>
/// <remarks>(see "Release It!" by Michael T. Nygard fi)</remarks>
/// <exception cref="ArgumentOutOfRangeException">exceptionsAllowedBeforeBreaking;Value must be greater than zero.</exception>
/// <exception cref="ArgumentNullException">onBreak</exception>
/// <exception cref="ArgumentNullException">onReset</exception>
public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan> onBreak, Action onReset)
=> policyBuilder.CircuitBreaker(
exceptionsAllowedBeforeBreaking,
durationOfBreak,
(exception, timespan, context) => onBreak(exception, timespan),
context => onReset()
);
/// <summary>
/// <para> Builds a <see cref="Policy"/> that will function like a Circuit Breaker.</para>
/// <para>The circuit will break if <paramref name="exceptionsAllowedBeforeBreaking"/>
/// exceptions that are handled by this policy are raised consecutively. </para>
/// <para>The circuit will stay broken for the <paramref name="durationOfBreak"/>. Any attempt to execute this policy
/// while the circuit is broken, will immediately throw a <see cref="BrokenCircuitException"/> containing the exception
/// that broke the circuit.
/// </para>
/// <para>If the first action after the break duration period results in a handled exception, the circuit will break
/// again for another <paramref name="durationOfBreak"/>; if no exception is thrown, the circuit will reset.
/// </para>
/// </summary>
/// <param name="policyBuilder">The policy builder.</param>
/// <param name="exceptionsAllowedBeforeBreaking">The number of exceptions that are allowed before opening the circuit.</param>
/// <param name="durationOfBreak">The duration the circuit will stay open before resetting.</param>
/// <param name="onBreak">The action to call when the circuit transitions to an <see cref="CircuitState.Open"/> state.</param>
/// <param name="onReset">The action to call when the circuit resets to a <see cref="CircuitState.Closed"/> state.</param>
/// <returns>The policy instance.</returns>
/// <remarks>(see "Release It!" by Michael T. Nygard fi)</remarks>
/// <exception cref="ArgumentOutOfRangeException">exceptionsAllowedBeforeBreaking;Value must be greater than zero.</exception>
/// <exception cref="ArgumentNullException">onBreak</exception>
/// <exception cref="ArgumentNullException">onReset</exception>
public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan, Context> onBreak, Action<Context> onReset)
{
Action doNothingOnHalfOpen = () => { };
return policyBuilder.CircuitBreaker(exceptionsAllowedBeforeBreaking,
durationOfBreak,
onBreak,
onReset,
doNothingOnHalfOpen
);
}
/// <summary>
/// <para> Builds a <see cref="Policy"/> that will function like a Circuit Breaker.</para>
/// <para>The circuit will break if <paramref name="exceptionsAllowedBeforeBreaking"/>
/// exceptions that are handled by this policy are raised consecutively. </para>
/// <para>The circuit will stay broken for the <paramref name="durationOfBreak"/>. Any attempt to execute this policy
/// while the circuit is broken, will immediately throw a <see cref="BrokenCircuitException"/> containing the exception
/// that broke the circuit.
/// </para>
/// <para>If the first action after the break duration period results in a handled exception, the circuit will break
/// again for another <paramref name="durationOfBreak"/>; if no exception is thrown, the circuit will reset.
/// </para>
/// </summary>
/// <param name="policyBuilder">The policy builder.</param>
/// <param name="exceptionsAllowedBeforeBreaking">The number of exceptions that are allowed before opening the circuit.</param>
/// <param name="durationOfBreak">The duration the circuit will stay open before resetting.</param>
/// <param name="onBreak">The action to call when the circuit transitions to an <see cref="CircuitState.Open"/> state.</param>
/// <param name="onReset">The action to call when the circuit resets to a <see cref="CircuitState.Closed"/> state.</param>
/// <param name="onHalfOpen">The action to call when the circuit transitions to <see cref="CircuitState.HalfOpen"/> state, ready to try action executions again. </param>
/// <returns>The policy instance.</returns>
/// <remarks>(see "Release It!" by Michael T. Nygard fi)</remarks>
/// <exception cref="ArgumentOutOfRangeException">exceptionsAllowedBeforeBreaking;Value must be greater than zero.</exception>
/// <exception cref="ArgumentNullException">onBreak</exception>
/// <exception cref="ArgumentNullException">onReset</exception>
public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan> onBreak, Action onReset, Action onHalfOpen)
=> policyBuilder.CircuitBreaker(
exceptionsAllowedBeforeBreaking,
durationOfBreak,
(exception, timespan, context) => onBreak(exception, timespan),
context => onReset(),
onHalfOpen
);
/// <summary>
/// <para> Builds a <see cref="Policy"/> that will function like a Circuit Breaker.</para>
/// <para>The circuit will break if <paramref name="exceptionsAllowedBeforeBreaking"/>
/// exceptions that are handled by this policy are raised consecutively. </para>
/// <para>The circuit will stay broken for the <paramref name="durationOfBreak"/>. Any attempt to execute this policy
/// while the circuit is broken, will immediately throw a <see cref="BrokenCircuitException"/> containing the exception
/// that broke the circuit.
/// </para>
/// <para>If the first action after the break duration period results in a handled exception, the circuit will break
/// again for another <paramref name="durationOfBreak"/>; if no exception is thrown, the circuit will reset.
/// </para>
/// </summary>
/// <param name="policyBuilder">The policy builder.</param>
/// <param name="exceptionsAllowedBeforeBreaking">The number of exceptions that are allowed before opening the circuit.</param>
/// <param name="durationOfBreak">The duration the circuit will stay open before resetting.</param>
/// <param name="onBreak">The action to call when the circuit transitions to an <see cref="CircuitState.Open"/> state.</param>
/// <param name="onReset">The action to call when the circuit resets to a <see cref="CircuitState.Closed"/> state.</param>
/// <param name="onHalfOpen">The action to call when the circuit transitions to <see cref="CircuitState.HalfOpen"/> state, ready to try action executions again. </param>
/// <returns>The policy instance.</returns>
/// <remarks>(see "Release It!" by Michael T. Nygard fi)</remarks>
/// <exception cref="ArgumentOutOfRangeException">exceptionsAllowedBeforeBreaking;Value must be greater than zero.</exception>
/// <exception cref="ArgumentNullException">onBreak</exception>
/// <exception cref="ArgumentNullException">onReset</exception>
/// <exception cref="ArgumentNullException">onHalfOpen</exception>
public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
=> policyBuilder.CircuitBreaker(
exceptionsAllowedBeforeBreaking,
durationOfBreak,
(exception, state, timespan, context) => onBreak(exception, timespan, context),
onReset,
onHalfOpen
);
/// <summary>
/// <para> Builds a <see cref="Policy"/> that will function like a Circuit Breaker.</para>
/// <para>The circuit will break if <paramref name="exceptionsAllowedBeforeBreaking"/>
/// exceptions that are handled by this policy are raised consecutively. </para>
/// <para>The circuit will stay broken for the <paramref name="durationOfBreak"/>. Any attempt to execute this policy
/// while the circuit is broken, will immediately throw a <see cref="BrokenCircuitException"/> containing the exception
/// that broke the circuit.
/// </para>
/// <para>If the first action after the break duration period results in a handled exception, the circuit will break
/// again for another <paramref name="durationOfBreak"/>; if no exception is thrown, the circuit will reset.
/// </para>
/// </summary>
/// <param name="policyBuilder">The policy builder.</param>
/// <param name="exceptionsAllowedBeforeBreaking">The number of exceptions that are allowed before opening the circuit.</param>
/// <param name="durationOfBreak">The duration the circuit will stay open before resetting.</param>
/// <param name="onBreak">The action to call when the circuit transitions to an <see cref="CircuitState.Open"/> state.</param>
/// <param name="onReset">The action to call when the circuit resets to a <see cref="CircuitState.Closed"/> state.</param>
/// <param name="onHalfOpen">The action to call when the circuit transitions to <see cref="CircuitState.HalfOpen"/> state, ready to try action executions again. </param>
/// <returns>The policy instance.</returns>
/// <remarks>(see "Release It!" by Michael T. Nygard fi)</remarks>
/// <exception cref="ArgumentOutOfRangeException">exceptionsAllowedBeforeBreaking;Value must be greater than zero.</exception>
/// <exception cref="ArgumentNullException">onBreak</exception>
/// <exception cref="ArgumentNullException">onReset</exception>
/// <exception cref="ArgumentNullException">onHalfOpen</exception>
public static CircuitBreakerPolicy CircuitBreaker(this PolicyBuilder policyBuilder, int exceptionsAllowedBeforeBreaking, TimeSpan durationOfBreak, Action<Exception, CircuitState, TimeSpan, Context> onBreak, Action<Context> onReset, Action onHalfOpen)
{
if (exceptionsAllowedBeforeBreaking <= 0) throw new ArgumentOutOfRangeException(nameof(exceptionsAllowedBeforeBreaking), "Value must be greater than zero.");
if (durationOfBreak < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(durationOfBreak), "Value must be greater than zero.");
if (onBreak == null) throw new ArgumentNullException(nameof(onBreak));
if (onReset == null) throw new ArgumentNullException(nameof(onReset));
if (onHalfOpen == null) throw new ArgumentNullException(nameof(onHalfOpen));
var breakerController = new ConsecutiveCountCircuitController<EmptyStruct>(
exceptionsAllowedBeforeBreaking,
durationOfBreak,
(outcome, state, timespan, context) => onBreak(outcome.Exception, state, timespan, context),
onReset,
onHalfOpen);
return new CircuitBreakerPolicy(
policyBuilder,
breakerController
);
}
}
}