-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Extract WithCancellationToken so it can be reused outside o…
…f ChannelPool
- Loading branch information
1 parent
a762790
commit 475a197
Showing
4 changed files
with
48 additions
and
32 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file or at | ||
* https://developers.google.com/open-source/licenses/bsd | ||
*/ | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Google.Api.Gax.Grpc | ||
{ | ||
internal static class TaskExtensions | ||
{ | ||
// TODO: b/322527105 | ||
// Note: this is duplicated in Google.Apis.Auth, Google.Apis.Core and Google.Api.Gax.Rest as well so it can stay internal. | ||
// Please change all implementations at the same time. | ||
/// <summary> | ||
/// Returns a task which can be cancelled by the given cancellation token, but otherwise observes the original | ||
/// task's state. This does *not* cancel any work that the original task was doing, and should be used carefully. | ||
/// </summary> | ||
internal static Task<T> WithCancellationToken<T>(this Task<T> task, CancellationToken cancellationToken) | ||
{ | ||
if (!cancellationToken.CanBeCanceled) | ||
{ | ||
return task; | ||
} | ||
|
||
return ImplAsync(); | ||
|
||
// Separate async method to allow the above optimization to avoid creating any new state machines etc. | ||
async Task<T> ImplAsync() | ||
{ | ||
var cts = new TaskCompletionSource<T>(); | ||
using (cancellationToken.Register(() => cts.TrySetCanceled())) | ||
{ | ||
var completedTask = await Task.WhenAny(task, cts.Task).ConfigureAwait(false); | ||
return await completedTask.ConfigureAwait(false); | ||
} | ||
} | ||
} | ||
} | ||
} |