Commit 53427697 authored by Nick Craver's avatar Nick Craver

Adapt clearer code from #926

While these were being cancelled already - we can still tidy into the extensions. When they were added we were competing on edits...now that we're clear we can do that cleanup pass.
parent 7dfaee46
...@@ -668,10 +668,7 @@ private async Task<bool> WaitAllIgnoreErrorsAsync(Task[] tasks, int timeoutMilli ...@@ -668,10 +668,7 @@ private async Task<bool> WaitAllIgnoreErrorsAsync(Task[] tasks, int timeoutMilli
} }
var allTasks = Task.WhenAll(tasks).ObserveErrors(); var allTasks = Task.WhenAll(tasks).ObserveErrors();
var cts = new CancellationTokenSource(); bool all = await allTasks.TimeoutAfter(timeoutMs: remaining).ObserveErrors().ForAwait();
var any = Task.WhenAny(allTasks, Task.Delay(remaining, cts.Token)).ObserveErrors();
bool all = await any.ForAwait() == allTasks;
cts.Cancel();
LogLockedWithThreadPoolStats(log, all ? "All tasks completed cleanly" : $"Not all tasks completed cleanly (from {caller}#{callerLineNumber}, timeout {timeoutMilliseconds}ms)", out busyWorkerCount); LogLockedWithThreadPoolStats(log, all ? "All tasks completed cleanly" : $"Not all tasks completed cleanly (from {caller}#{callerLineNumber}, timeout {timeoutMilliseconds}ms)", out busyWorkerCount);
return all; return all;
} }
...@@ -1351,13 +1348,11 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, Text ...@@ -1351,13 +1348,11 @@ internal async Task<bool> ReconfigureAsync(bool first, bool reconfigureAll, Text
{ {
if (RawConfig.ResolveDns && RawConfig.HasDnsEndPoints()) if (RawConfig.ResolveDns && RawConfig.HasDnsEndPoints())
{ {
var cts = new CancellationTokenSource();
var dns = RawConfig.ResolveEndPointsAsync(this, log).ObserveErrors(); var dns = RawConfig.ResolveEndPointsAsync(this, log).ObserveErrors();
if ((await Task.WhenAny(dns, Task.Delay(TimeoutMilliseconds, cts.Token)).ForAwait()) != dns) if (!await dns.TimeoutAfter(TimeoutMilliseconds).ForAwait())
{ {
throw new TimeoutException("Timeout resolving endpoints"); throw new TimeoutException("Timeout resolving endpoints");
} }
cts.Cancel();
} }
foreach (var endpoint in RawConfig.EndPoints) foreach (var endpoint in RawConfig.EndPoints)
{ {
......
...@@ -34,31 +34,18 @@ public static Task<T> ObserveErrors<T>(this Task<T> task) ...@@ -34,31 +34,18 @@ public static Task<T> ObserveErrors<T>(this Task<T> task)
// Inspired from https://github.com/dotnet/corefx/blob/81a246f3adf1eece3d981f1d8bb8ae9de12de9c6/src/Common/tests/System/Threading/Tasks/TaskTimeoutExtensions.cs#L15-L43 // Inspired from https://github.com/dotnet/corefx/blob/81a246f3adf1eece3d981f1d8bb8ae9de12de9c6/src/Common/tests/System/Threading/Tasks/TaskTimeoutExtensions.cs#L15-L43
// Licensed to the .NET Foundation under one or more agreements. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license. // The .NET Foundation licenses this file to you under the MIT license.
public static async Task TimeoutAfter(this Task task, int millisecondsTimeout, string message) public static async Task<bool> TimeoutAfter(this Task task, int timeoutMs)
{ {
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
if (task == await Task.WhenAny(task, Task.Delay(millisecondsTimeout, cts.Token)).ConfigureAwait(false)) if (task == await Task.WhenAny(task, Task.Delay(timeoutMs, cts.Token)).ConfigureAwait(false))
{ {
cts.Cancel(); cts.Cancel();
await task.ConfigureAwait(false); await task.ConfigureAwait(false);
return true;
} }
else else
{ {
throw new TimeoutException(message); return false;
}
}
public static async Task<TResult> TimeoutAfter<TResult>(this Task<TResult> task, int millisecondsTimeout, string message)
{
var cts = new CancellationTokenSource();
if (task == await Task<TResult>.WhenAny(task, Task<TResult>.Delay(millisecondsTimeout, cts.Token)).ConfigureAwait(false))
{
cts.Cancel();
return await task.ConfigureAwait(false);
}
else
{
throw new TimeoutException(message);
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment