Update checking now does a 36 hour rate limit if you have rate limit issues, or 6 hours otherwise. Potentially fixed an update period problem

This commit is contained in:
libraryaddict 2020-11-12 10:01:48 +13:00
parent e6cf577db0
commit a896721754
3 changed files with 46 additions and 25 deletions

View File

@ -256,9 +256,10 @@ public class DisguiseConfig {
@Setter @Setter
private static boolean saveUserPreferences; private static boolean saveUserPreferences;
@Getter @Getter
@Setter
private static long lastUpdateRequest; private static long lastUpdateRequest;
@Getter @Getter
private static boolean hittingRateLimit;
@Getter
@Setter @Setter
private static boolean copyPlayerTeamInfo; private static boolean copyPlayerTeamInfo;
@Getter @Getter
@ -294,6 +295,15 @@ public class DisguiseConfig {
doUpdaterTask(); doUpdaterTask();
} }
public static void setLastUpdateRequest(long lastRequest) {
if (lastRequest <= getLastUpdateRequest()) {
return;
}
lastUpdateRequest = lastRequest;
saveInternalConfig();
}
private static void doUpdaterTask() { private static void doUpdaterTask() {
boolean startTask = isAutoUpdate() || isNotifyUpdate() || "1592".equals( boolean startTask = isAutoUpdate() || isNotifyUpdate() || "1592".equals(
(LibsPremium.getPaidInformation() == null ? LibsPremium.getPluginInformation() : (LibsPremium.getPaidInformation() == null ? LibsPremium.getPluginInformation() :
@ -304,6 +314,8 @@ public class DisguiseConfig {
return; return;
} }
int timer = (int) (TimeUnit.HOURS.toSeconds(isHittingRateLimit() ? 36 : 6) * 20);
if (!LibsDisguises.getInstance().getConfig().getDefaults().getBoolean("AutoUpdate")) { if (!LibsDisguises.getInstance().getConfig().getDefaults().getBoolean("AutoUpdate")) {
updaterTask = Bukkit.getScheduler().runTaskTimer(LibsDisguises.getInstance(), new Runnable() { updaterTask = Bukkit.getScheduler().runTaskTimer(LibsDisguises.getInstance(), new Runnable() {
@Override @Override
@ -316,6 +328,8 @@ public class DisguiseConfig {
} }
} }
}, TimeUnit.HOURS.toSeconds(1) * 20, (20 * TimeUnit.MINUTES.toSeconds(10))); }, TimeUnit.HOURS.toSeconds(1) * 20, (20 * TimeUnit.MINUTES.toSeconds(10)));
return;
} }
if (updaterTask == null != startTask) { if (updaterTask == null != startTask) {
@ -328,25 +342,18 @@ public class DisguiseConfig {
return; return;
} }
long timeSinceLast = System.currentTimeMillis() - (getLastUpdateRequest() + TimeUnit.HOURS.toMillis(6)); // Get the ticks since last update
long timeSinceLast = (System.currentTimeMillis() - getLastUpdateRequest()) / 50;
// Change timer to 30 min if longer than that // Next update check will be in 30 seconds, or the timer - elapsed time. Whatever is greater
if (timeSinceLast > TimeUnit.MINUTES.toMillis(30)) { timeSinceLast = Math.max(30 * 20, timer - timeSinceLast);
timeSinceLast = TimeUnit.MINUTES.toMillis(30);
}
if (timeSinceLast > 0) {
timeSinceLast /= 50;
} else {
timeSinceLast = 0;
}
updaterTask = Bukkit.getScheduler().runTaskTimerAsynchronously(LibsDisguises.getInstance(), new Runnable() { updaterTask = Bukkit.getScheduler().runTaskTimerAsynchronously(LibsDisguises.getInstance(), new Runnable() {
@Override @Override
public void run() { public void run() {
LibsDisguises.getInstance().getUpdateChecker().doAutoUpdateCheck(); LibsDisguises.getInstance().getUpdateChecker().doAutoUpdateCheck();
} }
}, timeSinceLast, (20 * TimeUnit.HOURS.toSeconds(6))); // Check every 6 hours }, timeSinceLast, timer);
} }
public static void setUsingReleaseBuilds(boolean useReleaseBuilds) { public static void setUsingReleaseBuilds(boolean useReleaseBuilds) {
@ -358,6 +365,16 @@ public class DisguiseConfig {
saveInternalConfig(); saveInternalConfig();
} }
public static void setHittingRateLimit(boolean hitRateLimit) {
if (hitRateLimit == isHittingRateLimit()) {
return;
}
hittingRateLimit = hitRateLimit;
saveInternalConfig();
doUpdaterTask();
}
public static void setBisectHosted(boolean isBisectHosted, String serverIP) { public static void setBisectHosted(boolean isBisectHosted, String serverIP) {
if (isBisectHosted() == isBisectHosted && getSavedServerIp().equals(serverIP)) { if (isBisectHosted() == isBisectHosted && getSavedServerIp().equals(serverIP)) {
return; return;
@ -381,6 +398,7 @@ public class DisguiseConfig {
savedServerIp = configuration.getString("Server-IP", getSavedServerIp()); savedServerIp = configuration.getString("Server-IP", getSavedServerIp());
usingReleaseBuild = configuration.getBoolean("ReleaseBuild", isUsingReleaseBuild()); usingReleaseBuild = configuration.getBoolean("ReleaseBuild", isUsingReleaseBuild());
lastUpdateRequest = configuration.getLong("LastUpdateRequest", 0L); lastUpdateRequest = configuration.getLong("LastUpdateRequest", 0L);
hittingRateLimit = configuration.getBoolean("HittingRateLimit", false);
if (!configuration.contains("Bisect-Hosted") || !configuration.contains("Server-IP") || if (!configuration.contains("Bisect-Hosted") || !configuration.contains("Server-IP") ||
!configuration.contains("ReleaseBuild")) { !configuration.contains("ReleaseBuild")) {
@ -396,7 +414,7 @@ public class DisguiseConfig {
// Bisect hosted, server ip, release builds // Bisect hosted, server ip, release builds
for (Object s : new Object[]{isBisectHosted(), getSavedServerIp(), isUsingReleaseBuild(), for (Object s : new Object[]{isBisectHosted(), getSavedServerIp(), isUsingReleaseBuild(),
getLastUpdateRequest()}) { getLastUpdateRequest(), isHittingRateLimit()}) {
internalConfig = internalConfig.replaceFirst("%data%", "" + s); internalConfig = internalConfig.replaceFirst("%data%", "" + s);
} }
@ -747,8 +765,8 @@ public class DisguiseConfig {
} }
try { try {
String option = String option = config.getString("SelfDisguisesScoreboard", DisguisePushing.MODIFY_SCOREBOARD.name())
config.getString("SelfDisguisesScoreboard", DisguisePushing.MODIFY_SCOREBOARD.name()).toUpperCase(Locale.ENGLISH); .toUpperCase(Locale.ENGLISH);
if (!option.endsWith("_SCOREBOARD")) { if (!option.endsWith("_SCOREBOARD")) {
option += "_SCOREBOARD"; option += "_SCOREBOARD";

View File

@ -3,6 +3,7 @@ package me.libraryaddict.disguise.utilities.updates;
import com.google.gson.Gson; import com.google.gson.Gson;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import me.libraryaddict.disguise.DisguiseConfig;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.LibsPremium; import me.libraryaddict.disguise.utilities.LibsPremium;
@ -83,8 +84,7 @@ public class LDGithub {
} }
return ((String) map.get("body")).split("(\\r|\\n)+"); return ((String) map.get("body")).split("(\\r|\\n)+");
} } catch (Exception ignored) {
catch (Exception ignored) {
} }
return new String[0]; return new String[0];
@ -129,15 +129,18 @@ public class LDGithub {
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
gitData = new Gson().fromJson(json, GithubData.class); gitData = new Gson().fromJson(json, GithubData.class);
} } catch (IOException ex) {
catch (IOException ex) {
try (InputStream error = con.getErrorStream()) { try (InputStream error = con.getErrorStream()) {
String line = new BufferedReader(new InputStreamReader(error, StandardCharsets.UTF_8)).lines() String line = new BufferedReader(new InputStreamReader(error, StandardCharsets.UTF_8)).lines()
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
DisguiseUtilities.getLogger().severe("Error with Github! " + line); DisguiseUtilities.getLogger().severe("Error with Github! " + line);
if (line.contains("rate limit") && !DisguiseConfig.isHittingRateLimit()) {
DisguiseConfig.setHittingRateLimit(true);
DisguiseUtilities.getLogger().severe("Changed update checker to be every 36 hours due to rate limiting from this IP");
} }
catch (Exception ex1) { } catch (Exception ex1) {
DisguiseUtilities.getLogger().severe("Error when trying to read error stream! Inception!"); DisguiseUtilities.getLogger().severe("Error when trying to read error stream! Inception!");
ex1.printStackTrace(); ex1.printStackTrace();
} }
@ -162,8 +165,7 @@ public class LDGithub {
return new GithubUpdate(gitData.getTag_name().replace("v", ""), gitData.getBody().split("(\\r|\\n)+"), return new GithubUpdate(gitData.getTag_name().replace("v", ""), gitData.getBody().split("(\\r|\\n)+"),
download); download);
} } catch (Exception ex) {
catch (Exception ex) {
DisguiseUtilities.getLogger().warning("Failed to check for a release on Github"); DisguiseUtilities.getLogger().warning("Failed to check for a release on Github");
ex.printStackTrace(); ex.printStackTrace();
} }

View File

@ -8,3 +8,4 @@ Server-IP: %data%
ReleaseBuild: %data% ReleaseBuild: %data%
# Make sure the plugin aint spamming update requests # Make sure the plugin aint spamming update requests
LastUpdateCheck: %data% LastUpdateCheck: %data%
HittingRateLimit: %data%