Skip to content
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

Http resilience readme #4663

Merged
merged 6 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Microsoft.Extensions.Http.Resilience;
/// <item><description>Total request timeout strategy applies an overall timeout to the execution,
/// ensuring that the request including hedging attempts does not exceed the configured limit.</description></item>
/// <item><description>The hedging strategy executes the requests against multiple endpoints in case the dependency is slow or returns a transient error.</description></item>
/// <item><description>The bulkhead policy limits the maximum number of concurrent requests being send to the dependency.</description></item>
/// <item><description>The rate limiter pipeline limits the maximum number of requests being send to the dependency.</description></item>
/// <item><description>The circuit breaker blocks the execution if too many direct failures or timeouts are detected.</description></item>
/// <item><description>The attempt timeout strategy limits each request attempt duration and throws if its exceeded.</description></item>
/// </list>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>Microsoft.Extensions.Http.Resilience</RootNamespace>
<Description>Resilience mechanisms for HTTP Client.</Description>
<Description>Resilience mechanisms for HttpClient.</Description>
<Workstream>Resilience</Workstream>
</PropertyGroup>

Expand Down
53 changes: 52 additions & 1 deletion src/Libraries/Microsoft.Extensions.Http.Resilience/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Microsoft.Extensions.Http.Resilience

Resilience mechanisms for HTTP Client.
Resilience mechanisms for `HttpClient` built on the [Polly framework](https://www.pollydocs.org/).

## Install the package

Expand All @@ -18,6 +18,57 @@ Or directly in the C# project file:
</ItemGroup>
```

## Usage Examples

When configuring an HttpClient through the [HTTP client factory](https://learn.microsoft.com/dotnet/core/extensions/httpclient-factory) the following extensions can add a set of pre-configured hedging or resilience behaviors. These pipelines combine multiple resilience strategies with pre-configured defaults.
- The total request timeout pipeline applies an overall timeout to the execution, ensuring that the request including hedging attempts, does not exceed the configured limit.
- The retry pipeline retries the request in case the dependency is slow or returns a transient error.
- The rate limiter pipeline limits the maximum number of requests being send to the dependency.
- The circuit breaker blocks the execution if too many direct failures or timeouts are detected.
- The attempt timeout pipeline limits each request attempt duration and throws if its exceeded.

### Resilience

The standard resilience pipeline makes use of the above strategies to ensure HTTP requests can be sent reliably.

```csharp
var clientBuilder = services.AddHttpClient("MyClient");

clientBuilder.AddStandardResilienceHandler().Configure(o =>
{
o.CircuitBreaker.MinimumThroughput = 10;
});
```

### Hedging

The standard hedging pipeline uses a pool of circuit breakers to ensure that unhealthy endpoints are not hedged against. By default, the selection from pool is based on the URL Authority (scheme + host + port). It is recommended that you configure the way the strategies are selected by calling the `SelectPipelineByAuthority()` extensions. The last three strategies are applied to each individual endpoint.

```csharp
var clientBuilder = services.AddHttpClient("MyClient");

clientBuilder.AddStandardHedgingHandler().Configure(o =>
{
o.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(10);
});
```

### Custom Resilience

For more granular control a custom pipeline can be constructed.

```csharp
var clientBuilder = services.AddHttpClient("MyClient");

clientBuilder.AddResilienceHandler("myHandler", b =>
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
{
b.AddConcurrencyLimiter(new ConcurrencyLimiterOptions())
Tratcher marked this conversation as resolved.
Show resolved Hide resolved
.AddTimeout(new TimeoutStrategyOptions())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.AddTimeout(new TimeoutStrategyOptions())
.AddTimeout(new HttpTimeoutStrategyOptions())

It's better to use http specific options when possible. Applies to other as well. (retries, CB)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is HttpTimeoutStrategyOptions better when it doesn't have any properties? Why does it even exist?

.AddRetry(new RetryStrategyOptions<HttpResponseMessage>())
.AddFallback(new FallbackStrategyOptions<HttpResponseMessage>())
.AddCircuitBreaker(new CircuitBreakerStrategyOptions<HttpResponseMessage>());
});
```

## Feedback & Contributing
Tratcher marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading