Browse Source

Fix crosspost ratelimits (#2100)

tags/3.3.1
Quin Lynch GitHub 3 years ago
parent
commit
fad217e8bc
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 2 deletions
  1. +21
    -0
      src/Discord.Net.Rest/API/Common/Ratelimit.cs
  2. +1
    -1
      src/Discord.Net.Rest/Net/DefaultRestClient.cs
  3. +1
    -0
      src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs
  4. +23
    -1
      src/Discord.Net.Rest/Net/RateLimitInfo.cs

+ 21
- 0
src/Discord.Net.Rest/API/Common/Ratelimit.cs View File

@@ -0,0 +1,21 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.API
{
internal class Ratelimit
{
[JsonProperty("global")]
public bool Global { get; set; }

[JsonProperty("message")]
public string Message { get; set; }

[JsonProperty("retry_after")]
public double RetryAfter { get; set; }
}
}

+ 1
- 1
src/Discord.Net.Rest/Net/DefaultRestClient.cs View File

@@ -149,7 +149,7 @@ namespace Discord.Net.Rest
HttpResponseMessage response = await _client.SendAsync(request, cancelToken).ConfigureAwait(false);

var headers = response.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault(), StringComparer.OrdinalIgnoreCase);
var stream = !headerOnly ? await response.Content.ReadAsStreamAsync().ConfigureAwait(false) : null;
var stream = (!headerOnly || !response.IsSuccessStatusCode) ? await response.Content.ReadAsStreamAsync().ConfigureAwait(false) : null;

return new RestResponse(response.StatusCode, headers, stream);
}


+ 1
- 0
src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs View File

@@ -75,6 +75,7 @@ namespace Discord.Net.Queue
switch (response.StatusCode)
{
case (HttpStatusCode)429:
info.ReadRatelimitPayload(response.Stream);
if (info.IsGlobal)
{
#if DEBUG_LIMITS


+ 23
- 1
src/Discord.Net.Rest/Net/RateLimitInfo.cs View File

@@ -1,6 +1,10 @@
using Discord.API;
using Discord.Net.Rest;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace Discord.Net
{
@@ -25,7 +29,7 @@ namespace Discord.Net
public DateTimeOffset? Reset { get; }

/// <inheritdoc/>
public TimeSpan? ResetAfter { get; }
public TimeSpan? ResetAfter { get; private set; }

/// <inheritdoc/>
public string Bucket { get; }
@@ -56,5 +60,23 @@ namespace Discord.Net
Lag = headers.TryGetValue("Date", out temp) &&
DateTimeOffset.TryParse(temp, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
}

internal void ReadRatelimitPayload(Stream response)
{
try
{
if (response != null && response.Length != 0)
{
using (TextReader text = new StreamReader(response))
using (JsonReader reader = new JsonTextReader(text))
{
var ratelimit = Discord.Rest.DiscordRestClient.Serializer.Deserialize<Ratelimit>(reader);

ResetAfter = TimeSpan.FromSeconds(ratelimit.RetryAfter);
}
}
}
catch { }
}
}
}

Loading…
Cancel
Save