Skip to content

Commit 1817719

Browse files
committed
chore: better error reporting for ConnectionWatchDog
1 parent 19e6463 commit 1817719

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

src/main/java/io/lettuce/core/protocol/ConnectionWatchdog.java

+36-29
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
import java.util.concurrent.CompletableFuture;
2626
import java.util.concurrent.TimeUnit;
2727
import java.util.concurrent.atomic.AtomicBoolean;
28+
import java.util.function.Supplier;
2829

2930
import io.lettuce.core.ClientOptions;
3031
import io.lettuce.core.ConnectionBuilder;
3132
import io.lettuce.core.ConnectionEvents;
33+
import io.lettuce.core.RedisException;
3234
import io.lettuce.core.event.EventBus;
3335
import io.lettuce.core.event.connection.ReconnectAttemptEvent;
3436
import io.lettuce.core.event.connection.ReconnectFailedEvent;
@@ -215,16 +217,6 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
215217
channel = null;
216218

217219
if (listenOnChannelInactive && !reconnectionHandler.isReconnectSuspended()) {
218-
if (!isEventLoopGroupActive()) {
219-
logger.debug("isEventLoopGroupActive() == false");
220-
return;
221-
}
222-
223-
if (!isListenOnChannelInactive()) {
224-
logger.debug("Skip reconnect scheduling, listener disabled");
225-
return;
226-
}
227-
228220
if (!useAutoBatchFlushEndpoint) {
229221
this.scheduleReconnect();
230222
}
@@ -261,14 +253,16 @@ public void scheduleReconnect() {
261253
logger.debug("{} scheduleReconnect()", logPrefix());
262254

263255
if (!isEventLoopGroupActive()) {
264-
logger.debug("isEventLoopGroupActive() == false");
265-
notifyEndpointFailedToConnectIfNeeded();
256+
final String errMsg = "isEventLoopGroupActive() == false";
257+
logger.debug(errMsg);
258+
notifyEndpointFailedToConnectIfNeeded(errMsg);
266259
return;
267260
}
268261

269262
if (!isListenOnChannelInactive()) {
270-
logger.debug("Skip reconnect scheduling, listener disabled");
271-
notifyEndpointFailedToConnectIfNeeded();
263+
final String errMsg = "Skip reconnect scheduling, listener disabled";
264+
logger.debug(errMsg);
265+
notifyEndpointFailedToConnectIfNeeded(errMsg);
272266
return;
273267
}
274268

@@ -285,8 +279,9 @@ public void scheduleReconnect() {
285279
reconnectScheduleTimeout = null;
286280

287281
if (!isEventLoopGroupActive()) {
288-
logger.warn("Cannot execute scheduled reconnect timer, reconnect workers are terminated");
289-
notifyEndpointFailedToConnectIfNeeded();
282+
final String errMsg = "Cannot execute scheduled reconnect timer, reconnect workers are terminated";
283+
logger.warn(errMsg);
284+
notifyEndpointFailedToConnectIfNeeded(errMsg);
290285
return;
291286
}
292287

@@ -302,17 +297,25 @@ public void scheduleReconnect() {
302297
}
303298
} else {
304299
logger.debug("{} Skipping scheduleReconnect() because I have an active channel", logPrefix());
305-
notifyEndpointFailedToConnectIfNeeded();
300+
notifyEndpointFailedToConnectIfNeeded("Skipping scheduleReconnect() because I have an active channel");
301+
}
302+
}
303+
304+
private void notifyEndpointFailedToConnectIfNeeded(String msg) {
305+
if (useAutoBatchFlushEndpoint) {
306+
((AutoBatchFlushEndpoint) endpoint).notifyReconnectFailed(new RedisException(msg));
306307
}
307308
}
308309

309-
private void notifyEndpointFailedToConnectIfNeeded() {
310-
notifyEndpointFailedToConnectIfNeeded(new CancellationException());
310+
private void notifyEndpointFailedToConnectIfNeeded(Throwable t) {
311+
if (useAutoBatchFlushEndpoint) {
312+
((AutoBatchFlushEndpoint) endpoint).notifyReconnectFailed(t);
313+
}
311314
}
312315

313-
private void notifyEndpointFailedToConnectIfNeeded(Exception e) {
316+
private void notifyEndpointFailedToConnectIfNeeded(Supplier<Throwable> throwableSupplier) {
314317
if (useAutoBatchFlushEndpoint) {
315-
((AutoBatchFlushEndpoint) endpoint).notifyReconnectFailed(e);
318+
((AutoBatchFlushEndpoint) endpoint).notifyReconnectFailed(throwableSupplier.get());
316319
}
317320
}
318321

@@ -335,26 +338,29 @@ public void run(int attempt) throws Exception {
335338
* @param delay retry delay.
336339
* @throws Exception when reconnection fails.
337340
*/
338-
private void run(int attempt, Duration delay) throws Exception {
341+
private void run(int attempt, Duration delay) {
339342

340343
reconnectSchedulerSync.set(false);
341344
reconnectScheduleTimeout = null;
342345

343346
if (!isEventLoopGroupActive()) {
344-
logger.debug("isEventLoopGroupActive() == false");
345-
notifyEndpointFailedToConnectIfNeeded();
347+
final String errMsg = "isEventLoopGroupActive() == false";
348+
logger.debug(errMsg);
349+
notifyEndpointFailedToConnectIfNeeded(errMsg);
346350
return;
347351
}
348352

349353
if (!isListenOnChannelInactive()) {
350-
logger.debug("Skip reconnect scheduling, listener disabled");
351-
notifyEndpointFailedToConnectIfNeeded();
354+
final String errMsg = "Skip reconnect scheduling, listener disabled";
355+
logger.debug(errMsg);
356+
notifyEndpointFailedToConnectIfNeeded(errMsg);
352357
return;
353358
}
354359

355360
if (isReconnectSuspended()) {
356-
logger.debug("Skip reconnect scheduling, reconnect is suspended");
357-
notifyEndpointFailedToConnectIfNeeded();
361+
final String msg = "Skip reconnect scheduling, reconnect is suspended";
362+
logger.debug(msg);
363+
notifyEndpointFailedToConnectIfNeeded(msg);
358364
return;
359365
}
360366

@@ -411,7 +417,8 @@ private void run(int attempt, Duration delay) throws Exception {
411417
if (!isReconnectSuspended()) {
412418
scheduleReconnect();
413419
} else {
414-
notifyEndpointFailedToConnectIfNeeded();
420+
notifyEndpointFailedToConnectIfNeeded(
421+
() -> new RedisException("got error and then reconnect is suspended", t));
415422
}
416423
});
417424
} catch (Exception e) {

0 commit comments

Comments
 (0)