-
Notifications
You must be signed in to change notification settings - Fork 6k
Resiliency in .NET content #37270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Resiliency in .NET content #37270
Changes from 12 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
42af17e
Initial bits
IEvangelist db2524b
Trying to explore the resilience libs
IEvangelist a8cd975
Updates to resiliency
IEvangelist 2d43e0a
More updates
IEvangelist af43e96
Updates to docs
IEvangelist b45429e
More fixes and updates
IEvangelist 35bcf66
Fix heading
IEvangelist c4ae1cb
More HTTP bits
IEvangelist 2971d2f
Apply suggestions from code review
IEvangelist deb606a
More tweaks
IEvangelist 0436637
Added image
IEvangelist 84fad50
Additions
IEvangelist c5385c6
More updates
IEvangelist 7da24db
Apply suggestions from code review
IEvangelist 0cda35c
Clean up and updates
IEvangelist a3b83b4
Add context
IEvangelist 6f870bd
Retitle a bit
IEvangelist b48519a
A quick fix
IEvangelist 6f5d134
Resilience in TOC
IEvangelist 1fb59e2
Updates to headings
IEvangelist 16f50c3
Fix TOC entry
IEvangelist 45f1814
Added a bit more detail
IEvangelist d254367
Update diagram
IEvangelist ac69fce
Updates to HTTP resiliency content
IEvangelist 90e16c0
Add more configuration bits
IEvangelist c5cef6a
From peer feedback
IEvangelist 0201cdc
Clean up
IEvangelist 62081e1
Apply suggestions from code review
IEvangelist 7d3f28a
Update image
IEvangelist 01a0a61
Apply suggestions from code review
IEvangelist af85439
Updates from peer review
IEvangelist a92ec18
Major updates to the resilience bits
IEvangelist 690a0bb
A bit closer
IEvangelist 3b3c1da
The id's should work
IEvangelist f4095d4
There we go
IEvangelist 47088c2
code bits for resilience
IEvangelist 6befcd0
Update source with markers
IEvangelist 6076f8c
Apply suggestions from code review
IEvangelist c079b2f
Apply suggestions from code review
IEvangelist 8027a21
Address all feedback
IEvangelist 5aff14f
More clean up, final touches
IEvangelist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| public record class Comment( | ||
| namespace Http.Resilience.Example; | ||
|
|
||
| public record class Comment( | ||
| int PostId, int Id, string Name, string Email, string Body); |
9 changes: 9 additions & 0 deletions
9
docs/core/resilience/snippets/http-resilience/ExampleClient.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
docs/core/resilience/snippets/http-resilience/Program.CustomHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System.Net; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Http.Resilience; | ||
| using Polly; | ||
|
|
||
| internal partial class Program | ||
| { | ||
| private static void WithCustomHandler(IHttpClientBuilder httpClientBuilder) | ||
| { | ||
| // <custom> | ||
| httpClientBuilder.AddResilienceHandler( | ||
| "CustomPipeline", | ||
| static builder => | ||
| { | ||
| // See: https://www.pollydocs.org/strategies/retry.html | ||
| builder.AddRetry(new HttpRetryStrategyOptions | ||
| { | ||
| // Customize and configure the retry logic. | ||
| BackoffType = DelayBackoffType.Exponential, | ||
| MaxRetryAttempts = 5, | ||
| UseJitter = true | ||
| }); | ||
|
|
||
| // See: https://www.pollydocs.org/strategies/circuit-breaker.html | ||
| builder.AddCircuitBreaker(new HttpCircuitBreakerStrategyOptions | ||
| { | ||
| // Customize and configure the circuit breaker logic. | ||
| SamplingDuration = TimeSpan.FromSeconds(10), | ||
| FailureRatio = 0.2, | ||
| MinimumThroughput = 3, | ||
| ShouldHandle = static args => | ||
| { | ||
| return ValueTask.FromResult(args is | ||
| { | ||
| Outcome.Result.StatusCode: | ||
| HttpStatusCode.RequestTimeout or | ||
| HttpStatusCode.TooManyRequests | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| // See: https://www.pollydocs.org/strategies/timeout.html | ||
| builder.AddTimeout(TimeSpan.FromSeconds(5)); | ||
| }); | ||
| // </custom> | ||
| } | ||
|
|
||
| private static void WithAdvancedCustomHandler(IHttpClientBuilder httpClientBuilder) | ||
| { | ||
| // <advanced> | ||
| httpClientBuilder.AddResilienceHandler( | ||
| "AdvancedPipeline", | ||
| static (ResiliencePipelineBuilder<HttpResponseMessage> builder, | ||
| ResilienceHandlerContext context) => | ||
| { | ||
| // Enable the reloads whenever the named options change | ||
IEvangelist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| context.EnableReloads<HttpRetryStrategyOptions>("RetryOptions"); | ||
|
|
||
| // Retrieve the named options | ||
| var retryOptions = | ||
| context.GetOptions<HttpRetryStrategyOptions>("RetryOptions"); | ||
|
|
||
| // Add retries using the resolved options | ||
| builder.AddRetry(retryOptions); | ||
| }); | ||
| // </advanced> | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
docs/core/resilience/snippets/http-resilience/Program.HedgingHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Http.Resilience; | ||
|
|
||
| internal partial class Program | ||
| { | ||
| private static void WithStandardHedgingHandler(IHttpClientBuilder httpClientBuilder) | ||
| { | ||
| // <standard> | ||
| httpClientBuilder.AddStandardHedgingHandler(); | ||
| // </standard> | ||
| } | ||
|
|
||
| private static void WithConfiguredStandardHedgingHandler(IHttpClientBuilder httpClientBuilder) | ||
| { | ||
| // <ordered> | ||
| httpClientBuilder.AddStandardHedgingHandler(static (IRoutingStrategyBuilder builder) => | ||
| { | ||
| // Hedging allows sending multiple concurrent requests | ||
| builder.ConfigureOrderedGroups(static options => | ||
| { | ||
| options.Groups.Add(new UriEndpointGroup() | ||
| { | ||
| Endpoints = | ||
| { | ||
| // Imagine a scenario where 3% of the requests are | ||
| // sent to the experimental endpoint. | ||
| new() { Uri = new("https://example.net/api/experimental"), Weight = 3 }, | ||
| new() { Uri = new("https://example.net/api/stable"), Weight = 97 } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| // </ordered> | ||
|
|
||
| // <weighted> | ||
| httpClientBuilder.AddStandardHedgingHandler(static (IRoutingStrategyBuilder builder) => | ||
| { | ||
| // Hedging allows sending multiple concurrent requests | ||
| builder.ConfigureWeightedGroups(static options => | ||
| { | ||
| options.SelectionMode = WeightedGroupSelectionMode.EveryAttempt; | ||
|
|
||
| options.Groups.Add(new WeightedUriEndpointGroup() | ||
| { | ||
| Endpoints = | ||
| { | ||
| // Imagine a/b testing | ||
| new() { Uri = new("https://example.net/api/a"), Weight = 33 }, | ||
| new() { Uri = new("https://example.net/api/b"), Weight = 33 }, | ||
| new() { Uri = new("https://example.net/api/c"), Weight = 33 } | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| // </weighted> | ||
| } | ||
| } | ||
|
|
22 changes: 22 additions & 0 deletions
22
docs/core/resilience/snippets/http-resilience/Program.ResilienceHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Polly; | ||
|
|
||
| internal partial class Program | ||
| { | ||
| private static void WithStandardHandler(IHttpClientBuilder httpClientBuilder) | ||
| { | ||
| // <standard> | ||
| httpClientBuilder.AddStandardResilienceHandler(); | ||
| // </standard> | ||
| } | ||
|
|
||
| private static void WithConfiguredStandardHandler(IHttpClientBuilder httpClientBuilder) | ||
| { | ||
| // <configure> | ||
| httpClientBuilder.AddStandardResilienceHandler(static options => | ||
| { | ||
| options.Retry.BackoffType = DelayBackoffType.Linear; | ||
| }); | ||
| // </configure> | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
docs/core/resilience/snippets/http-resilience/Program.RetryOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Http.Resilience; | ||
|
|
||
| internal partial class Program | ||
| { | ||
| private static void ConfigureRetryOptions(HostApplicationBuilder builder) | ||
| { | ||
| // <options> | ||
| var section = | ||
| builder.Configuration.GetSection("RetryOptions"); | ||
IEvangelist marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| builder.Services.Configure<HttpRetryStrategyOptions>(section); | ||
| // </options> | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
docs/core/resilience/snippets/http-resilience/Program.ServiceCollection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Http.Resilience.Example; | ||
|
|
||
| internal partial class Program | ||
| { | ||
| private static void CreateServiceCollection() | ||
| { | ||
| // <services> | ||
| var services = new ServiceCollection(); | ||
|
|
||
| var httpClientBuilder = services.AddHttpClient<ExampleClient>( | ||
| configureClient: static client => | ||
| { | ||
| client.BaseAddress = new("https://jsonplaceholder.typicode.com"); | ||
| }); | ||
| // </services> | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.