21
21
22
22
import java .net .SocketAddress ;
23
23
import java .time .Duration ;
24
- import java .util .concurrent .CancellationException ;
25
24
import java .util .concurrent .CompletableFuture ;
26
25
import java .util .concurrent .TimeUnit ;
27
26
import java .util .concurrent .atomic .AtomicBoolean ;
27
+ import java .util .function .Supplier ;
28
28
29
29
import io .lettuce .core .ClientOptions ;
30
30
import io .lettuce .core .ConnectionBuilder ;
31
31
import io .lettuce .core .ConnectionEvents ;
32
+ import io .lettuce .core .RedisException ;
32
33
import io .lettuce .core .event .EventBus ;
33
34
import io .lettuce .core .event .connection .ReconnectAttemptEvent ;
34
35
import io .lettuce .core .event .connection .ReconnectFailedEvent ;
@@ -215,20 +216,11 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
215
216
channel = null ;
216
217
217
218
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
-
228
219
if (!useAutoBatchFlushEndpoint ) {
229
220
this .scheduleReconnect ();
221
+ } else {
222
+ doReconnectOnAutoBatchFlushEndpointQuiescence = this ::scheduleReconnect ;
230
223
}
231
- doReconnectOnAutoBatchFlushEndpointQuiescence = this ::scheduleReconnect ;
232
224
// otherwise, will be called later by BatchFlushEndpoint#onEndpointQuiescence
233
225
} else {
234
226
logger .debug ("{} Reconnect scheduling disabled" , logPrefix (), ctx );
@@ -237,7 +229,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
237
229
super .channelInactive (ctx );
238
230
}
239
231
240
- boolean willReconnect () {
232
+ boolean willReconnectOnAutoBatchFlushEndpointQuiescence () {
241
233
return doReconnectOnAutoBatchFlushEndpointQuiescence != null ;
242
234
}
243
235
@@ -261,14 +253,16 @@ public void scheduleReconnect() {
261
253
logger .debug ("{} scheduleReconnect()" , logPrefix ());
262
254
263
255
if (!isEventLoopGroupActive ()) {
264
- logger .debug ("isEventLoopGroupActive() == false" );
265
- notifyEndpointFailedToConnectIfNeeded ();
256
+ final String errMsg = "isEventLoopGroupActive() == false" ;
257
+ logger .debug (errMsg );
258
+ notifyEndpointFailedToConnectIfNeeded (errMsg );
266
259
return ;
267
260
}
268
261
269
262
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 );
272
266
return ;
273
267
}
274
268
@@ -285,8 +279,9 @@ public void scheduleReconnect() {
285
279
reconnectScheduleTimeout = null ;
286
280
287
281
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 );
290
285
return ;
291
286
}
292
287
@@ -302,17 +297,25 @@ public void scheduleReconnect() {
302
297
}
303
298
} else {
304
299
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 ));
306
307
}
307
308
}
308
309
309
- private void notifyEndpointFailedToConnectIfNeeded () {
310
- notifyEndpointFailedToConnectIfNeeded (new CancellationException ());
310
+ private void notifyEndpointFailedToConnectIfNeeded (Throwable t ) {
311
+ if (useAutoBatchFlushEndpoint ) {
312
+ ((AutoBatchFlushEndpoint ) endpoint ).notifyReconnectFailed (t );
313
+ }
311
314
}
312
315
313
- private void notifyEndpointFailedToConnectIfNeeded (Exception e ) {
316
+ private void notifyEndpointFailedToConnectIfNeeded (Supplier < Throwable > throwableSupplier ) {
314
317
if (useAutoBatchFlushEndpoint ) {
315
- ((AutoBatchFlushEndpoint ) endpoint ).notifyReconnectFailed (e );
318
+ ((AutoBatchFlushEndpoint ) endpoint ).notifyReconnectFailed (throwableSupplier . get () );
316
319
}
317
320
}
318
321
@@ -335,26 +338,29 @@ public void run(int attempt) throws Exception {
335
338
* @param delay retry delay.
336
339
* @throws Exception when reconnection fails.
337
340
*/
338
- private void run (int attempt , Duration delay ) throws Exception {
341
+ private void run (int attempt , Duration delay ) {
339
342
340
343
reconnectSchedulerSync .set (false );
341
344
reconnectScheduleTimeout = null ;
342
345
343
346
if (!isEventLoopGroupActive ()) {
344
- logger .debug ("isEventLoopGroupActive() == false" );
345
- notifyEndpointFailedToConnectIfNeeded ();
347
+ final String errMsg = "isEventLoopGroupActive() == false" ;
348
+ logger .debug (errMsg );
349
+ notifyEndpointFailedToConnectIfNeeded (errMsg );
346
350
return ;
347
351
}
348
352
349
353
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 );
352
357
return ;
353
358
}
354
359
355
360
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 );
358
364
return ;
359
365
}
360
366
@@ -411,7 +417,8 @@ private void run(int attempt, Duration delay) throws Exception {
411
417
if (!isReconnectSuspended ()) {
412
418
scheduleReconnect ();
413
419
} else {
414
- notifyEndpointFailedToConnectIfNeeded ();
420
+ notifyEndpointFailedToConnectIfNeeded (
421
+ () -> new RedisException ("got error and then reconnect is suspended" , t ));
415
422
}
416
423
});
417
424
} catch (Exception e ) {
0 commit comments