forked from App-vNext/Polly-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo00_NoStrategy.cs
71 lines (60 loc) · 2.54 KB
/
Demo00_NoStrategy.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
using PollyDemos.Helpers;
using PollyDemos.OutputHelpers;
namespace PollyDemos;
/// <summary>
/// <para>
/// Uses no strategy. <br/>
/// Demonstrates behavior of 'faulting server' we are testing against <br/>
/// Loops through a series of HTTP requests, keeping track of each requested <br/>
/// item and reporting server failures when encountering exceptions.
/// </para>
/// <para>
/// How to read the demo logs:
/// <list type="bullet">
/// <item>"Response: ... request #N(...)": Response received on time.</item>
/// <item>"Request N eventually failed ... (Too Many Requests)": Request is rejected due to rate limit exceeded.</item>
/// </list>
/// </para>
/// </summary>
public class Demo00_NoStrategy : DemoBase
{
public override string Description =>
"This demo demonstrates how our faulting server behaves, with no Polly strategy in use.";
public override async Task ExecuteAsync(CancellationToken cancellationToken, IProgress<DemoProgress> progress)
{
ArgumentNullException.ThrowIfNull(progress);
EventualSuccesses = 0;
EventualFailures = 0;
TotalRequests = 0;
PrintHeader(progress);
var client = new HttpClient();
var internalCancel = false;
// Do the following until a key is pressed
while (!(internalCancel || cancellationToken.IsCancellationRequested))
{
TotalRequests++;
try
{
// Make a request and get a response
var responseBody = await IssueRequestAndProcessResponseAsync(client, cancellationToken);
// Display the response message on the console
progress.Report(ProgressWithMessage($"Response : {responseBody}", Color.Green));
EventualSuccesses++;
}
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),
};
}