-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathProgram.cs
78 lines (70 loc) · 2.87 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
#pragma warning disable SA1633 // File should have header
using System.Net;
#pragma warning restore SA1633 // File should have header
using Polly;
using Polly.Fallback;
using Polly.Retry;
using Polly.Timeout;
// ----------------------------------------------------------------------------
// Create a generic resilience pipeline using ResiliencePipelineBuilder<T>
// ----------------------------------------------------------------------------
// The generic ResiliencePipelineBuilder<T> creates a ResiliencePipeline<T>
// that can execute synchronous and asynchronous callbacks that return T.
ResiliencePipeline<HttpResponseMessage> pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddFallback(new FallbackStrategyOptions<HttpResponseMessage>
{
FallbackAction = _ =>
{
// Return fallback result
return Outcome.FromResultAsValueTask(new HttpResponseMessage(HttpStatusCode.OK));
},
// You can also use switch expressions for succinct syntax
ShouldHandle = arguments => arguments.Outcome switch
{
// The "PredicateResult.True" is shorthand to "new ValueTask<bool>(true)"
{ Exception: HttpRequestException } => PredicateResult.True(),
{ Result: HttpResponseMessage response } when response.StatusCode == HttpStatusCode.InternalServerError => PredicateResult.True(),
_ => PredicateResult.False(),
},
OnFallback = _ =>
{
Console.WriteLine("Fallback!");
return default;
},
})
.AddRetry(new RetryStrategyOptions<HttpResponseMessage>
{
// You can use "PredicateBuilder" to configure the predicates
ShouldHandle = new PredicateBuilder<HttpResponseMessage>()
.HandleResult(r => r.StatusCode == HttpStatusCode.InternalServerError)
.Handle<HttpRequestException>(),
// Register user callback called whenever retry occurs
OnRetry = arguments =>
{
Console.WriteLine($"Retrying '{arguments.Outcome.Result?.StatusCode}'...");
return default;
},
Delay = TimeSpan.FromMilliseconds(400),
BackoffType = DelayBackoffType.Constant,
MaxRetryAttempts = 3,
})
.AddTimeout(new TimeoutStrategyOptions
{
Timeout = TimeSpan.FromSeconds(1),
// Register user callback called whenever timeout occurs
OnTimeout = _ =>
{
Console.WriteLine("Timeout occurred!");
return default;
},
})
.Build();
var response = await pipeline.ExecuteAsync(
async token =>
{
await Task.Delay(10, token);
// This causes the action fail, thus using the fallback strategy above
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
},
CancellationToken.None);
Console.WriteLine($"Response: {response.StatusCode}");