-
Notifications
You must be signed in to change notification settings - Fork 0
/
snippets.txt
112 lines (79 loc) · 2.82 KB
/
snippets.txt
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
// Failure injection
if (Random.Shared.NextDouble() < 0.2)
{
await Task.Delay(5000, cancellationToken);
}
if (Random.Shared.NextDouble() < 0.3)
{
throw new InvalidOperationException("Something went wrong.");
}
// appsettings.json
{
"weather-resilience": {
"Hedging":{
"Delay": "00:00:02"
}
}
}
// Build config
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
// Custom Resilience Pipeline
builder.AddTimeout(TimeSpan.FromSeconds(10));
builder.AddConcurrencyLimiter(100);
builder.AddRetry(new HttpRetryStrategyOptions
{
MaxRetryAttempts = 5,
Delay = TimeSpan.Zero,
});
builder.AddCircuitBreaker(new HttpCircuitBreakerStrategyOptions
{
SamplingDuration = TimeSpan.FromSeconds(5),
MinimumThroughput = 5,
FailureRatio = 0.9,
BreakDuration = TimeSpan.FromSeconds(5)
});
builder.AddTimeout(TimeSpan.FromSeconds(1));
// Standard Resilience Pipeline
options.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(10);
options.Retry.MaxRetryAttempts = 5;
options.Retry.Delay = TimeSpan.Zero;
options.CircuitBreaker.SamplingDuration = TimeSpan.FromSeconds(5);
options.CircuitBreaker.MinimumThroughput = 5;
options.CircuitBreaker.FailureRatio = 0.9;
options.CircuitBreaker.BreakDuration = TimeSpan.FromSeconds(5);
options.AttemptTimeout.Timeout = TimeSpan.FromSeconds(1);
// Standard Hedging Pipeline
options.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(10);
options.Hedging.MaxHedgedAttempts = 5;
options.Hedging.Delay = TimeSpan.FromMilliseconds(50);
options.Endpoint.CircuitBreaker.SamplingDuration = TimeSpan.FromSeconds(5);
options.Endpoint.CircuitBreaker.MinimumThroughput = 5;
options.Endpoint.CircuitBreaker.FailureRatio = 0.9;
options.Endpoint.CircuitBreaker.BreakDuration = TimeSpan.FromSeconds(5);
options.Endpoint.Timeout.Timeout = TimeSpan.FromSeconds(1);
// Dynamic Reloads
{
"Logging": {
"LogLevel": {
"Default": "None"
}
},
"weather-pipeline": {
"Hedging": {
"Delay": "00:00:02"
}
}
}
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Console.WriteLine("Pipeline reloaded!");
// Fallback
httpClientBuilder.AddResilienceHandler("fallback", builder =>
{
builder.AddFallback(new()
{
ShouldHandle = new PredicateBuilder<HttpResponseMessage>().Handle<BrokenCircuitException>(),
FallbackAction = _ => Outcome.FromResultAsValueTask(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable))
});
});