2021-06-09 14:22:10 +00:00
|
|
|
using Serilog;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace Myriad.Gateway.Limit;
|
|
|
|
|
|
|
|
public class TwilightGatewayRatelimiter: IGatewayRatelimiter
|
2021-06-09 14:22:10 +00:00
|
|
|
{
|
2021-12-26 06:42:47 +00:00
|
|
|
private readonly HttpClient _httpClient = new() { Timeout = TimeSpan.FromSeconds(30) };
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly string _url;
|
2021-06-09 14:22:10 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public TwilightGatewayRatelimiter(ILogger logger, string url)
|
|
|
|
{
|
|
|
|
_url = url;
|
|
|
|
_logger = logger.ForContext<TwilightGatewayRatelimiter>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Identify(int shard)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
try
|
2021-08-12 06:59:48 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger.Information("Shard {ShardId}: Requesting identify at gateway queue {GatewayQueueUrl}",
|
|
|
|
shard, _url);
|
2021-12-24 03:02:45 +00:00
|
|
|
await _httpClient.GetAsync(_url + "?shard=" + shard);
|
2021-11-27 02:10:56 +00:00
|
|
|
return;
|
2021-08-12 06:59:48 +00:00
|
|
|
}
|
2021-12-24 03:02:45 +00:00
|
|
|
catch (TaskCanceledException)
|
|
|
|
{
|
|
|
|
_logger.Warning("Shard {ShardId}: Gateway queue timed out, retrying", shard);
|
|
|
|
}
|
2021-06-09 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|