Skip to content

Commit

Permalink
Updates ratelimit implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
austinv11 committed Sep 23, 2016
1 parent a10a532 commit 725efe3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
22 changes: 12 additions & 10 deletions src/main/java/sx/blah/discord/api/internal/Requests.java
Expand Up @@ -97,7 +97,7 @@ public final class Request {
/**
* Keeps track of per-method rate limits. Pair is method, path
*/
private final Map<Pair<String, String>, Long> retryAfters = new ConcurrentHashMap<>();
private final Map<String, Long> retryAfters = new ConcurrentHashMap<>();

private Request(Class<? extends HttpUriRequest> clazz, IDiscordClient client) {
this.requestClass = clazz;
Expand Down Expand Up @@ -177,15 +177,17 @@ private String request(HttpUriRequest request) throws DiscordException, RateLimi
globalRetryAfter.get() - System.currentTimeMillis(), request.getMethod(), true);
}

Pair<String, String> methodRequestPair = Pair.of(request.getMethod(), request.getURI().getPath());
String route = request.getURI().getPath();
if (route.contains("?"))
route = route.substring(0, route.lastIndexOf("?")); //Strips query parameters

if (retryAfters.containsKey(methodRequestPair)) {
if (System.currentTimeMillis() > retryAfters.get(methodRequestPair))
retryAfters.remove(methodRequestPair);
if (retryAfters.containsKey(route)) {
if (System.currentTimeMillis() > retryAfters.get(route))
retryAfters.remove(route);
else
throw new RateLimitException("Rate limit exceeded.",
retryAfters.get(methodRequestPair) - System.currentTimeMillis(),
String.format("%s %s", methodRequestPair.getLeft(), methodRequestPair.getRight()), false);
retryAfters.get(route) - System.currentTimeMillis(),
route, false);
}

try (CloseableHttpResponse response = CLIENT.execute(request)) {
Expand All @@ -194,7 +196,7 @@ private String request(HttpUriRequest request) throws DiscordException, RateLimi
if (response.containsHeader("X-RateLimit-Remaining")) {
int remaining = Integer.parseInt(response.getFirstHeader("X-RateLimit-Remaining").getValue());
if (remaining == 0) {
retryAfters.put(methodRequestPair,
retryAfters.put(route,
Long.parseLong(response.getFirstHeader("X-RateLimit-Reset").getValue())*1000);
}
}
Expand Down Expand Up @@ -238,11 +240,11 @@ private String request(HttpUriRequest request) throws DiscordException, RateLimi
if (rateLimitResponse.global) {
globalRetryAfter.set(System.currentTimeMillis()+rateLimitResponse.retry_after);
} else {
retryAfters.put(methodRequestPair, System.currentTimeMillis()+rateLimitResponse.retry_after);
retryAfters.put(route, System.currentTimeMillis()+rateLimitResponse.retry_after);
}

throw new RateLimitException(rateLimitResponse.message, rateLimitResponse.retry_after,
String.format("%s %s", methodRequestPair.getLeft(), methodRequestPair.getRight()),
route,
rateLimitResponse.global);
}

Expand Down
26 changes: 19 additions & 7 deletions src/main/java/sx/blah/discord/util/RateLimitException.java
Expand Up @@ -7,12 +7,13 @@
public class RateLimitException extends Exception {

private final long retryAfter;
private final String method;
private final String route;
private final boolean global;

public RateLimitException(String message, long retryAfter, String method, boolean global) {
super(message);
this.retryAfter = retryAfter;
this.method = method;
this.route = method;
this.global = global;
}

Expand All @@ -33,20 +34,31 @@ public long getRetryDelay() {
*/
@Deprecated
public String getBucket() {
return method;
return route;
}

/**
* Gets the method this rate limit was in response to.
* Gets the route this rate limit was in response to.
*
* @return The method.
* @return The route.
* @deprecated See {@link #getRoute()}
*/
@Deprecated
public String getMethod() {
return method;
return route;
}

/**
* Gets the route this rate limit was in response to.
*
* @return The route.
*/
public String getRoute() {
return route;
}

/**
* Gets whether this is a global rate limit or limited to a particular method.
* Gets whether this is a global rate limit or limited to a particular route.
*
* @return True if global or false if otherwise.
*/
Expand Down

0 comments on commit 725efe3

Please sign in to comment.