-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNewPolicyCommand.cs
138 lines (123 loc) · 5.16 KB
/
NewPolicyCommand.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
using System;
using System.Management.Automation;
using Microsoft.Extensions.Caching.Memory;
using Polly;
using Polly.Caching;
using Polly.Caching.Memory;
namespace pspolly
{
[Cmdlet("New", "PollyPolicy")]
public class NewPolicyCommand : PSCmdlet
{
[Parameter(ParameterSetName = "Retry", Mandatory = true)]
public SwitchParameter Retry { get; set; }
[Parameter(ParameterSetName = "Retry")]
public int RetryCount { get; set; } = 1;
[Parameter(ParameterSetName = "Retry")]
public TimeSpan[] RetryWait { get; set; }
[Parameter(ParameterSetName = "RetryForever", Mandatory = true)]
public SwitchParameter RetryForever { get; set; }
[Parameter(ParameterSetName = "RetryForever")]
[Parameter(ParameterSetName = "Retry")]
public ScriptBlock SleepDuration { get; set; }
[Parameter(ParameterSetName = "CircuitBreaker", Mandatory = true)]
public SwitchParameter CircuitBreaker { get; set; }
[Parameter(ParameterSetName = "CircuitBreaker")]
public int ExceptionsAllowedBeforeBreaking { get; set; } = 1;
[Parameter(ParameterSetName = "CircuitBreaker")]
public ScriptBlock OnBreak { get; set; }
[Parameter(ParameterSetName = "CircuitBreaker")]
public TimeSpan DurationOfBreak { get; set; }
[Parameter(ParameterSetName = "Timeout")]
public TimeSpan Timeout { get; set; }
[Parameter(ParameterSetName = "Timeout")]
public ScriptBlock OnTimeout { get; set; }
[Parameter(ParameterSetName = "Cache")]
public DateTimeOffset AbsoluteExpiration { get; set; }
[Parameter(ParameterSetName = "Cache")]
public TimeSpan SlidingExpiration { get; set; }
[Parameter(ParameterSetName = "Cache")]
public ScriptBlock OnCacheError { get; set; }
[Parameter(ParameterSetName = "RateLimit", Mandatory = true)]
public int Executions { get; set; }
[Parameter(ParameterSetName = "RateLimit", Mandatory = true)]
public TimeSpan PerTimeSpan { get; set; }
private static MemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());
private static MemoryCacheProvider _memoryCacheProvider = new MemoryCacheProvider(_memoryCache);
protected override void ProcessRecord()
{
var policyBuilder = Policy.Handle<Exception>();
Policy policy = null;
if (ParameterSetName == "Retry")
{
if (SleepDuration != null)
{
policy = policyBuilder.WaitAndRetry(RetryCount, (retryCount) =>
{
var psObject = SleepDuration.Invoke(retryCount);
return (TimeSpan)psObject[0].BaseObject;
});
}
else if (RetryWait != null)
{
policy = policyBuilder.WaitAndRetry(RetryWait);
}
else
{
policy = policyBuilder.Retry(RetryCount);
}
}
if (ParameterSetName == "RetryForever")
{
if (SleepDuration == null)
{
policy = policyBuilder.RetryForever();
}
else
{
policy = policyBuilder.WaitAndRetryForever((retryCount) =>
{
var psObject = SleepDuration.Invoke(retryCount);
return (TimeSpan)psObject[0].BaseObject;
});
}
}
if (ParameterSetName == "CircuitBreaker")
{
policy = policyBuilder.CircuitBreaker(ExceptionsAllowedBeforeBreaking, DurationOfBreak, (ex, ts) =>
{
OnBreak?.Invoke(ex, ts);
}, () => { });
}
if (ParameterSetName == "Timeout")
{
policy = Policy.Timeout(Timeout, onTimeout: (context, timespan, task) =>
{
OnTimeout?.Invoke(context, timespan, task);
});
}
if (ParameterSetName == "Cache")
{
if (MyInvocation.BoundParameters.ContainsKey("AbsoluteExpiration"))
{
policy = Policy.Cache(_memoryCacheProvider, new AbsoluteTtl(AbsoluteExpiration), (context, key, ex) =>
{
OnCacheError?.Invoke(context, key, ex);
});
}
if (MyInvocation.BoundParameters.ContainsKey("SlidingExpiration"))
{
policy = Policy.Cache(_memoryCacheProvider, new SlidingTtl(SlidingExpiration), (context, key, ex) =>
{
OnCacheError?.Invoke(context, key, ex);
});
}
}
if (ParameterSetName == "RateLimit")
{
policy = Policy.RateLimit(Executions, PerTimeSpan);
}
WriteObject(policy);
}
}
}