From 8ba048a2ddc9be64ff1bc4800a23c6149f360598 Mon Sep 17 00:00:00 2001 From: MinnDevelopment Date: Fri, 17 Mar 2017 22:12:49 +0100 Subject: [PATCH] Updated server error handling in Requester to account for 5xx errors rather than simply cloudflare html responses --- .../dv8tion/jda/core/requests/Requester.java | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/dv8tion/jda/core/requests/Requester.java b/src/main/java/net/dv8tion/jda/core/requests/Requester.java index f812254c81..2748df89a1 100644 --- a/src/main/java/net/dv8tion/jda/core/requests/Requester.java +++ b/src/main/java/net/dv8tion/jda/core/requests/Requester.java @@ -30,11 +30,8 @@ import net.dv8tion.jda.core.requests.Route.CompiledRoute; import net.dv8tion.jda.core.requests.ratelimit.BotRateLimiter; import net.dv8tion.jda.core.requests.ratelimit.ClientRateLimiter; -import net.dv8tion.jda.core.requests.ratelimit.IBucket; import net.dv8tion.jda.core.utils.SimpleLog; -import java.util.List; - public class Requester { public static final SimpleLog LOG = SimpleLog.getLog("JDARequester"); @@ -83,12 +80,14 @@ public void request(Request apiRequest) } /** - * Used to execute an Request. Processes request related to provided bucket. + * Used to execute a Request. Processes request related to provided bucket. + * + * @param apiRequest + * The API request that needs to be sent * - * @param apiRequest The API request that needs to be sent - * @return Returns non-null if the request was ratelimited. Returns a Long containing retry_after milliseconds until - * the request can be made again. This could either be for the Per-Route ratelimit or the Global ratelimit. - * Check if globalCooldown is null to determine if it was Per-Route or Global. + * @return Non-null if the request was ratelimited. Returns a Long containing retry_after milliseconds until + * the request can be made again. This could either be for the Per-Route ratelimit or the Global ratelimit. + *
Check if globalCooldown is {@code null} to determine if it was Per-Route or Global. */ public Long execute(Request apiRequest) { @@ -115,33 +114,34 @@ public Long execute(Request apiRequest) try { - //If the request has been canceled via the Future, don't execute. - if (apiRequest.isCanceled()) - return null; - - HttpResponse response = request.asString(); - int attempt = 1; - while (attempt < 4 && response.getStatus() != 429 && response.getBody() != null && response.getBody().startsWith("<")) + HttpResponse response; + int attempt = 0; + do { - LOG.debug(String.format("Requesting %s -> %s returned HTML... retrying (attempt %d)", + //If the request has been canceled via the Future, don't execute. + if (apiRequest.isCanceled()) + return null; + response = request.asString(); + + if (response.getStatus() < 500) + break; + + attempt++; + LOG.debug(String.format("Requesting %s -> %s returned status %d... retrying (attempt %d)", request.getHttpRequest().getHttpMethod().name(), request.getHttpRequest().getUrl(), - attempt)); + response.getStatus(), attempt)); try { Thread.sleep(50 * attempt); } catch (InterruptedException ignored) {} - - //If the request has been canceled via the Future, don't execute. - if (apiRequest.isCanceled()) - return null; - response = request.asString(); - attempt++; } - if (response.getBody() != null && response.getBody().startsWith("<")) + while (attempt < 4 && response.getStatus() >= 500); + + if (response.getStatus() >= 500) { - //Epic failure due to cloudflare. Attempted 4 times. + //Epic failure from other end. Attempted 4 times. return null; }