/
BotRateLimiter.java
424 lines (388 loc) · 14.9 KB
/
BotRateLimiter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
* Copyright 2015-2019 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.internal.requests.ratelimit;
import net.dv8tion.jda.api.events.ExceptionEvent;
import net.dv8tion.jda.api.requests.Request;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.requests.RateLimiter;
import net.dv8tion.jda.internal.requests.Requester;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.requests.Route.RateLimit;
import okhttp3.Headers;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
public class BotRateLimiter extends RateLimiter
{
private static final String RESET_HEADER = "X-RateLimit-Reset";
private static final String LIMIT_HEADER = "X-RateLimit-Limit";
private static final String REMAINING_HEADER = "X-RateLimit-Remaining";
protected volatile Long timeOffset = null;
public BotRateLimiter(Requester requester)
{
super(requester);
}
@Override
public Long getRateLimit(Route.CompiledRoute route)
{
Bucket bucket = getBucket(route);
synchronized (bucket)
{
return bucket.getRateLimit();
}
}
@Override
protected void queueRequest(Request request)
{
Bucket bucket = getBucket(request.getRoute());
synchronized (bucket)
{
bucket.addToQueue(request);
}
}
@Override
protected Long handleResponse(Route.CompiledRoute route, okhttp3.Response response)
{
Bucket bucket = getBucket(route);
synchronized (bucket)
{
Headers headers = response.headers();
int code = response.code();
if (timeOffset == null)
setTimeOffset(headers);
if (code == 429)
{
String global = headers.get("X-RateLimit-Global");
String retry = headers.get("Retry-After");
if (retry == null || retry.isEmpty())
{
try (InputStream in = Requester.getBody(response))
{
DataObject limitObj = DataObject.fromJson(in);
retry = limitObj.get("retry_after").toString();
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
}
long retryAfter = Long.parseLong(retry);
if (Boolean.parseBoolean(global)) //global ratelimit
{
//If it is global, lock down the threads.
log.warn("Encountered global rate-limit! Retry-After: {}", retryAfter);
requester.getJDA().getSessionController().setGlobalRatelimit(getNow() + retryAfter);
}
else
{
log.warn("Encountered 429 on route /{}", bucket.getRoute());
updateBucket(bucket, headers, retryAfter);
}
return retryAfter;
}
else
{
updateBucket(bucket, headers, -1);
return null;
}
}
}
private Bucket getBucket(Route.CompiledRoute route)
{
String rateLimitRoute = route.getRatelimitRoute();
Bucket bucket = (Bucket) buckets.get(rateLimitRoute);
if (bucket == null)
{
synchronized (buckets)
{
bucket = (Bucket) buckets.get(rateLimitRoute);
if (bucket == null)
{
Route baseRoute = route.getBaseRoute();
bucket = new Bucket(rateLimitRoute, baseRoute.getRatelimit(), baseRoute.isMissingHeaders());
buckets.put(rateLimitRoute, bucket);
}
}
}
return bucket;
}
public long getNow()
{
return System.currentTimeMillis() + getTimeOffset();
}
public long getTimeOffset()
{
return timeOffset == null ? 0 : timeOffset;
}
private void setTimeOffset(Headers headers)
{
//Store as soon as possible to get the most accurate time difference;
long time = System.currentTimeMillis();
if (timeOffset == null)
{
//Get the date header provided by Discord.
//Format: "date" : "Fri, 16 Sep 2016 05:49:36 GMT"
String date = headers.get("Date");
if (date != null)
{
OffsetDateTime tDate = OffsetDateTime.parse(date, DateTimeFormatter.RFC_1123_DATE_TIME);
long lDate = tDate.toInstant().toEpochMilli(); //We want to work in milliseconds, not seconds
timeOffset = lDate - time; //Get offset in milliseconds.
}
}
}
private void updateBucket(Bucket bucket, Headers headers, long retryAfter)
{
int headerCount = 0;
if (retryAfter > 0)
{
bucket.resetTime = getNow() + retryAfter;
bucket.routeUsageRemaining = 0;
}
if (bucket.hasRatelimit()) // Check if there's a hardcoded rate limit
{
bucket.resetTime = getNow() + bucket.getRatelimit().getResetTime();
headerCount += 2;
//routeUsageLimit provided by the ratelimit object already in the bucket.
}
else
{
headerCount += parseLong(headers.get(RESET_HEADER), bucket, (time, b) -> b.resetTime = time * 1000); //Seconds to milliseconds
headerCount += parseInt(headers.get(LIMIT_HEADER), bucket, (limit, b) -> b.routeUsageLimit = limit);
}
//Currently, we check the remaining amount even for hardcoded ratelimits just to further respect Discord
// however, if there should ever be a case where Discord informs that the remaining is less than what
// it actually is and we add a custom ratelimit to handle that, we will need to instead move this to the
// above else statement and add a bucket.routeUsageRemaining-- decrement to the above if body.
//An example of this statement needing to be moved would be if the custom ratelimit reset time interval is
// equal to or greater than 1000ms, and the remaining count provided by discord is less than the ACTUAL
// amount that their systems allow in such a way that isn't a bug.
//The custom ratelimit system is primarily for ratelimits that can't be properly represented by Discord's
// header system due to their headers only supporting accuracy to the second. The custom ratelimit system
// allows for hardcoded ratelimits that allow accuracy to the millisecond which is important for some
// ratelimits like Reactions which is 1/0.25s, but discord reports the ratelimit as 1/1s with headers.
headerCount += parseInt(headers.get(REMAINING_HEADER), bucket, (remaining, b) -> b.routeUsageRemaining = remaining);
if (!bucket.missingHeaders && headerCount < 3)
{
log.debug("Encountered issue with headers when updating a bucket\n" +
"Route: {}\nHeaders: {}", bucket.getRoute(), headers);
}
}
private int parseInt(String input, Bucket bucket, IntObjectConsumer<? super Bucket> consumer)
{
try
{
int parsed = Integer.parseInt(input);
consumer.accept(parsed, bucket);
return 1;
}
catch (NumberFormatException ignored) {}
return 0;
}
private int parseLong(String input, Bucket bucket, LongObjectConsumer<? super Bucket> consumer)
{
try
{
long parsed = Long.parseLong(input);
consumer.accept(parsed, bucket);
return 1;
}
catch (NumberFormatException ignored) {}
return 0;
}
private class Bucket implements IBucket, Runnable
{
final String route;
final boolean missingHeaders;
final RateLimit rateLimit;
final ConcurrentLinkedQueue<Request> requests = new ConcurrentLinkedQueue<>();
volatile long resetTime = 0;
volatile int routeUsageRemaining = 1; //These are default values to only allow 1 request until we have properly
volatile int routeUsageLimit = 1; // ratelimit information.
public Bucket(String route, RateLimit rateLimit, boolean missingHeaders)
{
this.route = route;
this.rateLimit = rateLimit;
this.missingHeaders = missingHeaders;
if (rateLimit != null)
{
this.routeUsageRemaining = rateLimit.getUsageLimit();
this.routeUsageLimit = rateLimit.getUsageLimit();
}
}
void addToQueue(Request request)
{
requests.add(request);
submitForProcessing();
}
void submitForProcessing()
{
synchronized (submittedBuckets)
{
if (!submittedBuckets.contains(this))
{
Long delay = getRateLimit();
if (delay == null)
delay = 0L;
if (delay > 0)
{
log.debug("Backing off {} milliseconds on route /{}", delay, getRoute());
requester.getJDA().getRateLimitPool().schedule(this, delay, TimeUnit.MILLISECONDS);
}
else
{
requester.getJDA().getRateLimitPool().execute(this);
}
submittedBuckets.add(this);
}
}
}
Long getRateLimit()
{
long gCooldown = requester.getJDA().getSessionController().getGlobalRatelimit();
if (gCooldown > 0) //Are we on global cooldown?
{
long now = getNow();
if (now > gCooldown) //Verify that we should still be on cooldown.
{
//If we are done cooling down, reset the globalCooldown and continue.
requester.getJDA().getSessionController().setGlobalRatelimit(Long.MIN_VALUE);
}
else
{
//If we should still be on cooldown, return when we can go again.
return gCooldown - now;
}
}
if (this.routeUsageRemaining <= 0)
{
if (getNow() > this.resetTime)
{
this.routeUsageRemaining = this.routeUsageLimit;
this.resetTime = 0;
}
}
if (this.routeUsageRemaining > 0)
return null;
else
return this.resetTime - getNow();
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof Bucket))
return false;
Bucket oBucket = (Bucket) o;
return route.equals(oBucket.route);
}
@Override
public int hashCode()
{
return route.hashCode();
}
@Override
public void run()
{
requester.setContext();
try
{
synchronized (requests)
{
for (Iterator<Request> it = requests.iterator(); it.hasNext(); )
{
Long limit = getRateLimit();
if (limit != null && limit > 0)
break; // possible global cooldown here
Request request = null;
try
{
request = it.next();
if (isSkipped(it, request))
continue;
Long retryAfter = requester.execute(request);
if (retryAfter != null)
break;
else
it.remove();
}
catch (Throwable t)
{
log.error("Requester system encountered an internal error", t);
it.remove();
if (request != null)
request.onFailure(t);
}
}
synchronized (submittedBuckets)
{
submittedBuckets.remove(this);
if (!requests.isEmpty())
{
try
{
this.submitForProcessing();
}
catch (RejectedExecutionException e)
{
log.debug("Caught RejectedExecutionException when re-queuing a ratelimited request. The requester is probably shutdown, thus, this can be ignored.");
}
}
}
}
}
catch (Throwable err)
{
log.error("Requester system encountered an internal error from beyond the synchronized execution blocks. NOT GOOD!", err);
if (err instanceof Error)
{
JDAImpl api = requester.getJDA();
api.handleEvent(new ExceptionEvent(api, err, true));
}
}
}
@Override
public RateLimit getRatelimit()
{
return rateLimit;
}
@Override
public String getRoute()
{
return route;
}
@Override
public Queue<Request> getRequests()
{
return requests;
}
}
private interface LongObjectConsumer<T>
{
void accept(long n, T t);
}
private interface IntObjectConsumer<T>
{
void accept(int n, T t);
}
}