Added button and manager for in game bug reports (Part 1) (#35350)
* Added the button and manager * Minor cleanup * Reigstered to the wrong thing! * Unload UI * Address the review * First commit :) * Some cleanup * Added some comments and now the placehoder text goes away once you start typing * Some cleanup and better test command * Basic rate limiter class (Not finished) * Cleanup * Removed forgotten comment xD * Whitespace removal * Minor cleanup, cvar hours -> minutes * More minor tweaks * Don't cache timer and add examples to fields * Added CCvar for time between bug reports * Minor crash when restarting rounds fixed * It compiled on my computer! * Fix comment indents * Remove unecessary async, removed magic strings, simplfied sawmill to not use post inject * Make struct private * Simplfiy TryGetLongHeader * Changed list to enumerable * URI cleanup * Got rid of the queue, used a much better way! * Made the comments a little better and fix some issues with them * Added header consts * Maximum reports per round is now an error message * Time between reports is now in seconds * Change ordering * Change hotkey to O * only update window when its open * Split up validation * address review * Address a few issues * inheritance fix * API now doesn't keep track of requests, just uses the rate limited response from github * Rough idea of how channels would work * refactor: reorganized code, placed rate limiter into http-client-handler AND manager (usually only manager-one should work) * cleanup * Add user agent so api doesn't get mad * Better error logs * Cleanup * It now throws! * refactor: renaming, moved some methods, xml-doc cleanups * refactor: BugReportWindow formatted to convention, enforced 1 updates only 1 per sec * Add very basic licence info * Fixed the issues! * Set ccvar default to false * make the button better * fix test fail silly me * Adress the review! * refactor: cleanup of entry point code, binding server-side code with client-facing manager * Resolve the other issues and cleanup and stuff smile :) * not entity * fixes * Cleanup * Cleanup * forgor region * fixes * Split up function and more stuff * Better unsubs yaygit add -A * I pray... * Revert "I pray..." This reverts commit 9629fb4f1289c9009a03e4e4facd9ae975e6303e. * I think I have to add it in the pr * Revert "I think I have to add it in the pr" This reverts commit e185b42f570fe5f0f51e0e44761d7938e22e67f7. * Tweaks * Minor tweak to permissions --------- Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
This commit is contained in:
100
Content.Server/Github/RetryHttpHandler.cs
Normal file
100
Content.Server/Github/RetryHttpHandler.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Net;
|
||||
|
||||
namespace Content.Server.Github;
|
||||
|
||||
/// <summary>
|
||||
/// Basic rate limiter for the GitHub api! Will ensure there is only ever one outgoing request at a time and all
|
||||
/// requests respect the rate limit the best they can.
|
||||
/// <br/>
|
||||
/// <br/> Links to the api for more information:
|
||||
/// <br/> <see href="https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api?apiVersion=2022-11-28">Best practices</see>
|
||||
/// <br/> <see href="https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28">Rate limit information</see>
|
||||
/// </summary>
|
||||
/// <remarks> This was designed for the 2022-11-28 version of the API. </remarks>
|
||||
public sealed class RetryHandler(HttpMessageHandler innerHandler, int maxRetries, ISawmill sawmill) : DelegatingHandler(innerHandler)
|
||||
{
|
||||
private const int MaxWaitSeconds = 32;
|
||||
|
||||
/// Extra buffer time (In seconds) after getting rate limited we don't make the request exactly when we get more credits.
|
||||
private const long ExtraBufferTime = 1L;
|
||||
|
||||
#region Headers
|
||||
|
||||
private const string RetryAfterHeader = "retry-after";
|
||||
|
||||
private const string RemainingHeader = "x-ratelimit-remaining";
|
||||
private const string RateLimitResetHeader = "x-ratelimit-reset";
|
||||
|
||||
#endregion
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(
|
||||
HttpRequestMessage request,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
HttpResponseMessage response;
|
||||
var i = 0;
|
||||
do
|
||||
{
|
||||
response = await base.SendAsync(request, cancellationToken);
|
||||
if (response.IsSuccessStatusCode)
|
||||
return response;
|
||||
|
||||
i++;
|
||||
if (i < maxRetries)
|
||||
{
|
||||
var waitTime = CalculateNextRequestTime(response, i);
|
||||
await Task.Delay(waitTime, cancellationToken);
|
||||
}
|
||||
} while (!response.IsSuccessStatusCode && i < maxRetries);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Follows these guidelines but also has a small buffer so you should never quite hit zero:
|
||||
/// <br/>
|
||||
/// <see href="https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api?apiVersion=2022-11-28#handle-rate-limit-errors-appropriately"/>
|
||||
/// </summary>
|
||||
/// <param name="response">The last response from the API.</param>
|
||||
/// <param name="attempt">Number of current call attempt.</param>
|
||||
/// <returns>The amount of time to wait until the next request.</returns>
|
||||
private TimeSpan CalculateNextRequestTime(HttpResponseMessage response, int attempt)
|
||||
{
|
||||
var headers = response.Headers;
|
||||
var statusCode = response.StatusCode;
|
||||
|
||||
// Specific checks for rate limits.
|
||||
if (statusCode is HttpStatusCode.Forbidden or HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
// Retry after header
|
||||
if (GithubClient.TryGetHeaderAsLong(headers, RetryAfterHeader, out var retryAfterSeconds))
|
||||
return TimeSpan.FromSeconds(retryAfterSeconds.Value + ExtraBufferTime);
|
||||
|
||||
// Reset header (Tells us when we get more api credits)
|
||||
if (GithubClient.TryGetHeaderAsLong(headers, RemainingHeader, out var remainingRequests)
|
||||
&& GithubClient.TryGetHeaderAsLong(headers, RateLimitResetHeader, out var resetTime)
|
||||
&& remainingRequests == 0)
|
||||
{
|
||||
var delayTime = resetTime.Value - DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
sawmill.Warning(
|
||||
"github returned '{status}' status, have to wait until limit reset - in '{delay}' seconds",
|
||||
response.StatusCode,
|
||||
delayTime
|
||||
);
|
||||
return TimeSpan.FromSeconds(delayTime + ExtraBufferTime);
|
||||
}
|
||||
}
|
||||
|
||||
// If the status code is not the expected one or the rate limit checks are failing, just do an exponential backoff.
|
||||
return ExponentialBackoff(attempt);
|
||||
}
|
||||
|
||||
private static TimeSpan ExponentialBackoff(int i)
|
||||
{
|
||||
return TimeSpan.FromSeconds(Math.Min(MaxWaitSeconds, Math.Pow(2, i)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user