private async Task RunAsync()
        {
            while (_requests.TryDequeue(out var request))
            {
                using (request)
                {
                    if (LastRateLimit?.Remaining == 0)
                    {
                        var delay = request.RateLimitOverride != null
                            ? TimeSpan.FromMilliseconds(request.RateLimitOverride.Value)
                            : LastRateLimit.ResetsAfter;

                        if (delay > TimeSpan.Zero)
                        {
                            if (request.Options.MaximumRateLimitDuration != default && delay > request.Options.MaximumRateLimitDuration)
                            {
                                request.SetException(new DiscordHttpException((HttpStatusCode)429, null, "Rate-limit hit. Throwing due to Options.ThrowOnRateLimits."));
                                continue;
                            }

                            await Task.Delay(delay).ConfigureAwait(false);
                        }
                    }

                    try
                    {
                        LastRateLimit = await _rateLimiter.Client.HandleRequestAsync(request).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        request.SetException(ex);
                    }
                }
            }
        }
Exemple #2
0
        public async Task HandleRateLimitedAsync(RestRequest request, RateLimit rateLimit)
        {
            // TODO: proper rate-limit handling! (semaphore for global rates? / throw?)
            //       (also move to Discord's buckets)

            var severity = rateLimit.IsGlobal
                ? LogSeverity.Error
                : LogSeverity.Warning;
            var message = $"{(rateLimit.IsGlobal ? "Globally rate" : "Rate")} limited {request} for {rateLimit.ResetsAfter}";

            if (request.Options.MaximumRateLimitDuration != default && rateLimit.ResetsAfter > request.Options.MaximumRateLimitDuration)
            {
                request.SetException(new DiscordHttpException((HttpStatusCode)429, null, "Rate-limit hit. Throwing due to Options.ThrowOnRateLimits."));
                Client.Log(severity, $"{message} - will be throwing due to Options.ThrowOnRateLimits.");
                return;
            }

            Client.Log(severity, $"{message} - will be delaying.");
            await Task.Delay(rateLimit.ResetsAfter).ConfigureAwait(false);

            if (!rateLimit.IsGlobal)
            {
                request.Initialise(Client.Serializer);
                await Client.HandleRequestAsync(request).ConfigureAwait(false);
            }
        }
Exemple #3
0
 public async Task HandleRateLimitedAsync(RateLimit rateLimit)
 {
     // TODO
     Client.Log(rateLimit.IsGlobal
         ? LogMessageSeverity.Error
         : LogMessageSeverity.Warning,
                $"{(rateLimit.IsGlobal ? "Globally rate" : "Rate")} limited - will be delaying for {rateLimit.RetryAfter}.");
 }
Exemple #4
0
        private async Task RunAsync()
        {
            while (_requests.TryDequeue(out var request))
            {
                using (request)
                {
                    if (LastRateLimit?.Remaining == 0)
                    {
                        TimeSpan delay;
                        if (request.RateLimitOverride != null)
                        {
                            delay = TimeSpan.FromMilliseconds(request.RateLimitOverride.Value);
                        }
                        else
                        {
                            //delay = _rateLimit.ResetsAt.Value - _rateLimit.Date;
                            delay = TimeSpan.FromSeconds(LastRateLimit.ResetsAfter);
                        }

                        if (delay > TimeSpan.Zero)
                        {
                            await Task.Delay(delay).ConfigureAwait(false);
                        }
                    }

                    try
                    {
                        LastRateLimit = await _rateLimiter.Client.HandleRequestAsync(request).ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        request.SetException(ex);
                    }
                }
            }
        }