forked from App-vNext/Polly
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAdvancedCircuitBreakerConfiguration.cs
60 lines (51 loc) · 3.59 KB
/
AdvancedCircuitBreakerConfiguration.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
using Polly.Utilities;
using System;
namespace Polly.CircuitBreaker
{
/// <summary>
/// Describes the configuration parameters for a <see cref="CircuitBreakerPolicy"/>
/// </summary>
internal class AdvancedCircuitBreakerConfiguration : CircuitBreakerConfiguration, IAdvancedCircuitBreakerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="ConsecutiveCountCircuitBreakerConfiguration"/> class.
/// </summary>
/// <param name="failureThreshold">The failure threshold at which the circuit will break (a number between 0 and 1; eg 0.5 represents breaking if 50% or more of actions result in a handled failure.</param>
/// <param name="samplingDuration">The duration of the timeslice over which failure ratios are assessed.</param>
/// <param name="minimumThroughput">The minimum throughput: this many actions or more must pass through the circuit in the timeslice, for statistics to be considered significant and the circuit-breaker to come into action.</param>
/// <param name="durationOfBreak">The duration of break.</param>
/// <exception cref="ArgumentOutOfRangeException">failureThreshold;Value must be greater than zero</exception>
/// <exception cref="ArgumentOutOfRangeException">failureThreshold;Value must be less than or equal to one</exception>
/// <exception cref="ArgumentOutOfRangeException">samplingDuration;Value must be equal to or greater than the minimum resolution of the CircuitBreaker timer</exception>
/// <exception cref="ArgumentOutOfRangeException">minimumThroughput;Value must be greater than one</exception>
public AdvancedCircuitBreakerConfiguration(
double failureThreshold,
TimeSpan samplingDuration,
int minimumThroughput,
TimeSpan durationOfBreak)
: base(durationOfBreak)
{
var resolutionOfCircuit = TimeSpan.FromTicks(AdvancedCircuitController<EmptyStruct>.ResolutionOfCircuitTimer);
if (failureThreshold <= 0) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be greater than zero.");
if (failureThreshold > 1) throw new ArgumentOutOfRangeException(nameof(failureThreshold), "Value must be less than or equal to one.");
if (samplingDuration < resolutionOfCircuit) throw new ArgumentOutOfRangeException(nameof(samplingDuration),
$"Value must be equal to or greater than {resolutionOfCircuit.TotalMilliseconds} milliseconds. This is the minimum resolution of the CircuitBreaker timer.");
if (minimumThroughput <= 1) throw new ArgumentOutOfRangeException(nameof(minimumThroughput), "Value must be greater than one.");
FailureThreshold = failureThreshold;
SamplingDuration = samplingDuration;
MinimumThroughput = minimumThroughput;
}
/// <summary>
/// The failure threshold at which the circuit will break (a number between 0 and 1; eg 0.5 represents breaking if 50% or more of actions result in a handled failure.
/// </summary>
public double FailureThreshold { get; }
/// <summary>
/// The duration of the timeslice over which failure ratios are assessed.
/// </summary>
public TimeSpan SamplingDuration { get; }
/// <summary>
/// The minimum throughput: this many actions or more must pass through the circuit in the timeslice, for statistics to be considered significant and the circuit-breaker to come into action.
/// </summary>
public int MinimumThroughput { get; }
}
}