Run forceRegister async if possible -> improve performance

This commit is contained in:
games647
2016-02-02 14:57:20 +01:00
parent 157b8499a9
commit 353cd17823
6 changed files with 55 additions and 24 deletions

View File

@ -70,7 +70,7 @@
<dependency> <dependency>
<groupId>fr.xephi</groupId> <groupId>fr.xephi</groupId>
<artifactId>authme</artifactId> <artifactId>authme</artifactId>
<version>5.1-SNAPSHOT</version> <version>5.2-SNAPSHOT</version>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>

View File

@ -45,7 +45,7 @@ public class FastLoginBukkit extends JavaPlugin {
//SafeCacheBuilder is used in order to be version independent //SafeCacheBuilder is used in order to be version independent
private final ConcurrentMap<String, PlayerSession> session = SafeCacheBuilder.<String, PlayerSession>newBuilder() private final ConcurrentMap<String, PlayerSession> session = SafeCacheBuilder.<String, PlayerSession>newBuilder()
//2 minutes should be enough as a timeout for bad internet connection (Server, Client and Mojang) //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 //mapped by ip:port -> PlayerSession
.build(new CacheLoader<String, PlayerSession>() { .build(new CacheLoader<String, PlayerSession>() {

View File

@ -10,6 +10,7 @@ import de.st_ddt.crazylogin.metadata.Authenticated;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
/** /**
@ -59,18 +60,34 @@ public class CrazyLoginHook implements AuthPlugin {
} }
@Override @Override
public void forceRegister(Player player, String password) { public void forceRegister(final Player player, String password) {
CrazyLogin crazyLoginPlugin = CrazyLogin.getPlugin(); final CrazyLogin crazyLoginPlugin = CrazyLogin.getPlugin();
CrazyLoginDataDatabase crazyDatabase = crazyLoginPlugin.getCrazyDatabase(); final CrazyLoginDataDatabase crazyDatabase = crazyLoginPlugin.getCrazyDatabase();
LoginPlayerData playerData = crazyLoginPlugin.getPlayerData(player.getName()); //this executes a sql query and accesses only thread safe collections so we can run it async
if (playerData == null) { Bukkit.getScheduler().runTaskAsynchronously(crazyLoginPlugin, new Runnable() {
//create a fake account - this will be saved to the database with the password=FAILEDLOADING @Override
//user cannot login with that password unless the admin uses plain text public void run() {
//this automatically marks the player as logged in LoginPlayerData playerData = crazyLoginPlugin.getPlayerData(player.getName());
playerData = new LoginPlayerData(player); if (playerData == null) {
crazyDatabase.save(playerData); //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() { private PlayerListener getListener() {

View File

@ -8,6 +8,7 @@ import java.net.InetAddress;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
/** /**
@ -48,13 +49,23 @@ public class LoginSecurityHook implements AuthPlugin {
} }
@Override @Override
public void forceRegister(Player player, String password) { public void forceRegister(Player player, final String password) {
LoginSecurity securityPlugin = LoginSecurity.instance; final LoginSecurity securityPlugin = LoginSecurity.instance;
DataManager dataManager = securityPlugin.data; final DataManager dataManager = securityPlugin.data;
UUID playerUUID = player.getUniqueId(); UUID playerUUID = player.getUniqueId();
String uuidString = playerUUID.toString().replace("-", ""); final String uuidString = playerUUID.toString().replace("-", "");
InetAddress ipAddress = player.getAddress().getAddress(); final InetAddress ipAddress = player.getAddress().getAddress();
dataManager.register(uuidString, password, securityPlugin.hasher.getTypeId(), ipAddress.toString());
//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);
} }
} }

View File

@ -20,6 +20,7 @@ public class xAuthHook implements AuthPlugin {
xAuthPlugin.getPlayerManager().doLogin(xAuthPlayer); xAuthPlugin.getPlayerManager().doLogin(xAuthPlayer);
//we checked that the player is premium (paid account) //we checked that the player is premium (paid account)
//unprotect the inventory, op status...
xAuthPlayer.setPremium(true); xAuthPlayer.setPremium(true);
} }
} }
@ -38,13 +39,12 @@ public class xAuthHook implements AuthPlugin {
xAuthPlayer xAuthPlayer = xAuthPlugin.getPlayerManager().getPlayer(player); xAuthPlayer xAuthPlayer = xAuthPlugin.getPlayerManager().getPlayer(player);
if (xAuthPlayer != null) { 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); xAuthPlugin.getAuthClass(xAuthPlayer).adminRegister(player.getName(), password, null);
//we checked that the player is premium (paid account) //login in the player after registration
xAuthPlayer.setPremium(true); forceLogin(player);
//unprotect the inventory, op status...
xAuthPlugin.getPlayerManager().doLogin(xAuthPlayer);
} }
} }
} }

View File

@ -35,6 +35,9 @@ public class ProtcolSupportListener implements Listener {
} }
String playerName = loginStartEvent.getName(); 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)) { if (plugin.getEnabledPremium().contains(playerName)) {
//the player have to be registered in order to invoke the command //the player have to be registered in order to invoke the command
startPremiumSession(playerName, loginStartEvent, true); startPremiumSession(playerName, loginStartEvent, true);