-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
Commits
Show all changes
3 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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -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 --> | ||||||
|
@@ -119,3 +124,51 @@ sequenceDiagram | |||||
T->>P: Throws <br/>TimeoutRejectedException | ||||||
P->>C: Propagates exception | ||||||
``` | ||||||
|
||||||
## Anti-patterns | ||||||
|
||||||
### Ignoring Cancellation Token | ||||||
|
||||||
❌ 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In every other places we use
Suggested change
|
||||||
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( | ||||||
static async innerToken => await Task.Delay(3000, innerToken), | ||||||
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
This file contains 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 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
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]