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>
<groupId>fr.xephi</groupId>
<artifactId>authme</artifactId>
<version>5.1-SNAPSHOT</version>
<version>5.2-SNAPSHOT</version>
<optional>true</optional>
</dependency>

View File

@ -45,7 +45,7 @@ public class FastLoginBukkit extends JavaPlugin {
//SafeCacheBuilder is used in order to be version independent
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)
.expireAfterWrite(1, TimeUnit.MINUTES)
.expireAfterWrite(30, TimeUnit.SECONDS)
//mapped by ip:port -> 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 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() {

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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);