-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Program.cs
93 lines (77 loc) · 2.94 KB
/
Program.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
using Polly;
using Polly.Retry;
using Polly.Timeout;
// ------------------------------------------------------------------------
// 1. Create a simple resilience pipeline using ResiliencePipelineBuilder
// ------------------------------------------------------------------------
// The ResiliencePipelineBuilder creates a ResiliencePipeline
// that can be executed synchronously or asynchronously
// and for both void and result-returning user-callbacks.
ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
// Use convenience extension that accepts TimeSpan
.AddTimeout(TimeSpan.FromSeconds(5))
.Build();
// ------------------------------------------------------------------------
// 2. Execute the pipeline
// ------------------------------------------------------------------------
// Synchronously
pipeline.Execute(() => { });
// Asynchronously
await pipeline.ExecuteAsync(async token => await Task.Delay(10, token), CancellationToken.None);
// Synchronously with result
pipeline.Execute(token => "some-result");
// Asynchronously with result
await pipeline.ExecuteAsync(
async token =>
{
await Task.Delay(10, token);
return "some-result";
},
CancellationToken.None);
// Use state to avoid lambda allocation
pipeline.Execute(static state => state, "my-state");
// ------------------------------------------------------------------------
// 3. Create and execute a pipeline of strategies
// ------------------------------------------------------------------------
pipeline = new ResiliencePipelineBuilder()
// Add retries using the options
.AddRetry(new RetryStrategyOptions
{
// To configure the predicate you can use switch expressions
ShouldHandle = args => args.Outcome.Exception switch
{
TimeoutRejectedException => PredicateResult.True(),
// The "PredicateResult.False" is just shorthand for "new ValueTask<bool>(true)"
// You can also use "new PredicateBuilder().Handle<TimeoutRejectedException>()"
_ => PredicateResult.False(),
},
// Register user callback called whenever retry occurs
OnRetry = args =>
{
Console.WriteLine($"Retrying...{args.AttemptNumber} attempt");
return default;
},
Delay = TimeSpan.FromMilliseconds(400),
BackoffType = DelayBackoffType.Constant,
MaxRetryAttempts = 3,
})
// Add timeout using the options
.AddTimeout(new TimeoutStrategyOptions
{
Timeout = TimeSpan.FromSeconds(1),
// Register user callback called whenever timeout occurs
OnTimeout = args =>
{
Console.WriteLine($"Timeout occurred after {args.Timeout}!");
return default;
},
})
.Build();
try
{
await pipeline.ExecuteAsync(async token => await Task.Delay(TimeSpan.FromSeconds(2), token), CancellationToken.None);
}
catch (TimeoutRejectedException)
{
// ok, expected
}