Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 12 additions & 5 deletions docs/chaos/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,27 @@ builder
// Finally, configure chaos strategies if you want to inject chaos.
// These should come after the regular resilience strategies.

// 2% of invocations will be injected with chaos
const double InjectionRate = 0.02;
// 2% of total requests will be injected with chaos fault.
const double faultInjectionRate = 0.02;
// For the other 98% of total requests, 50% of them will be injected with latency. Then 49% of total request will be injected with chaos latency.
const double latencyInjectionRate = 0.50;
// For the other 49% of total requests, 10% of them will be injected with outcome. Then 4.9% of total request will be injected with chaos outcome.
const double outcomeInjectionRate = 0.10;

builder
.AddChaosLatency(InjectionRate, TimeSpan.FromMinutes(1)) // Inject a chaos latency to executions
.AddChaosFault(InjectionRate, () => new InvalidOperationException("Injected by chaos strategy!")) // Inject a chaos fault to executions
.AddChaosOutcome(InjectionRate, () => new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)) // Inject a chaos outcome to executions
.AddChaosFault(faultInjectionRate, () => new InvalidOperationException("Injected by chaos strategy!")) // Inject a chaos fault to executions
.AddChaosLatency(latencyInjectionRate, TimeSpan.FromMinutes(1)) // Inject a chaos latency to executions
.AddChaosOutcome(outcomeInjectionRate, () => new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)) // Inject a chaos outcome to executions
.AddChaosBehavior(0.001, cancellationToken => RestartRedisAsync(cancellationToken)); // Inject a chaos behavior to executions
```
<!-- endSnippet -->

> [!NOTE]
> It is usual to place the chaos strategy as the last strategy in the resilience pipeline. By placing the chaos strategies as last, they subvert the usual outbound call at the last minute, substituting their fault or adding extra latency, etc. The existing resilience strategies - further out in the `ResiliencePipeline` - still apply, so you can test how the Polly resilience strategies you have configured handle the chaos/faults injected by Simmy.

> [!NOTE]
> The `AddChaosFault`, `AddChaosLatency`, `AddChaosOutcome` will take effect sequentially if you combine them together. In the above example, we use `fault first then latency stragtegy`, it can save fault waiting time. If you put `AddChaosLatency` before `AddChaosFault`, you will get different behavior.

## Major differences

This section highlights the major differences compared to the [`Polly.Contrib.Simmy`](https://github.com/Polly-Contrib/Simmy) library:
Expand Down
14 changes: 9 additions & 5 deletions src/Snippets/Docs/Chaos.Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ public static void Usage()
// Finally, configure chaos strategies if you want to inject chaos.
// These should come after the regular resilience strategies.

// 2% of invocations will be injected with chaos
const double InjectionRate = 0.02;
// 2% of total requests will be injected with chaos fault.
const double faultInjectionRate = 0.02;
// For the other 98% of total requests, 50% of them will be injected with latency. Then 49% of total request will be injected with chaos latency.
const double latencyInjectionRate = 0.50;
// For the other 49% of total requests, 10% of them will be injected with outcome. Then 4.9% of total request will be injected with chaos outcome.
const double outcomeInjectionRate = 0.10;

builder
.AddChaosLatency(InjectionRate, TimeSpan.FromMinutes(1)) // Inject a chaos latency to executions
.AddChaosFault(InjectionRate, () => new InvalidOperationException("Injected by chaos strategy!")) // Inject a chaos fault to executions
.AddChaosOutcome(InjectionRate, () => new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)) // Inject a chaos outcome to executions
.AddChaosFault(faultInjectionRate, () => new InvalidOperationException("Injected by chaos strategy!")) // Inject a chaos fault to executions
.AddChaosLatency(latencyInjectionRate, TimeSpan.FromMinutes(1)) // Inject a chaos latency to executions
.AddChaosOutcome(outcomeInjectionRate, () => new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError)) // Inject a chaos outcome to executions
.AddChaosBehavior(0.001, cancellationToken => RestartRedisAsync(cancellationToken)); // Inject a chaos behavior to executions

#endregion
Expand Down