考虑这段代码:
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Polly;
using Polly.Timeout;
namespace Test
{
public class Program
{
public static async Task Main()
{
var tasks = new List<Task>();
for (var i = 0; i < 20; i++)
{
var task = new Task(Test);
tasks.Add(task);
}
foreach (var task in tasks)
task.Start();
await Task.WhenAll(tasks);
}
public static void Test()
{
Console.WriteLine($"Executing Test() at {DateTime.Now}");
Policy.Timeout(TimeSpan.FromSeconds(2), TimeoutStrategy.Pessimistic)
.Execute(() => { Thread.Sleep(200); });
}
}
}
我得到以下输出:
Executing Test() at 2018-05-16 15:10:21
Executing Test() at 2018-05-16 15:10:21
Executing Test() at 2018-05-16 15:10:21
Executing Test() at 2018-05-16 15:10:21
Executing Test() at 2018-05-16 15:10:21
Executing Test() at 2018-05-16 15:10:21
Executing Test() at 2018-05-16 15:10:21
Executing Test() at 2018-05-16 15:10:21
Executing Test() at 2018-05-16 15:10:22
Executing Test() at 2018-05-16 15:10:23
Executing Test() at 2018-05-16 15:10:24
Executing Test() at 2018-05-16 15:10:24
Executing Test() at 2018-05-16 15:10:24
Executing Test() at 2018-05-16 15:10:25
Executing Test() at 2018-05-16 15:10:26
Executing Test() at 2018-05-16 15:10:26
Executing Test() at 2018-05-16 15:10:27
Executing Test() at 2018-05-16 15:10:27
Executing Test() at 2018-05-16 15:10:27
Executing Test() at 2018-05-16 15:10:28
最后抛出 Polly.Timeout.TimeoutRejectedException。我真的不明白为什么要花这么长时间来执行?从输出看来,前 8 个任务并行执行,然后变得非常慢。如果没有超时策略或乐观超时策略,它会立即运行。但在我的情况下,只能使用悲观超时策略。
使用最新的 Polly 版本,即 6.0.1。