Skip to content

Commit

Permalink
fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
SooStrator1136 committed Oct 5, 2024
1 parent 0078768 commit 36b4fe4
Showing 1 changed file with 63 additions and 61 deletions.
124 changes: 63 additions & 61 deletions src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,75 +57,77 @@ public void onServerConnectedEvent(ServerConnectedEvent e) {
public void process() {
if (!processSemaphore.tryAcquire()) return;

// check queue server reachability
final RegisteredServer serverQueue;

try {
serverQueue = getServer(Config.serverQueue);
} catch (ServerNotReachableException e) {
log.warn(e.getMessage());
return;
}
// check queue server reachability
final RegisteredServer serverQueue;

try {
serverQueue = getServer(Config.serverQueue);
} catch (ServerNotReachableException e) {
log.warn(e.getMessage());
return;
}

// skip if no players queued
if (queuedPlayers.isEmpty())
return;
// skip if no players queued
if (queuedPlayers.isEmpty())
return;

// check the main server reachability
final RegisteredServer serverMain;

try {
serverMain = getServer(Config.serverMain);
} catch (ServerNotReachableException e) {
if (Instant.now().getEpochSecond() % 10 == 0) {
serverQueue.getPlayersConnected().forEach(queuedPlayer ->
queuedPlayer.sendMessage(Identity.nil(), Component.text(
Config.messageOffline
)));
}
return;
}

// check the main server reachability
final RegisteredServer serverMain;
// check main server full
boolean full = serverMain.getPlayersConnected().size() >= Config.maxPlayers;

try {
serverMain = getServer(Config.serverMain);
} catch (ServerNotReachableException e) {
// send info every 10 seconds
if (Instant.now().getEpochSecond() % 10 == 0) {
serverQueue.getPlayersConnected().forEach(queuedPlayer ->
queuedPlayer.sendMessage(Identity.nil(), Component.text(
Config.messageOffline
)));
sendInfo(serverQueue, full);
}
return;
}

// check main server full
boolean full = serverMain.getPlayersConnected().size() >= Config.maxPlayers;

// send info every 10 seconds
if (Instant.now().getEpochSecond() % 10 == 0) {
sendInfo(serverQueue, full);
}

if (full)
return;

QueuedPlayer currPlayer = queuedPlayers.getFirst();

//wait till delay has passed
if (currPlayer.joinTime() + Config.joinDelay > System.currentTimeMillis())
return;

// connect next player
UUID uuid = currPlayer.player().getUniqueId();

log.info("Processing " + uuid.toString());

// lookup player from queue server and ping to be safe the player is connected
serverQueue.getPlayersConnected().stream()
.filter(p -> p.getUniqueId().equals(uuid))
.findAny().ifPresentOrElse(p -> {
p.sendMessage(Identity.nil(), Component.text(Config.messageConnecting));
try {
if (p.createConnectionRequest(serverMain).connect().get().isSuccessful()) queuedPlayers.removeFirst();
} catch (InterruptedException | ExecutionException e) {
log.error("Unable to connect " + p.getUsername() + "(" + p.getUniqueId().toString() + ") to " + Config.serverMain + ": " + e.getMessage());
if (full)
return;

QueuedPlayer currPlayer = queuedPlayers.getFirst();

//wait till delay has passed
if (currPlayer.joinTime() + Config.joinDelay > System.currentTimeMillis())
return;

// connect next player
UUID uuid = currPlayer.player().getUniqueId();

log.info("Processing " + uuid.toString());

// lookup player from queue server and ping to be safe the player is connected
serverQueue.getPlayersConnected().stream()
.filter(p -> p.getUniqueId().equals(uuid))
.findAny().ifPresentOrElse(p -> {
p.sendMessage(Identity.nil(), Component.text(Config.messageConnecting));
try {
if (p.createConnectionRequest(serverMain).connect().get().isSuccessful()) queuedPlayers.removeFirst();
} catch (InterruptedException | ExecutionException e) {
log.error("Unable to connect " + p.getUsername() + "(" + p.getUniqueId().toString() + ") to " + Config.serverMain + ": " + e.getMessage());
}
},
() -> {
log.error("Unable to connect " + currPlayer.player().getUsername() + "(" + currPlayer.player().getUniqueId().toString() + ") to " + Config.serverMain + ": player is not connected to " + serverQueue.getServerInfo().getName());
queuedPlayers.removeFirst();
}
},
() -> {
log.error("Unable to connect " + currPlayer.player().getUsername() + "(" + currPlayer.player().getUniqueId().toString() + ") to " + Config.serverMain + ": player is not connected to " + serverQueue.getServerInfo().getName());
queuedPlayers.removeFirst();
}
);

processSemaphore.release();
);
} finally {
processSemaphore.release();
}
}

private void sendInfo(RegisteredServer serverQueue, boolean full) {
Expand Down

0 comments on commit 36b4fe4

Please sign in to comment.