-
Notifications
You must be signed in to change notification settings - Fork 2
/
HighLevel_Polly.cs
34 lines (30 loc) · 1.21 KB
/
HighLevel_Polly.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using Polly; //Policy.Bulkhead
namespace ThrottledParallelism.Strategies
{
public class HighLevel_Polly : IGovernedParallelDownloader
{
private static readonly HttpClient client = new HttpClient();
public Task DownloadThemAllAsync(IEnumerable<Uri> uris, ProcessResult processResult, byte maxThreads)
{
var throttler = Policy.BulkheadAsync(maxThreads, int.MaxValue); //!SPOT: maxQueuingActions param can cause BulkheadRejectedException
//Single Producer
//Waiting for all workers to finish
return Task.WhenAll(
uris.Select( //Multiple Workers
url => throttler.ExecuteAsync( //MaxDegreeOfParallelism
async () => await WorkerAsync(url, processResult)
)));
}
async Task WorkerAsync(Uri uri, ProcessResult processResult)
{
var content = await client.GetStringAsync(uri).ConfigureAwait(false);
processResult(Thread.CurrentThread.ManagedThreadId.ToString(), content);
}
}
}