-
Notifications
You must be signed in to change notification settings - Fork 3
/
PowerfulAsyncEnumerable.cs
63 lines (54 loc) · 2 KB
/
PowerfulAsyncEnumerable.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using static PowerfulAsyncEnumerableExtensions;
class PowerfulAsyncEnumerable : IRunnable
{
public async Task Run()
{
var httpClient = new HttpClient();
using var tokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var packagesWithNetFxAsms = new List<PackageMetadata>();
var totalCount = 0;
var catalogPage = await this.ReadPage5000FromNuget(httpClient).ConfigureAwait(false);
await foreach (var package in this.ReadPackageMetadata(httpClient, catalogPage, tokenSource.Token)
.ConfigureAwait(false))
{
totalCount++;
if (package.HasNetAssemblies)
{
packagesWithNetFxAsms.Add(package);
Console.Write("!");
}
else
{
Console.Write("~");
}
}
this.PrintStatistics(packagesWithNetFxAsms, totalCount);
}
public async IAsyncEnumerable<PackageMetadata> ReadPackageMetadata(
HttpClient httpClient,
CatalogPage catalogPage,
[EnumeratorCancellation] CancellationToken cancellationToken = default,
int maxConcurrentHttpCalls = 10)
{
using var throttler = new SemaphoreSlim(maxConcurrentHttpCalls);
var tasks = new List<Task<PackageMetadata>>();
foreach (var item in catalogPage.Items.Where(p => p.Type == "nuget:PackageDetails"))
{
tasks.Add(this.GetMetadataForPackage(httpClient, item.Url, throttler, cancellationToken));
}
while (tasks.Count > 0)
{
var done = await Task.WhenAny(tasks).ConfigureAwait(false);
tasks.Remove(done);
yield return await done
.ConfigureAwait(false);
}
}
}