-
Notifications
You must be signed in to change notification settings - Fork 128
/
Demo03_WaitAndRetryNTimes_WithEnoughRetries.cs
92 lines (80 loc) · 3.34 KB
/
Demo03_WaitAndRetryNTimes_WithEnoughRetries.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
using PollyDemos.Helpers;
using PollyDemos.OutputHelpers;
namespace PollyDemos;
/// <summary>
/// <para>
/// Demonstrates the Retry strategy with delays between retry attempts. <br/>
/// Loops through a series of HTTP requests, keeping track of each requested <br/>
/// item and reporting server failures when encountering exceptions.
/// </para>
/// <para>
/// Observations:
/// <list type="bullet">
/// <item>We now have waits and enough retries: all calls now succeed! Yay!</item>
/// <item>But we kinda had to guess how many retries would be enough before the server responded again...<br/>
/// (and we're hammering that server with retries)</item>
/// </list>
/// </para>
/// <para>
/// How to read the demo logs:
/// <list type="bullet">
/// <item>"Response: ... request #N(...)": Response received on time.</item>
/// </list>
/// </para>
/// </summary>
public class Demo03_WaitAndRetryNTimes_WithEnoughRetries : DemoBase
{
public override string Description =>
"Compared to previous demo, this adds enough waiting and retrying to always ensure success.";
public override async Task ExecuteAsync(CancellationToken cancellationToken, IProgress<DemoProgress> progress)
{
ArgumentNullException.ThrowIfNull(progress);
EventualSuccesses = 0;
Retries = 0;
EventualFailures = 0;
TotalRequests = 0;
PrintHeader(progress);
var strategy = new ResiliencePipelineBuilder().AddRetry(new()
{
ShouldHandle = new PredicateBuilder().Handle<Exception>(),
MaxRetryAttempts = 20, // Retry up to 20 times - this should be enough that we eventually succeed.
Delay = TimeSpan.FromMilliseconds(200),
OnRetry = args =>
{
var exception = args.Outcome.Exception!;
progress.Report(ProgressWithMessage($"Strategy logging: {exception.Message}", Color.Yellow));
Retries++;
return default;
}
}).Build();
var client = new HttpClient();
var internalCancel = false;
while (!(internalCancel || cancellationToken.IsCancellationRequested))
{
TotalRequests++;
try
{
await strategy.ExecuteAsync(async token =>
{
var responseBody = await IssueRequestAndProcessResponseAsync(client, token);
progress.Report(ProgressWithMessage($"Response : {responseBody}", Color.Green));
EventualSuccesses++;
}, cancellationToken);
}
catch (Exception e)
{
progress.Report(ProgressWithMessage($"Request {TotalRequests} eventually failed with: {e.Message}", Color.Red));
EventualFailures++;
}
await Task.Delay(TimeSpan.FromSeconds(0.5), cancellationToken);
internalCancel = ShouldTerminateByKeyPress();
}
}
public override Statistic[] LatestStatistics => new Statistic[]
{
new("Total requests made", TotalRequests),
new("Requests which eventually succeeded", EventualSuccesses, Color.Green),
new("Retries made to help achieve success", Retries, Color.Yellow),
new("Requests which eventually failed", EventualFailures, Color.Red),
};
}