-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathProgram.cs
100 lines (86 loc) · 3.59 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
94
95
96
97
98
99
100
using PollyDemos;
using PollyDemos.Helpers;
using PollyDemos.OutputHelpers;
using PollyTestClientConsole;
using PollyTestClientConsole.Menu;
using Spectre.Console;
using Color = PollyDemos.OutputHelpers.Color;
Statistic[] statistics = [];
Progress<DemoProgress> progress = new();
progress.ProgressChanged += (_, args) =>
{
foreach (var message in args.Messages)
{
WriteLineInColor(message.Message, message.Color);
}
statistics = args.Statistics;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Walk through the demos in order, to discover features.
// See <summary> at top of each demo class, for explanation.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
List<ConsoleMenuItem> menu =
[
new("00 - No strategy",
InvokeDemo<Demo00_NoStrategy>),
new("01 - Retry N times",
InvokeDemo<Demo01_RetryNTimes>),
new("02 - Wait and retry N times",
InvokeDemo<Demo02_WaitAndRetryNTimes>),
new("03 - Wait and retry N times, N big enough to guarantee success",
InvokeDemo<Demo03_WaitAndRetryNTimes_WithEnoughRetries>),
new("04 - Wait and retry forever",
InvokeDemo<Demo04_WaitAndRetryForever>),
new("05 - Wait and retry with exponential back-off",
InvokeDemo<Demo05_WaitAndRetryWithExponentialBackoff>),
new("06 - Wait and retry nesting circuit breaker",
InvokeDemo<Demo06_WaitAndRetryNestingCircuitBreaker>),
new("07 - Wait and retry chaining with circuit breaker by using Pipeline",
InvokeDemo<Demo07_WaitAndRetryNestingCircuitBreakerUsingPipeline>),
new("08 - Fallback, Retry, and CircuitBreaker in a Pipeline",
InvokeDemo<Demo08_Pipeline_Fallback_WaitAndRetry_CircuitBreaker>),
new("09 - Fallback, Timeout, and Retry in a Pipeline",
InvokeDemo<Demo09_Pipeline_Fallback_Timeout_WaitAndRetry>),
new("10 - Without isolation: Faulting calls swamp resources, also prevent good calls",
InvokeDemo<Demo10_SharedConcurrencyLimiter>),
new("11 - With isolation: Faulting calls separated, do not swamp resources, good calls still succeed",
InvokeDemo<Demo11_MultipleConcurrencyLimiters>),
new("12 - Hedging in latency mode",
InvokeDemo<Demo12_LatencyHedging>),
new("13 - Hedging in fallback mode: retry only",
InvokeDemo<Demo13_FallbackHedging_RetryOnly>),
new("14 - Hedging in fallback mode: retry with fallback",
InvokeDemo<Demo14_FallbackHedging_RetryWithFallback>),
new("15 - Hedging in parallel mode",
InvokeDemo<Demo15_ParallelHedging>),
new ("16 - Entity Framework with Retry N times",
InvokeDemo<Demo16_EntityFramework_WithRetryNTimes>),
new("-=Exit=-", () => Environment.Exit(0))
];
ConsoleMenu.PrintSplashScreen();
ConsoleMenu.Run(menu);
void InvokeDemo<T>() where T : DemoBase, new()
{
using var cancellationSource = new CancellationTokenSource();
var cancellationToken = cancellationSource.Token;
new T().ExecuteAsync(cancellationToken, progress).Wait();
cancellationSource.Cancel();
PrintStatisticsThenClear();
}
void PrintStatisticsThenClear()
{
var longestDescription = statistics.Max(s => s.Description.Length);
Console.WriteLine();
Console.WriteLine(new string('=', longestDescription));
Console.WriteLine();
foreach (Statistic stat in statistics)
{
WriteLineInColor($"{stat.Description.PadRight(longestDescription)}: {stat.Value}", stat.Color);
}
statistics = [];
}
void WriteLineInColor(string message, Color color)
{
var consoleColor = Spectre.Console.Color.FromConsoleColor(color.ToConsoleColor());
AnsiConsole.MarkupLineInterpolated($"[{consoleColor}]{message}[/]");
}