Skip to content

Commit

Permalink
Added sample for anti-pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-csala committed Jan 23, 2024
1 parent 0f5ff2b commit aa0c102
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/chaos/behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ sequenceDiagram

Use behavior strategies to inject delays.

<!-- snippet: chaos-behavior-anti-pattern-inject-delay -->
```cs
var pipeline = new ResiliencePipelineBuilder()
.AddChaosBehavior(new ChaosBehaviorStrategyOptions
{
BehaviorGenerator = static args => Task.Delay(TimeSpan.FromSeconds(7), args.Context.CancellationToken),
})
.Build();
```
<!-- endSnippet -->

✅ DO

Use the latency chaos instead as the [`ChaosLatencyStrategy`](latency.md) already correctly handles synchronous/asynchronous delay executions, cancellations, etc.

<!-- snippet: chaos-behavior-pattern-inject-delay -->
```cs
var pipeline = new ResiliencePipelineBuilder()
.AddChaosLatency(new ChaosLatencyStrategyOptions
{
Latency = TimeSpan.FromSeconds(7),
})
.Build();
```
<!-- endSnippet -->
25 changes: 25 additions & 0 deletions src/Snippets/Docs/Chaos.Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ public static void BehaviorUsage()
.Build();
#endregion
}

public static void AntiPattern_InjectDelay()
{
#region chaos-behavior-anti-pattern-inject-delay
var pipeline = new ResiliencePipelineBuilder()
.AddChaosBehavior(new ChaosBehaviorStrategyOptions
{
BehaviorGenerator = static args => Task.Delay(TimeSpan.FromSeconds(7), args.Context.CancellationToken),

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / code-ql (csharp)

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'System.Threading.Tasks.ValueTask'

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / code-ql (csharp)

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / code-ql (csharp)

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'System.Threading.Tasks.ValueTask'

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / code-ql (csharp)

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'System.Threading.Tasks.ValueTask'

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'System.Threading.Tasks.ValueTask'

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / windows-latest

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / macos-latest

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / publish-docs

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'System.Threading.Tasks.ValueTask'

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / publish-docs

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / publish-docs

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'System.Threading.Tasks.ValueTask'

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / publish-docs

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

Check failure on line 68 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
})
.Build();

#endregion
}

public static void Pattern_InjectDelay()
{
#region chaos-behavior-pattern-inject-delay
var pipeline = new ResiliencePipelineBuilder()
.AddChaosLatency(new ChaosLatencyStrategyOptions

Check failure on line 79 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / code-ql (csharp)

The type or namespace name 'ChaosLatencyStrategyOptions' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 79 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / code-ql (csharp)

The type or namespace name 'ChaosLatencyStrategyOptions' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 79 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / windows-latest

The type or namespace name 'ChaosLatencyStrategyOptions' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 79 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / windows-latest

The type or namespace name 'ChaosLatencyStrategyOptions' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 79 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / macos-latest

The type or namespace name 'ChaosLatencyStrategyOptions' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 79 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / publish-docs

The type or namespace name 'ChaosLatencyStrategyOptions' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 79 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / publish-docs

The type or namespace name 'ChaosLatencyStrategyOptions' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 79 in src/Snippets/Docs/Chaos.Behavior.cs

View workflow job for this annotation

GitHub Actions / ubuntu-latest

The type or namespace name 'ChaosLatencyStrategyOptions' could not be found (are you missing a using directive or an assembly reference?)
{
Latency = TimeSpan.FromSeconds(7),
})
.Build();
#endregion
}
}

internal class RedisConnectionException : Exception
Expand Down

0 comments on commit aa0c102

Please sign in to comment.