Skip to content

Commit 347bb2a

Browse files
committed
handle rate limiter
1 parent be96083 commit 347bb2a

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

MSStore.CLI/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using MSStore.CLI.Services.CredentialManager;
2626
using MSStore.CLI.Services.ElectronManager;
2727
using MSStore.CLI.Services.Graph;
28+
using MSStore.CLI.Services.Http;
2829
using MSStore.CLI.Services.PartnerCenter;
2930
using MSStore.CLI.Services.PWABuilder;
3031
using MSStore.CLI.Services.Telemetry;
@@ -117,7 +118,7 @@ public static async Task<int> Main(params string[] args)
117118
})
118119
.ConfigurePrimaryHttpMessageHandler(() =>
119120
{
120-
return new HttpClientHandler
121+
return new RetryAfterHttpHandler
121122
{
122123
CheckCertificateRevocationList = true
123124
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace MSStore.CLI.Services.Http
10+
{
11+
public sealed class RetryAfterHttpHandler : HttpClientHandler
12+
{
13+
/// <inheritdoc/>
14+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
15+
{
16+
do
17+
{
18+
var httpResponse = await base.SendAsync(request, cancellationToken);
19+
if (httpResponse.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
20+
{
21+
if (httpResponse.Headers.TryGetValues("Retry-After", out var values))
22+
{
23+
var retryAfter = values.FirstOrDefault();
24+
if (int.TryParse(retryAfter, out int delaySeconds))
25+
{
26+
await Task.Delay(delaySeconds * 1000, cancellationToken);
27+
continue;
28+
}
29+
}
30+
await Task.Delay(500, cancellationToken);
31+
continue;
32+
}
33+
else
34+
{
35+
return httpResponse;
36+
}
37+
}
38+
while (true);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)