This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
/
GitHubManager.cs
79 lines (72 loc) · 2.71 KB
/
GitHubManager.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Octokit;
using Polly;
using Polly.Retry;
using Statiq.Common;
namespace DiscoverDotnet
{
// Queues GitHub requests and ensures they're executed sequentially so we don't trigger the abuse detection mechanisms:
// "Make requests for a single user or client ID serially. Do not make requests for a single user or client ID concurrently."
// https://developer.github.com/v3/guides/best-practices-for-integrators/#dealing-with-abuse-rate-limits
public class GitHubManager
{
private const int MaxRetry = 3;
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
private readonly GitHubClient _gitHub;
public static readonly HashSet<string> MicrosoftOwners = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"dotnet",
"aspnet",
"microsoft",
"nuget",
"mono",
"azure"
};
public GitHubManager(ISettings settings)
{
_gitHub = new GitHubClient(new ProductHeaderValue(nameof(DiscoverDotnet)))
{
Credentials = new Credentials(settings.GetString("GITHUB_TOKEN"))
};
}
public async Task<TResult> GetAsync<TResult>(Func<GitHubClient, Task<TResult>> func, IExecutionContext context, bool retry = true)
{
AsyncRetryPolicy<TResult> retryPolicy = null;
if (retry)
{
retryPolicy = Policy<TResult>
.Handle<ApiException>()
.WaitAndRetryAsync(MaxRetry, attempt =>
{
context.LogInformation($"GitHub retry {attempt}");
return TimeSpan.FromSeconds(1 * Math.Pow(2, attempt));
});
}
await _semaphore.WaitAsync();
try
{
TResult result = retry
? await retryPolicy.ExecuteAsync(async _ => await func(_gitHub), context.CancellationToken)
: await func(_gitHub);
try
{
MiscellaneousRateLimit rateLimit = await _gitHub.Miscellaneous.GetRateLimits();
context.LogInformation($"GitHub rate limit: {rateLimit.Resources.Core.Remaining} remaining");
}
catch (Exception)
{
// Eat exceptions when getting rate limits
}
return result;
}
finally
{
_semaphore.Release();
}
}
}
}