3030@ Slf4j
3131@ EqualsAndHashCode
3232public class TelemetryDebugger implements Debugger , AutoCloseable {
33- private static final int MAX_RECONNECT_ATTEMPTS = 2 ;
33+ private static final int MAX_RECONNECT_ATTEMPTS = 5 ;
3434 private static final Duration RECONNECT_BASE_DELAY = Duration .ofSeconds (1 );
3535 private static final Duration MAX_RECONNECT_DELAY = Duration .ofSeconds (30 );
3636
@@ -40,7 +40,7 @@ public class TelemetryDebugger implements Debugger, AutoCloseable {
4040 private final ObjectMapper objectMapper ;
4141
4242 private final Map <Integer , RateLimiter > peerRateLimiter = new ConcurrentHashMap <>();
43- private final BlockingQueue <OutgoingMessageV1 > messageQueue = new LinkedBlockingQueue <>(1000 ); // Ограничение очереди
43+ private final BlockingQueue <OutgoingMessageV1 > messageQueue = new LinkedBlockingQueue <>(1000 );
4444
4545 private final Thread sendingLoopThread ;
4646
@@ -56,7 +56,6 @@ public TelemetryDebugger(GPGNetServer gpgNetServer, String telemetryServer, int
5656 gameId ,
5757 playerId );
5858
59- // Создаём первый клиент
6059 createNewWebSocketClient ();
6160
6261 objectMapper = new ObjectMapper ();
@@ -69,8 +68,7 @@ private void createNewWebSocketClient() {
6968 this .websocketClient = new WebSocketClient (websocketUri ) {
7069 @ Override
7170 public void onOpen (ServerHandshake handshakedata ) {
72- log .info ("Telemetry websocket opened" );
73- reconnectAttempt = 0 ; // Сброс счётчика при успехе
71+ log .trace ("Telemetry websocket opened" );
7472 }
7573
7674 @ Override
@@ -81,7 +79,6 @@ public void onMessage(String message) {
8179 @ Override
8280 public void onClose (int code , String reason , boolean remote ) {
8381 log .info ("Telemetry websocket closed (code: {}, reason: {})" , code , reason );
84- // Клиент закрыт — следующая попытка должна создать новый
8582 }
8683
8784 @ Override
@@ -105,8 +102,7 @@ private void sendMessage(OutgoingMessageV1 message) {
105102 private void sendingLoop () {
106103 try {
107104 while (shouldRun ) {
108- OutgoingMessageV1 message = messageQueue .poll (1 , TimeUnit .SECONDS );
109- if (message == null ) continue ;
105+ OutgoingMessageV1 message = messageQueue .take ();
110106
111107 if (!ensureConnected ()) {
112108 log .warn ("Failed to send telemetry message (no connection): {}" , message .getType ());
@@ -137,7 +133,9 @@ private void sendingLoop() {
137133
138134 private boolean ensureConnected () throws InterruptedException {
139135 while (shouldRun && !websocketClient .isOpen ()) {
140- if (!shouldRun ) return false ;
136+ if (!shouldRun ) {
137+ return false ;
138+ }
141139
142140 // Exponential latency with jitter
143141 Duration delay = RECONNECT_BASE_DELAY .multipliedBy ((long ) Math .pow (2 , Math .min (reconnectAttempt , 5 )));
@@ -149,8 +147,9 @@ private boolean ensureConnected() throws InterruptedException {
149147 Thread .sleep (delay .toMillis ());
150148
151149 try {
152- createNewWebSocketClient (); // Создаём новый клиент
150+ createNewWebSocketClient ();
153151 if (websocketClient .connectBlocking ()) {
152+ reconnectAttempt = 0 ;
154153 return true ;
155154 } else {
156155 log .warn ("Failed to connect to telemetry websocket (attempt {}/{})" , reconnectAttempt + 1 , MAX_RECONNECT_ATTEMPTS );
0 commit comments