Skip to content

Commit

Permalink
fix too many dispatcher threads
Browse files Browse the repository at this point in the history
  • Loading branch information
0us committed Nov 27, 2019
1 parent 9dceabf commit 1fb6cd5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
import java.net.DatagramPacket;
import java.util.Deque;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.*;

/**
* Class holding all necessary information to handle a given DatagramPacket
Expand Down Expand Up @@ -50,6 +49,7 @@ public class DispatchProcessor implements Tickable {
private static final double MAX_MA_LENIENCY = (1d / CommunicationConfig.TICKRATE - 1);
private static final int MAX_ACTIONS_AGE = CommunicationConfig.TICKRATE / 10;

private volatile Future future = null;

public DispatchProcessor(GameState<PlayerState, MovableState> gameState) {
this.gameState = gameState;
Expand All @@ -71,13 +71,12 @@ public DispatchProcessor(GameState<PlayerState, MovableState> gameState) {
* @param packet the packet to process
* @param executor the executor to execute on
*/
void tryRun(DatagramPacket packet, ExecutorService executor) {
if (running) {
void tryRun(DatagramPacket packet, ExecutorService executor) throws ExecutionException, InterruptedException {
if (future != null && !future.isDone()) {
enqueueTask(packet);
} else {
running = true;
enqueueTask(packet);
executor.execute(this.worker);
future = executor.submit(this.worker);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public class PlayerUpdateDispatcher extends ThreadPoolExecutor {
private AtomicLong tickCounter;

public PlayerUpdateDispatcher() {
super(2, CommunicationConfig.MAX_PLAYERS * 3, CommunicationConfig.RETRY_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS, new SynchronousQueue<>(),
super(
1,
CommunicationConfig.MAX_PLAYERS * 3,
CommunicationConfig.RETRY_CONNECTION_TIMEOUT,
TimeUnit.MILLISECONDS,
new SynchronousQueue<>(),
new ThreadFactoryBuilder().setNameFormat("Dispathcher-thread-%d").build());
gameStateMaster = GameStateMaster.getInstance();
connections = Connections.getInstance();
Expand All @@ -41,13 +46,22 @@ public PlayerUpdateDispatcher() {
public void dispatch(DatagramPacket packet) {
SocketAddress socketAddr = packet.getSocketAddress();
DispatchProcessor worker = workers.get(socketAddr);
if (worker != null) {
worker.tryRun(packet, this);
} else {
worker = new DispatchProcessor(gameStateMaster.getGameState());
GameServer.observe(worker);
workers.put(socketAddr, worker);
worker.tryRun(packet, this);
try {
if (worker != null) {
worker.tryRun(packet, this);
} else {
System.out.println("NEW DISPATCHPROCESSOR FOR " + socketAddr);
worker = new DispatchProcessor(gameStateMaster.getGameState());
GameServer.observe(worker);
workers.put(socketAddr, worker);
worker.tryRun(packet, this);
}
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class GameDataSender extends ThreadPoolExecutor {
private long time;

public GameDataSender() {
super(1, Integer.MAX_VALUE, CommunicationConfig.RETRY_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS, new SynchronousQueue<>(),
super(1, Integer.MAX_VALUE, CommunicationConfig.TICKRATE*10, TimeUnit.MILLISECONDS, new SynchronousQueue<>(),
new ThreadFactoryBuilder().setNameFormat("GameDataSender-%d").build());
this.gameStateMaster = GameStateMaster.getInstance();
LogManager.getLogger(tag).trace("Created " + this.getClass().getName());
Expand Down

0 comments on commit 1fb6cd5

Please sign in to comment.