-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEffectiveAsyncLoadWithSemaphoreslim.cs
225 lines (205 loc) · 5.95 KB
/
EffectiveAsyncLoadWithSemaphoreslim.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
namespace DummyConsole;
class EffectiveLoad
{
static async Task Main()
{
#region string[] list
string[] list = {
"www.youtube.com",
"www.facebook.com ",
"www.baidu.com ",
"www.yahoo.com ",
"www.amazon.com ",
"www.wikipedia.org ",
"www.qq.com ",
"www.google.co.in ",
"www.twitter.com ",
"www.live.com ",
"www.taobao.com ",
"www.bing.com ",
"www.instagram.com ",
"www.weibo.com ",
"www.sina.com.cn ",
"www.linkedin.com ",
"www.yahoo.co.jp ",
"www.msn.com ",
"www.vk.com ",
"www.google.de ",
"www.yandex.ru ",
"www.hao123.com ",
"www.google.co.uk ",
"www.reddit.com ",
"www.ebay.com ",
"www.google.fr ",
"www.t.co ",
"www.tmall.com ",
"www.google.com.br ",
"www.360.cn ",
"www.sohu.com ",
"www.amazon.co.jp ",
"www.pinterest.com ",
"www.netflix.com ",
"www.google.it ",
"www.google.ru ",
"www.microsoft.com ",
"www.google.es ",
"www.wordpress.com ",
"www.gmw.cn ",
"www.tumblr.com ",
"www.paypal.com ",
"www.blogspot.com ",
"www.imgur.com ",
"www.stackoverflow.com ",
"www.aliexpress.com ",
"www.naver.com ",
"www.ok.ru ",
"www.apple.com ",
"www.github.com ",
"www.chinadaily.com.cn ",
"www.imdb.com ",
"www.google.co.kr ",
"www.fc2.com ",
"www.jd.com ",
"www.blogger.com ",
"www.163.com ",
"www.google.ca ",
"www.whatsapp.com ",
"www.amazon.in ",
"www.office.com ",
"www.tianya.cn ",
"www.google.co.id ",
"www.youku.com ",
"www.rakuten.co.jp ",
"www.craigslist.org ",
"www.amazon.de ",
"www.nicovideo.jp ",
"www.google.pl ",
"www.soso.com ",
"www.bilibili.com ",
"www.dropbox.com ",
"www.xinhuanet.com ",
"www.outbrain.com ",
"www.pixnet.net ",
"www.alibaba.com ",
"www.alipay.com ",
"www.microsoftonline.com ",
"www.booking.com ",
"www.googleusercontent.com",
"www.google.com.au ",
"www.popads.net ",
"www.cntv.cn ",
"www.zhihu.com ",
"www.amazon.co.uk ",
"www.diply.com ",
"www.coccoc.com ",
"www.cnn.com ",
"www.bbc.co.uk ",
"www.twitch.tv ",
"www.wikia.com ",
"www.google.co.th ",
"www.go.com ",
"www.google.com.ph ",
"www.doubleclick.net ",
"www.onet.pl ",
"www.googleadservices.com ",
"www.accuweather.com ",
"www.googleweblight.com ",
"www.answers.yahoo.com "
};
list = list.Select(l => l.Trim()).ToArray();
list = list.Concat(list).ToArray();
#endregion
// pass a timeout accordingly
await PrintPingList(list);
}
public readonly struct NetworkInfo
{
public string Website { get; init; }
public IPAddress Address { get; init; }
public IPStatus Status { get; init; }
public long RoundTripTime { get; init; }
}
public static async Task PrintPingList(string[] sites, int timeout = -1)
{
using CancellationTokenSource source = new(timeout);
try
{
// PingAsync(sites,source.Token) is to cancel running tasks
// pingResultList.WithCancellation(source.Token) is to cancel IAsyncEnumerable's enumerator ==> IAsyncEnumerable <out T>.GetAsyncEnumerator(CancellationToken)
var pingResultList = PingAsync(sites, source.Token);
await Print(pingResultList, source.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation has been cancelled!");
// log it!
}
}
public static async Task Print(IAsyncEnumerable<NetworkInfo> pingResultList,
CancellationToken token = default)
{
await foreach (var pingResult in pingResultList
.WithCancellation(token).ConfigureAwait(false))
{
Console.WriteLine($"Website:{pingResult.Website}\n"
+ $"Address:{pingResult.Address}\nStatus:{pingResult.Status}\n"
+ $"RoundTripTime: {pingResult.RoundTripTime}");
Console.WriteLine();
// catch the cancellation quicker
if (token.IsCancellationRequested)
throw new OperationCanceledException();
}
}
public static async IAsyncEnumerable<NetworkInfo> PingAsync(string[] websites, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var length = websites.Length;
var taskNetworkInfo = new Queue<Task<NetworkInfo>>();
// allow 20 worker thread in at a time, this will prevent the thread exhaustion.
using (var semaphore_20 = new SemaphoreSlim(20))
{
for (int i = 0; i < length; i++)
{
var k = i;
NetworkInfo info = new();
await semaphore_20.WaitAsync(cancellationToken);
taskNetworkInfo.Enqueue(Task.Run(async () =>
{
Ping ping = new();
try
{
var pingResult = await ping.SendPingAsync(websites[k], 500);
info = new NetworkInfo
{
Address = pingResult.Address,
Website = websites[k],
RoundTripTime = pingResult.RoundtripTime,
Status = pingResult.Status
};
}
catch { /* log the errors */}
finally
{
ping.Dispose();
}
return info;
}, cancellationToken));
// flush 10 items to IAsyncEnumerable at a time
// other 10 items in queue will be completed in the meantime
// this will create a nice throughput.
int qFlush = (i + 1) % 10;
if (qFlush == 0 || i >= length - 10)
{
while (taskNetworkInfo.TryDequeue(out var networkInfo))
{
if (!cancellationToken.IsCancellationRequested)
semaphore_20.Release();
yield return await networkInfo;
}
}
}
}
}
}