Add a rate limit for mojang api requests. Configurable via hidden.yml

This commit is contained in:
t00thpick1 2014-08-12 11:51:34 -04:00
parent 0a066f51bb
commit a1be17c72c
3 changed files with 37 additions and 8 deletions

View File

@ -6,16 +6,18 @@ import com.gmail.nossr50.mcMMO;
public class HiddenConfig { public class HiddenConfig {
private static HiddenConfig instance; private static HiddenConfig instance;
private static String fileName; private String fileName;
private static YamlConfiguration config; private YamlConfiguration config;
private static boolean chunkletsEnabled; private boolean chunkletsEnabled;
private static int conversionRate; private int conversionRate;
private static boolean useEnchantmentBuffs; private boolean useEnchantmentBuffs;
private static boolean resendChunksAfterBlockAbility; private boolean resendChunksAfterBlockAbility;
private static int uuidConvertAmount; private int uuidConvertAmount;
private int mojangRateLimit;
private long mojangLimitPeriod;
public HiddenConfig(String fileName) { public HiddenConfig(String fileName) {
HiddenConfig.fileName = fileName; this.fileName = fileName;
load(); load();
} }
@ -35,6 +37,8 @@ public class HiddenConfig {
useEnchantmentBuffs = config.getBoolean("Options.EnchantmentBuffs", true); useEnchantmentBuffs = config.getBoolean("Options.EnchantmentBuffs", true);
resendChunksAfterBlockAbility = config.getBoolean("Options.RefreshChunks", false); resendChunksAfterBlockAbility = config.getBoolean("Options.RefreshChunks", false);
uuidConvertAmount = config.getInt("Options.UUIDConvertAmount", 5); uuidConvertAmount = config.getInt("Options.UUIDConvertAmount", 5);
mojangRateLimit = config.getInt("Options.MojangRateLimit", 50000);
mojangLimitPeriod = config.getLong("Options.MojangLimitPeriod", 600000);
} }
} }
@ -57,4 +61,12 @@ public class HiddenConfig {
public int getUUIDConvertAmount() { public int getUUIDConvertAmount() {
return uuidConvertAmount; return uuidConvertAmount;
} }
public int getMojangRateLimit() {
return mojangRateLimit;
}
public long getMojangLimitPeriod() {
return mojangLimitPeriod;
}
} }

View File

@ -18,6 +18,8 @@ import com.gmail.nossr50.util.uuid.UUIDFetcher;
public class UUIDUpdateAsyncTask extends BukkitRunnable { public class UUIDUpdateAsyncTask extends BukkitRunnable {
private mcMMO plugin; private mcMMO plugin;
private static final int MAX_LOOKUP = Math.max(HiddenConfig.getInstance().getUUIDConvertAmount(), 100); private static final int MAX_LOOKUP = Math.max(HiddenConfig.getInstance().getUUIDConvertAmount(), 100);
private static final int RATE_LIMIT = HiddenConfig.getInstance().getMojangRateLimit();
private static final long LIMIT_PERIOD = HiddenConfig.getInstance().getMojangLimitPeriod();
private static final int BATCH_SIZE = 5000; private static final int BATCH_SIZE = 5000;
private List<String> userNames; private List<String> userNames;
@ -43,6 +45,16 @@ public class UUIDUpdateAsyncTask extends BukkitRunnable {
Map<String, UUID> fetchedUUIDs = new HashMap<String, UUID>(); Map<String, UUID> fetchedUUIDs = new HashMap<String, UUID>();
while (size != 0) { while (size != 0) {
if (checkedUsers + 100 > RATE_LIMIT) {
try {
Thread.sleep(LIMIT_PERIOD);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
startMillis = System.currentTimeMillis();
checkedUsers = 0;
}
if (size > MAX_LOOKUP) { if (size > MAX_LOOKUP) {
userNamesSection = userNames.subList(size - MAX_LOOKUP, size); userNamesSection = userNames.subList(size - MAX_LOOKUP, size);
size -= MAX_LOOKUP; size -= MAX_LOOKUP;

View File

@ -14,3 +14,8 @@ Options:
# Amount of users to convert every interval # Amount of users to convert every interval
UUIDConvertAmount: 100 UUIDConvertAmount: 100
# Amount of users to be converted at a time before waiting MojangLimitPeriod milliseconds to begin again
# This setting is for large servers to avoid being temp banned from mojang api
MojangRateLimit: 50000
# Amount of time to wait after hitting the MojangRateLimit in UUID conversion
MojangLimitPeriod: 600000