From 353cd17823066632bf60db1bcfb32cd83ee72e31 Mon Sep 17 00:00:00 2001 From: games647 Date: Tue, 2 Feb 2016 14:57:20 +0100 Subject: [PATCH] Run forceRegister async if possible -> improve performance --- bukkit/pom.xml | 2 +- .../fastlogin/bukkit/FastLoginBukkit.java | 2 +- .../bukkit/hooks/CrazyLoginHook.java | 39 +++++++++++++------ .../bukkit/hooks/LoginSecurityHook.java | 23 ++++++++--- .../fastlogin/bukkit/hooks/xAuthHook.java | 10 ++--- .../listener/ProtcolSupportListener.java | 3 ++ 6 files changed, 55 insertions(+), 24 deletions(-) diff --git a/bukkit/pom.xml b/bukkit/pom.xml index a1221089..994c88ff 100644 --- a/bukkit/pom.xml +++ b/bukkit/pom.xml @@ -70,7 +70,7 @@ fr.xephi authme - 5.1-SNAPSHOT + 5.2-SNAPSHOT true diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java index e55c962a..3a8a462c 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/FastLoginBukkit.java @@ -45,7 +45,7 @@ public class FastLoginBukkit extends JavaPlugin { //SafeCacheBuilder is used in order to be version independent private final ConcurrentMap session = SafeCacheBuilder.newBuilder() //2 minutes should be enough as a timeout for bad internet connection (Server, Client and Mojang) - .expireAfterWrite(1, TimeUnit.MINUTES) + .expireAfterWrite(30, TimeUnit.SECONDS) //mapped by ip:port -> PlayerSession .build(new CacheLoader() { diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/CrazyLoginHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/CrazyLoginHook.java index aa8e5a14..6725012a 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/CrazyLoginHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/CrazyLoginHook.java @@ -10,6 +10,7 @@ import de.st_ddt.crazylogin.metadata.Authenticated; import java.util.logging.Level; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; /** @@ -59,18 +60,34 @@ public class CrazyLoginHook implements AuthPlugin { } @Override - public void forceRegister(Player player, String password) { - CrazyLogin crazyLoginPlugin = CrazyLogin.getPlugin(); - CrazyLoginDataDatabase crazyDatabase = crazyLoginPlugin.getCrazyDatabase(); + public void forceRegister(final Player player, String password) { + final CrazyLogin crazyLoginPlugin = CrazyLogin.getPlugin(); + final CrazyLoginDataDatabase crazyDatabase = crazyLoginPlugin.getCrazyDatabase(); - LoginPlayerData playerData = crazyLoginPlugin.getPlayerData(player.getName()); - if (playerData == null) { - //create a fake account - this will be saved to the database with the password=FAILEDLOADING - //user cannot login with that password unless the admin uses plain text - //this automatically marks the player as logged in - playerData = new LoginPlayerData(player); - crazyDatabase.save(playerData); - } + //this executes a sql query and accesses only thread safe collections so we can run it async + Bukkit.getScheduler().runTaskAsynchronously(crazyLoginPlugin, new Runnable() { + @Override + public void run() { + LoginPlayerData playerData = crazyLoginPlugin.getPlayerData(player.getName()); + if (playerData == null) { + //create a fake account - this will be saved to the database with the password=FAILEDLOADING + //user cannot login with that password unless the admin uses plain text + //this automatically marks the player as logged in + playerData = new LoginPlayerData(player); + crazyDatabase.save(playerData); + + //this method is not thread-safe and requires the existence of the account + //so reschedule it to the main thread + Bukkit.getScheduler().runTask(crazyLoginPlugin, new Runnable() { + @Override + public void run() { + //login the player after registration + forceLogin(player); + } + }); + } + } + }); } private PlayerListener getListener() { diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/LoginSecurityHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/LoginSecurityHook.java index 9e939316..91904390 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/LoginSecurityHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/LoginSecurityHook.java @@ -8,6 +8,7 @@ import java.net.InetAddress; import java.util.UUID; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; /** @@ -48,13 +49,23 @@ public class LoginSecurityHook implements AuthPlugin { } @Override - public void forceRegister(Player player, String password) { - LoginSecurity securityPlugin = LoginSecurity.instance; - DataManager dataManager = securityPlugin.data; + public void forceRegister(Player player, final String password) { + final LoginSecurity securityPlugin = LoginSecurity.instance; + final DataManager dataManager = securityPlugin.data; UUID playerUUID = player.getUniqueId(); - String uuidString = playerUUID.toString().replace("-", ""); - InetAddress ipAddress = player.getAddress().getAddress(); - dataManager.register(uuidString, password, securityPlugin.hasher.getTypeId(), ipAddress.toString()); + final String uuidString = playerUUID.toString().replace("-", ""); + final InetAddress ipAddress = player.getAddress().getAddress(); + + //this executes a sql query without interacting with other parts so we can run it async. + Bukkit.getScheduler().runTaskAsynchronously(securityPlugin, new Runnable() { + @Override + public void run() { + dataManager.register(uuidString, password, securityPlugin.hasher.getTypeId(), ipAddress.toString()); + } + }); + + //notify the plugin that this player can be logged in + forceLogin(player); } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/xAuthHook.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/xAuthHook.java index bf16afbd..cf32a176 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/xAuthHook.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/hooks/xAuthHook.java @@ -20,6 +20,7 @@ public class xAuthHook implements AuthPlugin { xAuthPlugin.getPlayerManager().doLogin(xAuthPlayer); //we checked that the player is premium (paid account) + //unprotect the inventory, op status... xAuthPlayer.setPremium(true); } } @@ -38,13 +39,12 @@ public class xAuthHook implements AuthPlugin { xAuthPlayer xAuthPlayer = xAuthPlugin.getPlayerManager().getPlayer(player); if (xAuthPlayer != null) { + //this should run async because the plugin executes a sql query, but the method + //accesses non thread-safe collections :( xAuthPlugin.getAuthClass(xAuthPlayer).adminRegister(player.getName(), password, null); - //we checked that the player is premium (paid account) - xAuthPlayer.setPremium(true); - - //unprotect the inventory, op status... - xAuthPlugin.getPlayerManager().doLogin(xAuthPlayer); + //login in the player after registration + forceLogin(player); } } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/ProtcolSupportListener.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/ProtcolSupportListener.java index 334d3986..9c46ef80 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/ProtcolSupportListener.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/listener/ProtcolSupportListener.java @@ -35,6 +35,9 @@ public class ProtcolSupportListener implements Listener { } String playerName = loginStartEvent.getName(); + + //remove old data every time on a new login in order to keep the session only for one person + plugin.getSessions().remove(playerName); if (plugin.getEnabledPremium().contains(playerName)) { //the player have to be registered in order to invoke the command startPremiumSession(playerName, loginStartEvent, true);