-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
BulkheadTResultSyntax.cs
83 lines (76 loc) · 6.49 KB
/
BulkheadTResultSyntax.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
#nullable enable
namespace Polly;
public partial class Policy
{
/// <summary>
/// <para>Builds a bulkhead isolation <see cref="Policy{TResult}"/>, which limits the maximum concurrency of actions executed through the policy. Imposing a maximum concurrency limits the potential of governed actions, when faulting, to bring down the system.</para>
/// <para>When an execution would cause the number of actions executing concurrently through the policy to exceed <paramref name="maxParallelization"/>, the action is not executed and a <see cref="BulkheadRejectedException"/> is thrown.</para>
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="maxParallelization">The maximum number of concurrent actions that may be executing through the policy.</param>
/// <exception cref="ArgumentOutOfRangeException">maxParallelization;Value must be greater than zero.</exception>
/// <returns>The policy instance.</returns>
public static BulkheadPolicy<TResult> Bulkhead<TResult>(int maxParallelization)
{
Action<Context> doNothing = _ => { };
return Bulkhead<TResult>(maxParallelization, 0, doNothing);
}
/// <summary>
/// <para>Builds a bulkhead isolation <see cref="Policy{TResult}"/>, which limits the maximum concurrency of actions executed through the policy. Imposing a maximum concurrency limits the potential of governed actions, when faulting, to bring down the system.</para>
/// <para>When an execution would cause the number of actions executing concurrently through the policy to exceed <paramref name="maxParallelization"/>, the action is not executed and a <see cref="BulkheadRejectedException"/> is thrown.</para>
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="maxParallelization">The maximum number of concurrent actions that may be executing through the policy.</param>
/// <param name="onBulkheadRejected">An action to call, if the bulkhead rejects execution due to oversubscription.</param>
/// <exception cref="ArgumentOutOfRangeException">maxParallelization;Value must be greater than zero.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="onBulkheadRejected"/> is <see langword="null"/>.</exception>
/// <returns>The policy instance.</returns>
public static BulkheadPolicy<TResult> Bulkhead<TResult>(int maxParallelization, Action<Context> onBulkheadRejected) =>
Bulkhead<TResult>(maxParallelization, 0, onBulkheadRejected);
/// <summary>
/// Builds a bulkhead isolation <see cref="Policy{TResult}" />, which limits the maximum concurrency of actions executed through the policy. Imposing a maximum concurrency limits the potential of governed actions, when faulting, to bring down the system.
/// <para>When an execution would cause the number of actions executing concurrently through the policy to exceed <paramref name="maxParallelization" />, the policy allows a further <paramref name="maxQueuingActions" /> executions to queue, waiting for a concurrent execution slot. When an execution would cause the number of queuing actions to exceed <paramref name="maxQueuingActions" />, a <see cref="BulkheadRejectedException" /> is thrown.</para>
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="maxParallelization">The maximum number of concurrent actions that may be executing through the policy.</param>
/// <param name="maxQueuingActions">The maximum number of actions that may be queuing, waiting for an execution slot.</param>
/// <returns>The policy instance.</returns>
/// <exception cref="ArgumentOutOfRangeException">maxParallelization;Value must be greater than zero.</exception>
/// <exception cref="ArgumentOutOfRangeException">maxQueuingActions;Value must be greater than or equal to zero.</exception>
public static BulkheadPolicy<TResult> Bulkhead<TResult>(int maxParallelization, int maxQueuingActions)
{
Action<Context> doNothing = _ => { };
return Bulkhead<TResult>(maxParallelization, maxQueuingActions, doNothing);
}
/// <summary>
/// Builds a bulkhead isolation <see cref="Policy{TResult}" />, which limits the maximum concurrency of actions executed through the policy. Imposing a maximum concurrency limits the potential of governed actions, when faulting, to bring down the system.
/// <para>When an execution would cause the number of actions executing concurrently through the policy to exceed <paramref name="maxParallelization" />, the policy allows a further <paramref name="maxQueuingActions" /> executions to queue, waiting for a concurrent execution slot. When an execution would cause the number of queuing actions to exceed <paramref name="maxQueuingActions" />, a <see cref="BulkheadRejectedException" /> is thrown.</para>
/// </summary>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <param name="maxParallelization">The maximum number of concurrent actions that may be executing through the policy.</param>
/// <param name="maxQueuingActions">The maximum number of actions that may be queuing, waiting for an execution slot.</param>
/// <param name="onBulkheadRejected">An action to call, if the bulkhead rejects execution due to oversubscription.</param>
/// <returns>The policy instance.</returns>
/// <exception cref="ArgumentOutOfRangeException">maxParallelization;Value must be greater than zero.</exception>
/// <exception cref="ArgumentOutOfRangeException">maxQueuingActions;Value must be greater than or equal to zero.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="onBulkheadRejected"/> is <see langword="null"/>.</exception>
public static BulkheadPolicy<TResult> Bulkhead<TResult>(int maxParallelization, int maxQueuingActions, Action<Context> onBulkheadRejected)
{
if (maxParallelization <= 0)
{
throw new ArgumentOutOfRangeException(nameof(maxParallelization), "Value must be greater than zero.");
}
if (maxQueuingActions < 0)
{
throw new ArgumentOutOfRangeException(nameof(maxQueuingActions), "Value must be greater than or equal to zero.");
}
if (onBulkheadRejected == null)
{
throw new ArgumentNullException(nameof(onBulkheadRejected));
}
return new BulkheadPolicy<TResult>(
maxParallelization,
maxQueuingActions,
onBulkheadRejected);
}
}