Skip to content

Commit

Permalink
prevent race condition when processing players
Browse files Browse the repository at this point in the history
  • Loading branch information
SooStrator1136 committed Oct 3, 2024
1 parent 84b91b3 commit 17febeb
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/zeroBzeroT/anarchyqueue/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Semaphore;

// velocity api event docs:
// https://jd.papermc.io/velocity/3.3.0/com/velocitypowered/api/event/package-summary.html
Expand All @@ -25,6 +26,8 @@ public class Queue {

private final ProxyServer proxyServer;

private final Semaphore processSemaphore = new Semaphore(1);

/**
* We don't use ConcurrentLinkedQueue for this because we want index-based access to players.
*/
Expand Down Expand Up @@ -52,6 +55,8 @@ public void onServerConnectedEvent(ServerConnectedEvent e) {
}

public void process() {
if (!processSemaphore.tryAcquire()) return;

// check queue server reachability
final RegisteredServer serverQueue;

Expand Down Expand Up @@ -113,6 +118,8 @@ public void process() {
queuedPlayers.removeFirst();
}
);

processSemaphore.release();
}

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

0 comments on commit 17febeb

Please sign in to comment.