Add static to fields for single instance to manage all heartbeat threads. Far more efficient than 1 thread each sleeping!

This commit is contained in:
kohlerpop1
2025-09-04 22:01:40 -04:00
parent 646e4c68ab
commit 9c75abc80f

View File

@@ -30,13 +30,13 @@ import java.util.concurrent.*;
public class WebSocketHeartbeatTask
{
// Single shared pool for all heartbeat tasks
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, r -> {
private static final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, r -> {
Thread t = new Thread(r, "heartbeat-pool");
t.setDaemon(true);
return t;
});
private final Map<WebSocket, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
private final Map<WebSocket, Long> commTime = new ConcurrentHashMap<>();
private static final Map<WebSocket, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
private static final Map<WebSocket, Long> commTime = new ConcurrentHashMap<>();
private final byte[] heartbeatBytes = Base64.getDecoder().decode("MgJwYjoCaGI="); // Used to be '3A026862' aka ':\x02hb', now is '2\x02pb:\x02hb'.