UUID conversion reliability improvement.

From bug reports, seems the max batch size might have been changed by
Mojang.  Can't find any docs, so the code now starts with 100 per batch
and goes smaller with the first batch until it succeeds.
This commit is contained in:
ryanhamshire 2015-01-08 19:40:08 -08:00
parent 54075027f1
commit ecd0664d66

View File

@ -16,7 +16,7 @@ import java.nio.ByteBuffer;
import java.util.*; import java.util.*;
class UUIDFetcher { class UUIDFetcher {
private static final double PROFILES_PER_REQUEST = 100; private static int PROFILES_PER_REQUEST = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private final JSONParser jsonParser = new JSONParser(); private final JSONParser jsonParser = new JSONParser();
private final List<String> names; private final List<String> names;
@ -90,15 +90,14 @@ class UUIDFetcher {
{ {
GriefPrevention.AddLogEntry("Calling Mojang to get UUIDs for remaining unresolved players (this is the slowest step)..."); GriefPrevention.AddLogEntry("Calling Mojang to get UUIDs for remaining unresolved players (this is the slowest step)...");
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); for (int i = 0; i * PROFILES_PER_REQUEST < names.size(); i++)
for (int i = 0; i < requests; i++)
{ {
boolean retry = false; boolean retry = false;
JSONArray array = null; JSONArray array = null;
do do
{ {
HttpURLConnection connection = createConnection(); HttpURLConnection connection = createConnection();
String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); String body = JSONArray.toJSONString(names.subList(i * PROFILES_PER_REQUEST, Math.min((i + 1) * PROFILES_PER_REQUEST, names.size())));
writeBody(connection, body); writeBody(connection, body);
retry = false; retry = false;
array = null; array = null;
@ -112,9 +111,23 @@ class UUIDFetcher {
if(e.getMessage().contains("429")) if(e.getMessage().contains("429"))
{ {
retry = true; retry = true;
//if this is the first time we're sending anything, the batch size must be too big
//try reducing it
if(i == 0 && PROFILES_PER_REQUEST > 1)
{
GriefPrevention.AddLogEntry("Batch size " + PROFILES_PER_REQUEST + " seems too large. Looking for a workable batch size...");
PROFILES_PER_REQUEST = Math.max(PROFILES_PER_REQUEST - 5, 1);
}
//otherwise, keep the batch size which has worked for previous iterations
//but wait a little while before trying again.
else
{
GriefPrevention.AddLogEntry("Mojang says we're sending requests too fast. Will retry every 30 seconds until we succeed..."); GriefPrevention.AddLogEntry("Mojang says we're sending requests too fast. Will retry every 30 seconds until we succeed...");
Thread.sleep(30000); Thread.sleep(30000);
} }
}
else else
{ {
throw e; throw e;
@ -131,7 +144,7 @@ class UUIDFetcher {
lookupCache.put(name, uuid); lookupCache.put(name, uuid);
lookupCache.put(name.toLowerCase(), uuid); lookupCache.put(name.toLowerCase(), uuid);
} }
if (rateLimiting && i != requests - 1) { if (rateLimiting) {
Thread.sleep(200L); Thread.sleep(200L);
} }
} }