Skip to content

Commit 22b61e2

Browse files
Tratchermartintmk
andauthored
Http resilience readme (#4663)
* Http resilience readme * Update HttpStandardHedgingResilienceOptions.cs * Apply suggestions from code review Co-authored-by: martintmk <[email protected]> * Custom pipeline * Fixup * Better pipeline --------- Co-authored-by: martintmk <[email protected]>
1 parent 9921fb5 commit 22b61e2

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

src/Libraries/Microsoft.Extensions.Http.Resilience/Hedging/HttpStandardHedgingResilienceOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Microsoft.Extensions.Http.Resilience;
2222
/// <item><description>Total request timeout strategy applies an overall timeout to the execution,
2323
/// ensuring that the request including hedging attempts does not exceed the configured limit.</description></item>
2424
/// <item><description>The hedging strategy executes the requests against multiple endpoints in case the dependency is slow or returns a transient error.</description></item>
25-
/// <item><description>The bulkhead policy limits the maximum number of concurrent requests being send to the dependency.</description></item>
25+
/// <item><description>The rate limiter pipeline limits the maximum number of requests being send to the dependency.</description></item>
2626
/// <item><description>The circuit breaker blocks the execution if too many direct failures or timeouts are detected.</description></item>
2727
/// <item><description>The attempt timeout strategy limits each request attempt duration and throws if its exceeded.</description></item>
2828
/// </list>

src/Libraries/Microsoft.Extensions.Http.Resilience/Microsoft.Extensions.Http.Resilience.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<RootNamespace>Microsoft.Extensions.Http.Resilience</RootNamespace>
4-
<Description>Resilience mechanisms for HTTP Client.</Description>
4+
<Description>Resilience mechanisms for HttpClient.</Description>
55
<Workstream>Resilience</Workstream>
66
</PropertyGroup>
77

src/Libraries/Microsoft.Extensions.Http.Resilience/README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Microsoft.Extensions.Http.Resilience
22

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

55
## Install the package
66

@@ -18,6 +18,60 @@ Or directly in the C# project file:
1818
</ItemGroup>
1919
```
2020

21+
## Usage Examples
22+
23+
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.
24+
- 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.
25+
- The retry pipeline retries the request in case the dependency is slow or returns a transient error.
26+
- The rate limiter pipeline limits the maximum number of requests being send to the dependency.
27+
- The circuit breaker blocks the execution if too many direct failures or timeouts are detected.
28+
- The attempt timeout pipeline limits each request attempt duration and throws if its exceeded.
29+
30+
### Resilience
31+
32+
The standard resilience pipeline makes use of the above strategies to ensure HTTP requests can be sent reliably.
33+
34+
```csharp
35+
var clientBuilder = services.AddHttpClient("MyClient");
36+
37+
clientBuilder.AddStandardResilienceHandler().Configure(o =>
38+
{
39+
o.CircuitBreaker.MinimumThroughput = 10;
40+
});
41+
```
42+
43+
### Hedging
44+
45+
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.
46+
47+
```csharp
48+
var clientBuilder = services.AddHttpClient("MyClient");
49+
50+
clientBuilder.AddStandardHedgingHandler().Configure(o =>
51+
{
52+
o.TotalRequestTimeout.Timeout = TimeSpan.FromSeconds(10);
53+
});
54+
```
55+
56+
### Custom Resilience
57+
58+
For more granular control a custom pipeline can be constructed.
59+
60+
```csharp
61+
var clientBuilder = services.AddHttpClient("MyClient");
62+
63+
clientBuilder.AddResilienceHandler("myHandler", b =>
64+
{
65+
b.AddFallback(new FallbackStrategyOptions<HttpResponseMessage>()
66+
{
67+
FallbackAction = _ => Outcome.FromResultAsValueTask(new HttpResponseMessage(HttpStatusCode.ServiceUnavailable))
68+
})
69+
.AddConcurrencyLimiter(100)
70+
.AddRetry(new HttpRetryStrategyOptions())
71+
.AddCircuitBreaker(new HttpCircuitBreakerStrategyOptions())
72+
.AddTimeout(new HttpTimeoutStrategyOptions());
73+
});
74+
```
2175

2276
## Feedback & Contributing
2377

0 commit comments

Comments
 (0)