Skip to content

Commit

Permalink
Updated server error handling in Requester to account for 5xx errors …
Browse files Browse the repository at this point in the history
…rather than simply cloudflare html responses
  • Loading branch information
MinnDevelopment committed Mar 17, 2017
1 parent 7b0f527 commit 8ba048a
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/main/java/net/dv8tion/jda/core/requests/Requester.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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.
* <br>Check if globalCooldown is {@code null} to determine if it was Per-Route or Global.
*/
public Long execute(Request apiRequest)
{
Expand All @@ -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<String> response = request.asString();
int attempt = 1;
while (attempt < 4 && response.getStatus() != 429 && response.getBody() != null && response.getBody().startsWith("<"))
HttpResponse<String> 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;
}

Expand Down

0 comments on commit 8ba048a

Please sign in to comment.