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

[Docs] Improve timeout docs #1767

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 53 additions & 0 deletions docs/strategies/timeout.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

---

The timeout resilience strategy cancels the execution if it does not complete within the specified timeout period. If the execution is canceled by the timeout strategy, it throws a `TimeoutRejectedException`. The timeout strategy operates by wrapping the incoming cancellation token with a new one. Should the original token be canceled, the timeout strategy will transparently honor the original cancellation token without throwing a `TimeoutRejectedException`.

> ![IMPORTANT]
> It is crucial that the user's callback respects the cancellation token. If it does not, the callback will continue executing even after a cancellation request, thereby ignoring the cancellation.

## Usage

<!-- snippet: timeout -->
Expand Down Expand Up @@ -119,3 +124,51 @@ sequenceDiagram
T->>P: Throws <br/>TimeoutRejectedException
P->>C: Propagates exception
```

## Anti-patterns

### Ignoring Cancellation Token
Copy link
Contributor

Choose a reason for hiding this comment

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

On other pages the anti-patterns are numbered. I think it might make sense to add numbering here as well

Suggested change
### Ignoring Cancellation Token
### 1 - Ignoring Cancellation Token

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does the number add any value though?

Copy link
Contributor

Choose a reason for hiding this comment

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

If you have multiple paragraphs and one might refer to a previous one then its number could enough.

But I also did not follow this :D

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me delete all the numbers in my next PR where I change the [!IMPORTANT] blocks to [!INFO]


❌ DON'T

Ignore the cancellation token provided by the resilience pipeline:

<!-- snippet: timeout-ignore-cancellation-token -->
```cs
var outerToken = CancellationToken.None;
martintmk marked this conversation as resolved.
Show resolved Hide resolved

var pipeline = new ResiliencePipelineBuilder()
.AddTimeout(TimeSpan.FromSeconds(1))
.Build();

await pipeline.ExecuteAsync(
async innerToken => await Task.Delay(3000, outerToken), // The delay call should use innerToken
Copy link
Contributor

Choose a reason for hiding this comment

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

In every other places we use TimeSpan.FromXYZ in the examples. I would suggest to use here as well.

Suggested change
async innerToken => await Task.Delay(3000, outerToken), // The delay call should use innerToken
async innerToken => await Task.Delay(TimeSpan.FromSeconds(3), outerToken), // The delay call should use innerToken

outerToken);
```
<!-- endSnippet -->

**Reasoning**:

The provided callback ignores the `innerToken` passed from the pipeline and instead uses the `outerToken`. For this reason, the cancelled `innerToken` is ignored, and the callback is not cancelled within 1 second.

✅ DO

Respect the cancellation token provided by the pipeline:

<!-- snippet: timeout-respect-cancellation-token -->
```cs
var outerToken = CancellationToken.None;

var pipeline = new ResiliencePipelineBuilder()
.AddTimeout(TimeSpan.FromSeconds(1))
.Build();

await pipeline.ExecuteAsync(
async innerToken => await Task.Delay(3000, innerToken),
martincostello marked this conversation as resolved.
Show resolved Hide resolved
outerToken);
```
<!-- endSnippet -->

**Reasoning**:

The provided callback respects the `innerToken` provided by the pipeline, and as a result, the callback is correctly cancelled by the timeout strategy after 1 second.
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
The provided callback respects the `innerToken` provided by the pipeline, and as a result, the callback is correctly cancelled by the timeout strategy after 1 second.
The provided callback respects the `innerToken` provided by the pipeline, and as a result, the callback is correctly cancelled by the timeout strategy after 1 second plus `TimeoutRejectedException` is thrown.

2 changes: 1 addition & 1 deletion src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class RetryStrategyOptions<TResult> : ResilienceStrategyOptions
/// <see cref="DelayBackoffType.Constant"/> Represents the constant delay between retries.
/// </item>
/// </list>
/// This property is ignored when <see cref="DelayGenerator"/> is set.
/// This property is ignored when <see cref="DelayGenerator"/> is set and returns a valid <see cref="TimeSpan"/> value.
/// </remarks>
/// <value>
/// The default value is 2 seconds.
Expand Down
34 changes: 34 additions & 0 deletions src/Snippets/Docs/Timeout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,38 @@ public static async Task Usage()

#endregion
}

public static async Task IgnoreCancellationToken()
{
#region timeout-ignore-cancellation-token

var outerToken = CancellationToken.None;

var pipeline = new ResiliencePipelineBuilder()
.AddTimeout(TimeSpan.FromSeconds(1))
.Build();

await pipeline.ExecuteAsync(
async innerToken => await Task.Delay(3000, outerToken), // The delay call should use innerToken
outerToken);

#endregion
}

public static async Task RespectCancellationToken()
{
#region timeout-respect-cancellation-token

var outerToken = CancellationToken.None;

var pipeline = new ResiliencePipelineBuilder()
.AddTimeout(TimeSpan.FromSeconds(1))
.Build();

await pipeline.ExecuteAsync(
async innerToken => await Task.Delay(3000, innerToken),
outerToken);

#endregion
}
}