Skip to content

Timeout behavior support#1058

Merged
jbogard merged 4 commits intoLuckyPennySoftware:masterfrom
zachpainter77:timeout-behavior-support
Sep 11, 2024
Merged

Timeout behavior support#1058
jbogard merged 4 commits intoLuckyPennySoftware:masterfrom
zachpainter77:timeout-behavior-support

Conversation

@zachpainter77
Copy link
Copy Markdown
Contributor

@zachpainter77 zachpainter77 commented Aug 3, 2024

This PR adds support for creating a timeout behavior.

This PR is in direct response to #1056 , and the long discussion of #592. This was an older issue turned to discussion. I have solved the issue with the below implementation.

The below implementation modifies the RequestHandlerDelegate to take an optional CancellationToken. I also modify the RequestHandlerWrapperImpl to match the delegate so the casting still works. Then I modify the Aggregation to pass the token into the handle method for the next handler.

Usage:

 public class TimeoutBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
     where TRequest : notnull
 {
     public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
     {
         using (var cts = new CancellationTokenSource(someTimeoutDuration))
         {
             return await next(cts.Token);
         }
     }
 }

Test Handler:

 public class TimeoutRequestHandler : IRequestHandler<TimeoutRequest>
 {
     private readonly Dependency _dependency;

     public TimeoutRequestHandler(Dependency dependency) => _dependency = dependency;

     public async Task Handle(TimeoutRequest request, CancellationToken cancellationToken)
     {
         await Task.Delay(someTimeThatIsLongerThanTheTimeoutDuration, cancellationToken);

         _dependency.Called = true;
     }
 }

Unit Test:

 [Fact]
 public async Task TimeoutBehavior_Void_Should_Cancel_Long_Running_Task_And_Throw_Exception()
 {
     var request = new TimeoutRequest();

     var exception = await Should.ThrowAsync<TaskCanceledException>(() => _mediator.Send(request));

     exception.ShouldNotBeNull();
     exception.ShouldBeAssignableTo<TaskCanceledException>();
     _dependency.Called.ShouldBeFalse();
 }

@zachpainter77 zachpainter77 mentioned this pull request Aug 3, 2024
@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open 28 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the Stale label Aug 31, 2024
@dazinator
Copy link
Copy Markdown

not stale.. still waiting for feedback and hopeful merge..

@jbogard jbogard merged commit 0d3bf4c into LuckyPennySoftware:master Sep 11, 2024
This was referenced Jul 21, 2025
sloweyyy added a commit to sloweyyy/cloud-native-ecommerce-platform that referenced this pull request Mar 22, 2026
Updated [MediatR](https://github.com/jbogard/MediatR) from 12.3.0 to
12.5.0.

<details>
<summary>Release notes</summary>

_Sourced from [MediatR's
releases](https://github.com/jbogard/MediatR/releases)._

## 12.5.0

## What's Changed
* Open behavior multiple registration extensions by @​Emopusta in
LuckyPennySoftware/MediatR#1065
* Remove duplicate `Nullable` property from `MediatR.Contracts` by
@​jithu7432 in LuckyPennySoftware/MediatR#1061
* Timeout behavior support by @​zachpainter77 in
LuckyPennySoftware/MediatR#1058
* GitHub Actions upload-artifacts@​v2 deprecated moving to v4 by
@​jithu7432 in LuckyPennySoftware/MediatR#1072
* update MinVer from 4.3.0 to 6.0.0 by @​adamralph in
LuckyPennySoftware/MediatR#1102
* Update setup-dotnet package version. by @​Emopusta in
LuckyPennySoftware/MediatR#1086
* Add test for multiple open behavior registration feature. by
@​Emopusta in LuckyPennySoftware/MediatR#1077
* Add validation and comments to OpenBehavior entity. by @​Emopusta in
LuckyPennySoftware/MediatR#1078
* Passing CancellationToken to the call chain by @​podobaas in
LuckyPennySoftware/MediatR#1100

## New Contributors
* @​Emopusta made their first contribution in
LuckyPennySoftware/MediatR#1065
* @​jithu7432 made their first contribution in
LuckyPennySoftware/MediatR#1061
* @​podobaas made their first contribution in
LuckyPennySoftware/MediatR#1100

**Full Changelog**:
LuckyPennySoftware/MediatR@v12.4.1...v12.5.0

## 12.4.1

## What's Changed
* Make Auto Registration of Generic Handlers OPT-IN by @​zachpainter77
in LuckyPennySoftware/MediatR#1057


**Full Changelog**:
LuckyPennySoftware/MediatR@v12.4.0...v12.4.1

## 12.4.0

## What's Changed
* Add Support For Generic Handlers With Multiple Generic Type Parameters
by @​zachpainter77 in LuckyPennySoftware/MediatR#1048


**Full Changelog**:
LuckyPennySoftware/MediatR@v12.3.0...v12.4.0

Commits viewable in [compare
view](LuckyPennySoftware/MediatR@v12.3.0...v12.5.0).
</details>

You can trigger a rebase of this PR by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

> **Note**
> Automatic rebases have been disabled on this pull request as it has
been open for over 30 days.
sloweyyy added a commit to sloweyyy/cloud-native-ecommerce-platform that referenced this pull request Mar 25, 2026
Updated [MediatR](https://github.com/jbogard/MediatR) from 12.3.0 to
12.5.0.

<details>
<summary>Release notes</summary>

_Sourced from [MediatR's
releases](https://github.com/jbogard/MediatR/releases)._

## 12.5.0

## What's Changed
* Open behavior multiple registration extensions by @​Emopusta in
LuckyPennySoftware/MediatR#1065
* Remove duplicate `Nullable` property from `MediatR.Contracts` by
@​jithu7432 in LuckyPennySoftware/MediatR#1061
* Timeout behavior support by @​zachpainter77 in
LuckyPennySoftware/MediatR#1058
* GitHub Actions upload-artifacts@​v2 deprecated moving to v4 by
@​jithu7432 in LuckyPennySoftware/MediatR#1072
* update MinVer from 4.3.0 to 6.0.0 by @​adamralph in
LuckyPennySoftware/MediatR#1102
* Update setup-dotnet package version. by @​Emopusta in
LuckyPennySoftware/MediatR#1086
* Add test for multiple open behavior registration feature. by
@​Emopusta in LuckyPennySoftware/MediatR#1077
* Add validation and comments to OpenBehavior entity. by @​Emopusta in
LuckyPennySoftware/MediatR#1078
* Passing CancellationToken to the call chain by @​podobaas in
LuckyPennySoftware/MediatR#1100

## New Contributors
* @​Emopusta made their first contribution in
LuckyPennySoftware/MediatR#1065
* @​jithu7432 made their first contribution in
LuckyPennySoftware/MediatR#1061
* @​podobaas made their first contribution in
LuckyPennySoftware/MediatR#1100

**Full Changelog**:
LuckyPennySoftware/MediatR@v12.4.1...v12.5.0

## 12.4.1

## What's Changed
* Make Auto Registration of Generic Handlers OPT-IN by @​zachpainter77
in LuckyPennySoftware/MediatR#1057


**Full Changelog**:
LuckyPennySoftware/MediatR@v12.4.0...v12.4.1

## 12.4.0

## What's Changed
* Add Support For Generic Handlers With Multiple Generic Type Parameters
by @​zachpainter77 in LuckyPennySoftware/MediatR#1048


**Full Changelog**:
LuckyPennySoftware/MediatR@v12.3.0...v12.4.0

Commits viewable in [compare
view](LuckyPennySoftware/MediatR@v12.3.0...v12.5.0).
</details>

You can trigger a rebase of this PR by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

> **Note**
> Automatic rebases have been disabled on this pull request as it has
been open for over 30 days.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants