Skip to content

Commit fad217e

Browse files
authored
Fix crosspost ratelimits (#2100)
1 parent 503d32a commit fad217e

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Discord.API
9+
{
10+
internal class Ratelimit
11+
{
12+
[JsonProperty("global")]
13+
public bool Global { get; set; }
14+
15+
[JsonProperty("message")]
16+
public string Message { get; set; }
17+
18+
[JsonProperty("retry_after")]
19+
public double RetryAfter { get; set; }
20+
}
21+
}

src/Discord.Net.Rest/Net/DefaultRestClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private async Task<RestResponse> SendInternalAsync(HttpRequestMessage request, C
149149
HttpResponseMessage response = await _client.SendAsync(request, cancelToken).ConfigureAwait(false);
150150

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

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

src/Discord.Net.Rest/Net/Queue/RequestQueueBucket.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public async Task<Stream> SendAsync(RestRequest request)
7575
switch (response.StatusCode)
7676
{
7777
case (HttpStatusCode)429:
78+
info.ReadRatelimitPayload(response.Stream);
7879
if (info.IsGlobal)
7980
{
8081
#if DEBUG_LIMITS

src/Discord.Net.Rest/Net/RateLimitInfo.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
using Discord.API;
2+
using Discord.Net.Rest;
3+
using Newtonsoft.Json;
14
using System;
25
using System.Collections.Generic;
36
using System.Globalization;
7+
using System.IO;
48

59
namespace Discord.Net
610
{
@@ -25,7 +29,7 @@ public struct RateLimitInfo : IRateLimitInfo
2529
public DateTimeOffset? Reset { get; }
2630

2731
/// <inheritdoc/>
28-
public TimeSpan? ResetAfter { get; }
32+
public TimeSpan? ResetAfter { get; private set; }
2933

3034
/// <inheritdoc/>
3135
public string Bucket { get; }
@@ -56,5 +60,23 @@ internal RateLimitInfo(Dictionary<string, string> headers, string endpoint)
5660
Lag = headers.TryGetValue("Date", out temp) &&
5761
DateTimeOffset.TryParse(temp, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
5862
}
63+
64+
internal void ReadRatelimitPayload(Stream response)
65+
{
66+
try
67+
{
68+
if (response != null && response.Length != 0)
69+
{
70+
using (TextReader text = new StreamReader(response))
71+
using (JsonReader reader = new JsonTextReader(text))
72+
{
73+
var ratelimit = Discord.Rest.DiscordRestClient.Serializer.Deserialize<Ratelimit>(reader);
74+
75+
ResetAfter = TimeSpan.FromSeconds(ratelimit.RetryAfter);
76+
}
77+
}
78+
}
79+
catch { }
80+
}
5981
}
6082
}

0 commit comments

Comments
 (0)