|
| 1 | +using System; |
| 2 | +using System.CommandLine; |
| 3 | +using System.CommandLine.Builder; |
| 4 | +using System.CommandLine.Hosting; |
| 5 | +using System.CommandLine.Invocation; |
| 6 | +using System.CommandLine.Parsing; |
| 7 | +using System.Linq; |
| 8 | +using System.Net.Http.Headers; |
| 9 | +using System.Threading; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +using Microsoft.Extensions.DependencyInjection; |
| 13 | +using Microsoft.Extensions.Hosting; |
| 14 | +using Microsoft.Extensions.Logging; |
| 15 | + |
| 16 | +using Octokit; |
| 17 | +using Octokit.DependencyInjection; |
| 18 | + |
| 19 | +namespace THNETII.Octokit.RateLimitDrain |
| 20 | +{ |
| 21 | + public static class Program |
| 22 | + { |
| 23 | + public static Task<int> Main(string[] args) |
| 24 | + { |
| 25 | + var cmdRoot = new RootCommand() |
| 26 | + { |
| 27 | + Handler = CommandHandler.Create |
| 28 | + (async (IHost host, CancellationToken cancelToken) => |
| 29 | + { |
| 30 | + var provider = host.Services; |
| 31 | + var logger = provider.GetRequiredService<ILoggerFactory>() |
| 32 | + .CreateLogger(typeof(Program)); |
| 33 | + var client = provider.GetRequiredService<IGitHubClient>(); |
| 34 | + client.SetRequestTimeout(Timeout.InfiniteTimeSpan); |
| 35 | + |
| 36 | + while (!cancelToken.IsCancellationRequested) |
| 37 | + { |
| 38 | + try |
| 39 | + { |
| 40 | + int remaining, limit; |
| 41 | + DateTimeOffset reset; |
| 42 | + |
| 43 | + try |
| 44 | + { |
| 45 | + var meta = await client.Miscellaneous.GetMetadata().ConfigureAwait(false); |
| 46 | + var apiInfo = client.GetLastApiInfo(); |
| 47 | + var rateLimitInfo = apiInfo.RateLimit; |
| 48 | + |
| 49 | + remaining = rateLimitInfo.Remaining; |
| 50 | + limit = rateLimitInfo.Limit; |
| 51 | + reset = rateLimitInfo.Reset; |
| 52 | + |
| 53 | + logger.LogInformation($"Received Response, rate limit info: {{{nameof(rateLimitInfo.Remaining)}}} / {{{nameof(rateLimitInfo.Limit)}}} messages remaining. Next Rate-Limit window starts at: {{{nameof(rateLimitInfo.Reset)}}}", remaining, limit, reset); |
| 54 | + } |
| 55 | + catch (RateLimitExceededException rateLimitExcept) |
| 56 | + { |
| 57 | + |
| 58 | + foreach (var detail in rateLimitExcept.ApiError?.Errors ?? Enumerable.Empty<ApiErrorDetail>()) |
| 59 | + { |
| 60 | + logger.LogError(new EventId(rateLimitExcept.HResult, detail.Code), |
| 61 | + detail.Message); |
| 62 | + } |
| 63 | + |
| 64 | + remaining = rateLimitExcept.Remaining; |
| 65 | + limit = rateLimitExcept.Limit; |
| 66 | + reset = rateLimitExcept.Reset; |
| 67 | + |
| 68 | + logger.LogError(new EventId(rateLimitExcept.HResult, nameof(RateLimitExceededException)), |
| 69 | + rateLimitExcept, $"Received Response, rate limit info: {{{nameof(rateLimitExcept.Remaining)}}} / {{{nameof(rateLimitExcept.Limit)}}} messages remaining. Next Rate-Limit window starts at: {{{nameof(rateLimitExcept.Reset)}}}", |
| 70 | + remaining, limit, reset.ToLocalTime()); |
| 71 | + |
| 72 | + var timeToReset = rateLimitExcept.Reset - DateTimeOffset.Now; |
| 73 | + if (timeToReset > TimeSpan.Zero) |
| 74 | + { |
| 75 | + var delayTask = Task.Delay(timeToReset, cancelToken); |
| 76 | + logger.LogInformation($"Time to wait until next Rate-Limit window: {{{nameof(HttpResponseHeaders.RetryAfter)}}}", |
| 77 | + timeToReset); |
| 78 | + await delayTask.ConfigureAwait(false); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + logger.LogDebug("Time to next Rate-Limit windows is less than 0. Continuing with next request immediately."); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + catch (OperationCanceledException except) |
| 87 | + { |
| 88 | + logger.LogWarning(except, "Stopping program due to intercepted cancellation"); |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + }) |
| 93 | + }; |
| 94 | + var cmdParser = new CommandLineBuilder(cmdRoot) |
| 95 | + .UseDefaults() |
| 96 | + .UseHost(Host.CreateDefaultBuilder, host => |
| 97 | + { |
| 98 | + host.ConfigureServices(services => |
| 99 | + { |
| 100 | + services.AddGitHubClient(nameof(Octokit), github => github |
| 101 | + .UseAssemblyProductHeader(typeof(Program).Assembly) |
| 102 | + .UseHttpClientFactoryConnection() |
| 103 | + .AddClients() |
| 104 | + ); |
| 105 | + }); |
| 106 | + }) |
| 107 | + .Build(); |
| 108 | + |
| 109 | + return cmdParser.InvokeAsync(args); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments