Skip to content

Commit

Permalink
Add rate limit pausing when collecting stats
Browse files Browse the repository at this point in the history
Since we run for many ours, it might be we run out of requests.
  • Loading branch information
kzu committed Oct 1, 2024
1 parent 99a6554 commit dc15077
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Commands/CliGraphQueryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CliGraphQueryClient : IGraphQueryClient
// Legacy queries won't have anything in the "query" (a URL endpoint is expected), but can still paginate in the CLI.
// NOTE: this is an inconsistency with the HttpGraphQueryClient, which doesn't do pagination for legacy queries.
// TODO: perhaps we should implement that, but it's not needed right now.
// In Particular, we ARE levarging this inconsistency in our RepositoryContributors query.
(query.IsLegacy ||
(query.Query.Contains("$endCursor") && query.Query.Contains("first:")));

Expand Down
26 changes: 26 additions & 0 deletions src/Commands/NuGetStatsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ public override async Task<int> ExecuteAsync(CommandContext context, NuGetStatsS

using var withToken = GitHub.WithToken(token);
if (!string.IsNullOrEmpty(token) && withToken is null)
{
AnsiConsole.MarkupLine(":cross_mark: [yellow]Invalid GitHub token provided[/]");
return -1;
}

var repository = new SourceRepository(new PackageSource("https://api.nuget.org/v3/index.json"), Repository.Provider.GetCoreV3());
var resource = await repository.GetResourceAsync<PackageMetadataResource>();
Expand Down Expand Up @@ -376,11 +380,33 @@ await Parallel.ForEachAsync(tasks, paralell, async (source, cancellation) =>
if (!model.Repositories.ContainsKey(ownerRepo))
{
var contribs = await graph.QueryAsync(GraphQueries.RepositoryContributors(ownerRepo));
if (contribs?.Length == 0)
{
// Make sure we haven't exhausted the GH API rate limit
var rate = await graph.QueryAsync(GraphQueries.RateLimits);
if (rate is not { })
throw new InvalidOperationException("Failed to get rate limits for current token.");

if (rate.General.Remaining < 10 || rate.GraphQL.Remaining < 10)
{
var reset = DateTimeOffset.FromUnixTimeSeconds(Math.Min(rate.General.Reset, rate.GraphQL.Reset));
var wait = reset - DateTimeOffset.UtcNow;
task.Description = $":hourglass_not_done: [yellow]Rate limit exhausted, waiting {wait.Humanize()} until reset[/]";
await Task.Delay(wait, cancellation);
contribs = await graph.QueryAsync(GraphQueries.RepositoryContributors(ownerRepo));
}
}

if (contribs != null)
{
model.Repositories.TryAdd(ownerRepo, new(contribs));
}
else
{
// Might not be a GH repo at all, or perhaps it's just empty?
model.Repositories.TryAdd(ownerRepo, []);
AnsiConsole.MarkupLine($":warning: [yellow]{link}[/]: no contributors found for [white]{ownerRepo}[/]");
}
}

foreach (var author in model.Repositories[ownerRepo])
Expand Down
1 change: 0 additions & 1 deletion src/Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Text.Json;
using Devlooped;
Expand Down

0 comments on commit dc15077

Please sign in to comment.