|
21 | 21 | import net.swofty.commons.config.ConfigProvider; |
22 | 22 | import net.swofty.commons.protocol.RedisProtocol; |
23 | 23 | import net.swofty.commons.protocol.objects.proxy.to.*; |
| 24 | +import net.swofty.commons.redis.ProxyHeartbeat; |
24 | 25 | import net.swofty.commons.redis.RedisClient; |
25 | 26 | import net.swofty.proxyapi.ProxyAPI; |
26 | 27 | import net.swofty.proxyapi.ProxyService; |
|
48 | 49 | import java.util.Set; |
49 | 50 | import java.util.UUID; |
50 | 51 | import java.util.concurrent.CompletableFuture; |
| 52 | +import java.util.concurrent.Executors; |
| 53 | +import java.util.concurrent.ScheduledExecutorService; |
51 | 54 | import java.util.concurrent.TimeUnit; |
52 | 55 | import java.util.concurrent.atomic.AtomicBoolean; |
| 56 | +import java.util.concurrent.atomic.AtomicInteger; |
53 | 57 | import java.util.stream.Collectors; |
54 | 58 |
|
55 | 59 |
|
@@ -246,7 +250,7 @@ static void main(String[] args) { |
246 | 250 |
|
247 | 251 | Logger.info("Received server name: " + HypixelConst.getServerName()); |
248 | 252 | }); |
249 | | - checkProxyConnected(MinecraftServer.getSchedulerManager()); |
| 253 | + checkProxyConnected(); |
250 | 254 |
|
251 | 255 | // Initialize anticheat |
252 | 256 | if (ConfigProvider.settings().getIntegrations().isAnticheat()) { |
@@ -340,32 +344,42 @@ private static Map<String, String> parseOptionalArgs(String[] args) { |
340 | 344 | return options; |
341 | 345 | } |
342 | 346 |
|
343 | | - private static void checkProxyConnected(Scheduler scheduler) { |
344 | | - scheduler.submitTask(() -> { |
345 | | - AtomicBoolean responded = new AtomicBoolean(false); |
| 347 | + private static final int PROXY_HEARTBEAT_CHECK_SECONDS = 3; |
| 348 | + private static final int PROXY_HEARTBEAT_MAX_MISSES = 3; |
346 | 349 |
|
| 350 | + private static void checkProxyConnected() { |
| 351 | + ProxyHeartbeat.init(ConfigProvider.settings().getRedisUri()); |
| 352 | + |
| 353 | + ScheduledExecutorService monitor = Executors.newSingleThreadScheduledExecutor(runnable -> { |
| 354 | + Thread thread = new Thread(runnable, "proxy-heartbeat-monitor"); |
| 355 | + thread.setDaemon(true); |
| 356 | + return thread; |
| 357 | + }); |
| 358 | + |
| 359 | + AtomicInteger missed = new AtomicInteger(0); |
| 360 | + monitor.scheduleAtFixedRate(() -> { |
| 361 | + boolean alive; |
347 | 362 | try { |
348 | | - RedisClient.requestProxy(new ProxyIsOnlineProtocol(), |
349 | | - new ProxyIsOnlineProtocol.Request()).thenAccept(response -> { |
350 | | - if (response.online()) { |
351 | | - responded.set(true); |
352 | | - } |
353 | | - }); |
| 363 | + alive = ProxyHeartbeat.isProxyAlive(); |
354 | 364 | } catch (Exception e) { |
355 | | - MinecraftServer.getConnectionManager().getOnlinePlayers().forEach(player -> player.kick("§cServer has lost connection to the proxy, please rejoin")); |
356 | | - CompletableFuture.delayedExecutor(500, TimeUnit.MILLISECONDS) |
357 | | - .execute(() -> System.exit(0)); |
358 | | - return TaskSchedule.stop(); |
| 365 | + alive = false; |
359 | 366 | } |
360 | 367 |
|
361 | | - scheduler.scheduleTask(() -> { |
362 | | - if (!responded.get()) { |
363 | | - Logger.error("Proxy did not respond to alive check. Shutting down..."); |
364 | | - System.exit(0); |
365 | | - } |
366 | | - }, TaskSchedule.tick(20), TaskSchedule.stop()); |
| 368 | + if (alive) { |
| 369 | + missed.set(0); |
| 370 | + return; |
| 371 | + } |
367 | 372 |
|
368 | | - return TaskSchedule.seconds(1); |
369 | | - }, ExecutionType.TICK_END); |
| 373 | + int misses = missed.incrementAndGet(); |
| 374 | + Logger.warn("Proxy heartbeat missing ({}/{})", misses, PROXY_HEARTBEAT_MAX_MISSES); |
| 375 | + if (misses >= PROXY_HEARTBEAT_MAX_MISSES) { |
| 376 | + Logger.error("Proxy heartbeat absent for ~{}s. Shutting down...", |
| 377 | + PROXY_HEARTBEAT_MAX_MISSES * PROXY_HEARTBEAT_CHECK_SECONDS); |
| 378 | + MinecraftServer.getConnectionManager().getOnlinePlayers() |
| 379 | + .forEach(player -> player.kick("§cServer has lost connection to the proxy, please rejoin")); |
| 380 | + CompletableFuture.delayedExecutor(500, TimeUnit.MILLISECONDS) |
| 381 | + .execute(() -> System.exit(0)); |
| 382 | + } |
| 383 | + }, PROXY_HEARTBEAT_CHECK_SECONDS, PROXY_HEARTBEAT_CHECK_SECONDS, TimeUnit.SECONDS); |
370 | 384 | } |
371 | 385 | } |
0 commit comments