From 36b4fe4e996e2e4ccca67cbcdb028590e9b694ea Mon Sep 17 00:00:00 2001 From: SooStrator1136 <49093091+SooStrator1136@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:07:15 +0200 Subject: [PATCH] fix deadlock --- .../org/zeroBzeroT/anarchyqueue/Queue.java | 124 +++++++++--------- 1 file changed, 63 insertions(+), 61 deletions(-) diff --git a/src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java b/src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java index f0d6524..248df5b 100644 --- a/src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java +++ b/src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java @@ -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) {