54
54
import reactor .core .publisher .Sinks ;
55
55
import reactor .core .scheduler .Schedulers ;
56
56
import reactor .netty .Connection ;
57
+ import reactor .netty .channel .AbortedException ;
57
58
import reactor .netty .tcp .TcpClient ;
58
59
import reactor .util .Logger ;
59
60
import reactor .util .Loggers ;
@@ -496,6 +497,11 @@ private void handleClose() {
496
497
}
497
498
498
499
private void handleConnectionError (Throwable error ) {
500
+
501
+ if (AbortedException .isConnectionReset (error ) && !isConnected ()) {
502
+ drainError (() -> this .messageSubscriber .createClientClosedException (error ));
503
+ }
504
+
499
505
drainError (() -> new PostgresConnectionException (error ));
500
506
}
501
507
@@ -535,6 +541,10 @@ public PostgresConnectionClosedException(String reason) {
535
541
super (reason );
536
542
}
537
543
544
+ public PostgresConnectionClosedException (String reason , @ Nullable Throwable cause ) {
545
+ super (reason , cause );
546
+ }
547
+
538
548
}
539
549
540
550
static class PostgresConnectionException extends R2dbcNonTransientResourceException {
@@ -670,7 +680,7 @@ private class BackendMessageSubscriber implements CoreSubscriber<BackendMessage>
670
680
671
681
private Subscription upstream ;
672
682
673
- public Flux <BackendMessage > addConversation (Predicate <BackendMessage > takeUntil , Publisher <FrontendMessage > requests , Consumer <Flux <FrontendMessage >> sender ,
683
+ public Flux <BackendMessage > addConversation (Predicate <BackendMessage > takeUntil , Publisher <FrontendMessage > requests , Consumer <Publisher <FrontendMessage >> sender ,
674
684
Supplier <Boolean > isConnected ) {
675
685
676
686
return Flux .create (sink -> {
@@ -688,13 +698,7 @@ public Flux<BackendMessage> addConversation(Predicate<BackendMessage> takeUntil,
688
698
return ;
689
699
}
690
700
691
- Flux <FrontendMessage > requestMessages = Flux .from (requests ).doOnNext (m -> {
692
- if (!isConnected .get ()) {
693
- sink .error (new PostgresConnectionClosedException ("Cannot exchange messages because the connection is closed" ));
694
- }
695
- });
696
-
697
- sender .accept (requestMessages );
701
+ sender .accept (requests );
698
702
} else {
699
703
sink .error (new RequestQueueException ("Cannot exchange messages because the request queue limit is exceeded" ));
700
704
}
@@ -703,7 +707,11 @@ public Flux<BackendMessage> addConversation(Predicate<BackendMessage> takeUntil,
703
707
}
704
708
705
709
PostgresConnectionClosedException createClientClosedException () {
706
- return new PostgresConnectionClosedException ("Cannot exchange messages because the connection is closed" );
710
+ return createClientClosedException (null );
711
+ }
712
+
713
+ PostgresConnectionClosedException createClientClosedException (@ Nullable Throwable cause ) {
714
+ return new PostgresConnectionClosedException ("Cannot exchange messages because the connection is closed" , cause );
707
715
}
708
716
709
717
/**
0 commit comments