-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Docs] Add docs for individual resilience strategies #1553
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
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,99 @@ | ||
| # Circuit breaker resilience strategy | ||
|
|
||
| ## About | ||
|
|
||
| - **Options**: | ||
| - [`CircuitBreakerStrategyOptions`](../../src/Polly.Core/CircuitBreaker/CircuitBreakerStrategyOptions.cs) | ||
| - [`CircuitBreakerStrategyOptions<T>`](../../src/Polly.Core/CircuitBreaker/CircuitBreakerStrategyOptions.TResult.cs) | ||
| - **Extensions**: `AddCircuitBreaker` | ||
| - **Strategy Type**: Reactive | ||
| - **Exceptions**: | ||
| - `BrokenCircuitException`: Thrown when a circuit is broken and the action could not be executed. | ||
| - `IsolatedCircuitException`: Thrown when a circuit is isolated (held open) by manual override. | ||
|
|
||
| > [!NOTE] | ||
| > Version 8 documentation for this strategy has not yet been migrated. For more information on circuit breaker concepts and behavior, refer to the [older documentation](https://github.com/App-vNext/Polly/wiki/Circuit-Breaker). | ||
|
|
||
| > [!NOTE] | ||
| > Be aware that the Circuit Breaker strategy [rethrows all exceptions](https://github.com/App-vNext/Polly/wiki/Circuit-Breaker#exception-handling), including those that are handled. A Circuit Breaker's role is to monitor faults and break the circuit when a certain threshold is reached; it does not manage retries. Combine the Circuit Breaker with a Retry strategy if needed. | ||
|
|
||
| ## Usage | ||
|
|
||
| <!-- snippet: circuit-breaker --> | ||
| ```cs | ||
| // Add circuit breaker with default options. | ||
| // See https://github.com/App-vNext/Polly/blob/main/docs/strategies/circuit-breaker.md#defaults for default values. | ||
| new ResiliencePipelineBuilder().AddCircuitBreaker(new CircuitBreakerStrategyOptions()); | ||
|
|
||
| // Add circuit breaker with customized options: | ||
| // | ||
| // The circuit will break if more than 50% of actions result in handled exceptions, | ||
| // within any 10-second sampling duration, and at least 8 actions are processed. | ||
| new ResiliencePipelineBuilder().AddCircuitBreaker(new CircuitBreakerStrategyOptions | ||
| { | ||
| FailureRatio = 0.5, | ||
| SamplingDuration = TimeSpan.FromSeconds(10), | ||
| MinimumThroughput = 8, | ||
| BreakDuration = TimeSpan.FromSeconds(30), | ||
| ShouldHandle = new PredicateBuilder().Handle<SomeExceptionType>() | ||
| }); | ||
|
|
||
| // Handle specific failed results for HttpResponseMessage: | ||
| new ResiliencePipelineBuilder<HttpResponseMessage>() | ||
| .AddCircuitBreaker(new CircuitBreakerStrategyOptions<HttpResponseMessage> | ||
| { | ||
| ShouldHandle = new PredicateBuilder<HttpResponseMessage>() | ||
| .Handle<SomeExceptionType>() | ||
| .HandleResult(response => response.StatusCode == HttpStatusCode.InternalServerError) | ||
| }); | ||
|
|
||
| // Monitor the circuit state, useful for health reporting: | ||
| var stateProvider = new CircuitBreakerStateProvider(); | ||
|
|
||
| new ResiliencePipelineBuilder<HttpResponseMessage>() | ||
| .AddCircuitBreaker(new() { StateProvider = stateProvider }) | ||
| .Build(); | ||
|
|
||
| /* | ||
| CircuitState.Closed - Normal operation; actions are executed. | ||
| CircuitState.Open - Circuit is open; actions are blocked. | ||
| CircuitState.HalfOpen - Recovery state after break duration expires; actions are permitted. | ||
| CircuitState.Isolated - Circuit is manually held open; actions are blocked. | ||
| */ | ||
|
|
||
| // Manually control the Circuit Breaker state: | ||
| var manualControl = new CircuitBreakerManualControl(); | ||
|
|
||
| new ResiliencePipelineBuilder() | ||
| .AddCircuitBreaker(new() { ManualControl = manualControl }) | ||
| .Build(); | ||
|
|
||
| // Manually isolate a circuit, e.g., to isolate a downstream service. | ||
| await manualControl.IsolateAsync(); | ||
|
|
||
| // Manually close the circuit to allow actions to be executed again. | ||
| await manualControl.CloseAsync(); | ||
| ``` | ||
| <!-- endSnippet --> | ||
|
|
||
| ## Defaults | ||
|
|
||
| | Property | Default Value | Description | | ||
| | ------------------- | -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | | ||
| | `ShouldHandle` | Predicate that handles all exceptions except `OperationCanceledException`. | Specifies which results and exceptions are managed by the circuit breaker strategy. | | ||
| | `FailureRatio` | 0.1 | The ratio of failures to successes that will cause the circuit to break. | | ||
| | `MinimumThroughput` | 100 | The minimum number of actions that must occur in the circuit within a specific time slice. | | ||
| | `SamplingDuration` | 30 seconds | The time period over which failure ratios are calculated. | | ||
| | `BreakDuration` | 5 seconds | The time period for which the circuit will remain open before attempting to reset. | | ||
martincostello marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| | `OnClosed` | `Null` | Event triggered when the circuit transitions to the `Closed` state. | | ||
| | `OnOpened` | `Null` | Event triggered when the circuit transitions to the `Opened` state. | | ||
| | `OnHalfOpened` | `Null` | Event triggered when the circuit transitions to the `HalfOpened` state. | | ||
| | `ManualControl` | `Null` | Allows for manual control to isolate or close the circuit. | | ||
| | `StateProvider` | `Null` | Enables the retrieval of the current state of the circuit. | | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Making the Netflix API More Resilient](https://techblog.netflix.com/2011/12/making-netflix-api-more-resilient.html) | ||
| - [Circuit Breaker by Martin Fowler](https://martinfowler.com/bliki/CircuitBreaker.html) | ||
| - [Circuit Breaker Pattern by Microsoft](https://msdn.microsoft.com/en-us/library/dn589784.aspx) | ||
| - [Original Circuit Breaking Article](https://web.archive.org/web/20160106203951/http://thatextramile.be/blog/2008/05/the-circuit-breaker) | ||
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.