forked from Jroland/kafka-net
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from gigya/AddTaskExtensionAndConfigureAwait
ConfigureAwait(false) And task Extensions
- Loading branch information
Showing
8 changed files
with
85 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace KafkaNet.Common | ||
{ | ||
/// <summary> | ||
/// Utility functions for dealing with Task's. | ||
/// </summary> | ||
/// <remarks> | ||
/// This is copy of orleans TaskExtensions here:https://github.com/dotnet/orleans/blob/master/src/Orleans/Async/TaskExtensions.cs#L218 | ||
/// </remarks> | ||
|
||
public static class TaskExtensions | ||
{ | ||
|
||
/// <summary> | ||
/// Observes and ignores a potential exception on a given Task. | ||
/// If a Task fails and throws an exception which is never observed, it will be caught by the .NET finalizer thread. | ||
/// This function awaits the given task and if the exception is thrown, it observes this exception and simply ignores it. | ||
/// This will prevent the escalation of this exception to the .NET finalizer thread. | ||
/// </summary> | ||
/// <param name="task">The task to be ignored.</param> | ||
[SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "ignored")] | ||
public static void Ignore(this Task task) | ||
{ | ||
if (task.IsCompleted) | ||
{ | ||
var ignored = task.Exception; | ||
} | ||
else | ||
{ | ||
task.ContinueWith( | ||
t => { var ignored = t.Exception; }, | ||
CancellationToken.None, | ||
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously, | ||
TaskScheduler.Default); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This will apply a timeout delay to the task, allowing us to exit early | ||
/// </summary> | ||
/// <param name="taskToComplete">The task we will timeout after timeSpan</param> | ||
/// <param name="timeout">Amount of time to wait before timing out</param> | ||
/// <exception cref="TimeoutException">If we time out we will get this exception</exception> | ||
/// <returns>The value of the completed task</returns> | ||
|
||
public static async Task<T> WithTimeout<T>(this Task<T> taskToComplete, TimeSpan timeSpan) | ||
{ | ||
if (taskToComplete.IsCompleted) | ||
{ | ||
return await taskToComplete; | ||
} | ||
|
||
var timeoutCancellationTokenSource = new CancellationTokenSource(); | ||
var completedTask = await Task.WhenAny(taskToComplete, Task.Delay(timeSpan, timeoutCancellationTokenSource.Token)); | ||
|
||
// We got done before the timeout, or were able to complete before this code ran, return the result | ||
if (taskToComplete == completedTask) | ||
{ | ||
timeoutCancellationTokenSource.Cancel(); | ||
// Await this so as to propagate the exception correctly | ||
return await taskToComplete; | ||
} | ||
|
||
// We did not complete before the timeout, we fire and forget to ensure we observe any exceptions that may occur | ||
taskToComplete.Ignore(); | ||
throw new TimeoutException(String.Format("WithTimeout has timed out after {0}.", timeSpan)); | ||
} | ||
|
||
} | ||
} |
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
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