From a1be17c72c1032cdff130d81a512dce3765c3e8a Mon Sep 17 00:00:00 2001 From: t00thpick1 Date: Tue, 12 Aug 2014 11:51:34 -0400 Subject: [PATCH] Add a rate limit for mojang api requests. Configurable via hidden.yml --- .../gmail/nossr50/config/HiddenConfig.java | 28 +++++++++++++------ .../database/UUIDUpdateAsyncTask.java | 12 ++++++++ src/main/resources/hidden.yml | 5 ++++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/HiddenConfig.java b/src/main/java/com/gmail/nossr50/config/HiddenConfig.java index 124aeb733..bd551e990 100644 --- a/src/main/java/com/gmail/nossr50/config/HiddenConfig.java +++ b/src/main/java/com/gmail/nossr50/config/HiddenConfig.java @@ -6,16 +6,18 @@ import com.gmail.nossr50.mcMMO; public class HiddenConfig { private static HiddenConfig instance; - private static String fileName; - private static YamlConfiguration config; - private static boolean chunkletsEnabled; - private static int conversionRate; - private static boolean useEnchantmentBuffs; - private static boolean resendChunksAfterBlockAbility; - private static int uuidConvertAmount; + private String fileName; + private YamlConfiguration config; + private boolean chunkletsEnabled; + private int conversionRate; + private boolean useEnchantmentBuffs; + private boolean resendChunksAfterBlockAbility; + private int uuidConvertAmount; + private int mojangRateLimit; + private long mojangLimitPeriod; public HiddenConfig(String fileName) { - HiddenConfig.fileName = fileName; + this.fileName = fileName; load(); } @@ -35,6 +37,8 @@ public class HiddenConfig { useEnchantmentBuffs = config.getBoolean("Options.EnchantmentBuffs", true); resendChunksAfterBlockAbility = config.getBoolean("Options.RefreshChunks", false); 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() { return uuidConvertAmount; } + + public int getMojangRateLimit() { + return mojangRateLimit; + } + + public long getMojangLimitPeriod() { + return mojangLimitPeriod; + } } diff --git a/src/main/java/com/gmail/nossr50/runnables/database/UUIDUpdateAsyncTask.java b/src/main/java/com/gmail/nossr50/runnables/database/UUIDUpdateAsyncTask.java index 78a44dd7b..54504e8c6 100644 --- a/src/main/java/com/gmail/nossr50/runnables/database/UUIDUpdateAsyncTask.java +++ b/src/main/java/com/gmail/nossr50/runnables/database/UUIDUpdateAsyncTask.java @@ -18,6 +18,8 @@ import com.gmail.nossr50.util.uuid.UUIDFetcher; public class UUIDUpdateAsyncTask extends BukkitRunnable { private mcMMO plugin; 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 List userNames; @@ -43,6 +45,16 @@ public class UUIDUpdateAsyncTask extends BukkitRunnable { Map fetchedUUIDs = new HashMap(); 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) { userNamesSection = userNames.subList(size - MAX_LOOKUP, size); size -= MAX_LOOKUP; diff --git a/src/main/resources/hidden.yml b/src/main/resources/hidden.yml index fcc0a6707..c158e8122 100644 --- a/src/main/resources/hidden.yml +++ b/src/main/resources/hidden.yml @@ -14,3 +14,8 @@ Options: # Amount of users to convert every interval 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