@@ -496,10 +496,10 @@ private Map<String, String> peerProperties() {
496
496
throw new StreamException ("Error when establishing stream connection" , request .error ());
497
497
}
498
498
} catch (StreamException e ) {
499
- outstandingRequests . remove (correlationId );
499
+ this . handleRpcError (correlationId , e );
500
500
throw e ;
501
501
} catch (RuntimeException e ) {
502
- outstandingRequests . remove (correlationId );
502
+ this . handleRpcError (correlationId , e );
503
503
throw new StreamException ("Error while trying to exchange peer properties" , e );
504
504
}
505
505
}
@@ -572,10 +572,10 @@ private SaslAuthenticateResponse sendSaslAuthenticate(
572
572
request .block ();
573
573
return request .response .get ();
574
574
} catch (StreamException e ) {
575
- outstandingRequests . remove (correlationId );
575
+ this . handleRpcError (correlationId , e );
576
576
throw e ;
577
577
} catch (RuntimeException e ) {
578
- outstandingRequests . remove (correlationId );
578
+ this . handleRpcError (correlationId , e );
579
579
throw new StreamException ("Error while trying to authenticate" , e );
580
580
}
581
581
}
@@ -602,10 +602,10 @@ private Map<String, String> open(String virtualHost) {
602
602
}
603
603
return request .response .get ().connectionProperties ;
604
604
} catch (StreamException e ) {
605
- outstandingRequests . remove (correlationId );
605
+ this . handleRpcError (correlationId , e );
606
606
throw e ;
607
607
} catch (RuntimeException e ) {
608
- outstandingRequests . remove (correlationId );
608
+ this . handleRpcError (correlationId , e );
609
609
throw new StreamException ("Error during open command" , e );
610
610
}
611
611
}
@@ -646,10 +646,10 @@ private void sendClose(short code, String reason) {
646
646
+ formatConstant (request .response .get ().getResponseCode ()));
647
647
}
648
648
} catch (StreamException e ) {
649
- outstandingRequests . remove (correlationId );
649
+ this . handleRpcError (correlationId , e );
650
650
throw e ;
651
651
} catch (RuntimeException e ) {
652
- outstandingRequests . remove (correlationId );
652
+ this . handleRpcError (correlationId , e );
653
653
throw new StreamException ("Error while closing connection" , e );
654
654
}
655
655
}
@@ -669,10 +669,10 @@ private List<String> getSaslMechanisms() {
669
669
request .block ();
670
670
return request .response .get ();
671
671
} catch (StreamException e ) {
672
- outstandingRequests . remove (correlationId );
672
+ this . handleRpcError (correlationId , e );
673
673
throw e ;
674
674
} catch (RuntimeException e ) {
675
- outstandingRequests . remove (correlationId );
675
+ this . handleRpcError (correlationId , e );
676
676
throw new StreamException ("Error while exchanging SASL mechanisms" , e );
677
677
}
678
678
}
@@ -699,10 +699,10 @@ public Response create(String stream, Map<String, String> arguments) {
699
699
request .block ();
700
700
return request .response .get ();
701
701
} catch (StreamException e ) {
702
- outstandingRequests . remove (correlationId );
702
+ this . handleRpcError (correlationId , e );
703
703
throw e ;
704
704
} catch (RuntimeException e ) {
705
- outstandingRequests . remove (correlationId );
705
+ this . handleRpcError (correlationId , e );
706
706
throw new StreamException (format ("Error while creating stream '%s'" , stream ), e );
707
707
}
708
708
}
@@ -750,10 +750,10 @@ Response createSuperStream(
750
750
request .block ();
751
751
return request .response .get ();
752
752
} catch (StreamException e ) {
753
- outstandingRequests . remove (correlationId );
753
+ this . handleRpcError (correlationId , e );
754
754
throw e ;
755
755
} catch (RuntimeException e ) {
756
- outstandingRequests . remove (correlationId );
756
+ this . handleRpcError (correlationId , e );
757
757
throw new StreamException (format ("Error while creating super stream '%s'" , superStream ), e );
758
758
}
759
759
}
@@ -776,10 +776,10 @@ Response deleteSuperStream(String superStream) {
776
776
request .block ();
777
777
return request .response .get ();
778
778
} catch (StreamException e ) {
779
- outstandingRequests . remove (correlationId );
779
+ this . handleRpcError (correlationId , e );
780
780
throw e ;
781
781
} catch (RuntimeException e ) {
782
- outstandingRequests . remove (correlationId );
782
+ this . handleRpcError (correlationId , e );
783
783
throw new StreamException (format ("Error while deleting stream '%s'" , superStream ), e );
784
784
}
785
785
}
@@ -860,10 +860,10 @@ public Response delete(String stream) {
860
860
request .block ();
861
861
return request .response .get ();
862
862
} catch (StreamException e ) {
863
- outstandingRequests . remove (correlationId );
863
+ this . handleRpcError (correlationId , e );
864
864
throw e ;
865
865
} catch (RuntimeException e ) {
866
- outstandingRequests . remove (correlationId );
866
+ this . handleRpcError (correlationId , e );
867
867
throw new StreamException (format ("Error while deleting stream '%s'" , stream ), e );
868
868
}
869
869
}
@@ -891,10 +891,10 @@ public Map<String, StreamMetadata> metadata(String... streams) {
891
891
request .block ();
892
892
return request .response .get ();
893
893
} catch (StreamException e ) {
894
- outstandingRequests . remove (correlationId );
894
+ this . handleRpcError (correlationId , e );
895
895
throw e ;
896
896
} catch (RuntimeException e ) {
897
- outstandingRequests . remove (correlationId );
897
+ this . handleRpcError (correlationId , e );
898
898
throw new StreamException (
899
899
format ("Error while getting metadata for stream(s) '%s'" , join ("," , streams )), e );
900
900
}
@@ -930,10 +930,10 @@ public Response declarePublisher(byte publisherId, String publisherReference, St
930
930
request .block ();
931
931
return request .response .get ();
932
932
} catch (StreamException e ) {
933
- outstandingRequests . remove (correlationId );
933
+ this . handleRpcError (correlationId , e );
934
934
throw e ;
935
935
} catch (RuntimeException e ) {
936
- outstandingRequests . remove (correlationId );
936
+ this . handleRpcError (correlationId , e );
937
937
throw new StreamException (
938
938
format ("Error while declaring publisher for stream '%s'" , stream ), e );
939
939
}
@@ -955,10 +955,10 @@ public Response deletePublisher(byte publisherId) {
955
955
request .block ();
956
956
return request .response .get ();
957
957
} catch (StreamException e ) {
958
- outstandingRequests . remove (correlationId );
958
+ this . handleRpcError (correlationId , e );
959
959
throw e ;
960
960
} catch (RuntimeException e ) {
961
- outstandingRequests . remove (correlationId );
961
+ this . handleRpcError (correlationId , e );
962
962
throw new StreamException ("Error while deleting publisher" , e );
963
963
}
964
964
}
@@ -1293,10 +1293,10 @@ public Response subscribe(
1293
1293
request .block ();
1294
1294
return request .response .get ();
1295
1295
} catch (StreamException e ) {
1296
- outstandingRequests . remove (correlationId );
1296
+ this . handleRpcError (correlationId , e );
1297
1297
throw e ;
1298
1298
} catch (RuntimeException e ) {
1299
- outstandingRequests . remove (correlationId );
1299
+ this . handleRpcError (correlationId , e );
1300
1300
throw new StreamException (
1301
1301
format ("Error while trying to subscribe to stream '%s'" , stream ), e );
1302
1302
}
@@ -1351,10 +1351,10 @@ public QueryOffsetResponse queryOffset(String reference, String stream) {
1351
1351
QueryOffsetResponse response = request .response .get ();
1352
1352
return response ;
1353
1353
} catch (StreamException e ) {
1354
- outstandingRequests . remove (correlationId );
1354
+ this . handleRpcError (correlationId , e );
1355
1355
throw e ;
1356
1356
} catch (RuntimeException e ) {
1357
- outstandingRequests . remove (correlationId );
1357
+ this . handleRpcError (correlationId , e );
1358
1358
throw new StreamException (
1359
1359
format (
1360
1360
"Error while querying offset for reference '%s' on stream '%s'" , reference , stream ),
@@ -1397,10 +1397,10 @@ public long queryPublisherSequence(String publisherReference, String stream) {
1397
1397
}
1398
1398
return response .getSequence ();
1399
1399
} catch (StreamException e ) {
1400
- outstandingRequests . remove (correlationId );
1400
+ this . handleRpcError (correlationId , e );
1401
1401
throw e ;
1402
1402
} catch (RuntimeException e ) {
1403
- outstandingRequests . remove (correlationId );
1403
+ this . handleRpcError (correlationId , e );
1404
1404
throw new StreamException (
1405
1405
format (
1406
1406
"Error while querying publisher sequence for '%s' on stream '%s'" ,
@@ -1425,10 +1425,10 @@ public Response unsubscribe(byte subscriptionId) {
1425
1425
request .block ();
1426
1426
return request .response .get ();
1427
1427
} catch (StreamException e ) {
1428
- outstandingRequests . remove (correlationId );
1428
+ this . handleRpcError (correlationId , e );
1429
1429
throw e ;
1430
1430
} catch (RuntimeException e ) {
1431
- outstandingRequests . remove (correlationId );
1431
+ this . handleRpcError (correlationId , e );
1432
1432
throw new StreamException ("Error while unsubscribing" , e );
1433
1433
}
1434
1434
}
@@ -1450,6 +1450,7 @@ void closingSequence(ShutdownContext.ShutdownReason reason) {
1450
1450
this .shutdownListenerCallback .accept (reason );
1451
1451
}
1452
1452
this .nettyClosing .run ();
1453
+ this .failOutstandingRequests ();
1453
1454
if (this .closeDispatchingExecutorService != null ) {
1454
1455
this .closeDispatchingExecutorService .accept (this .dispatchingExecutorService );
1455
1456
}
@@ -1458,6 +1459,24 @@ void closingSequence(ShutdownContext.ShutdownReason reason) {
1458
1459
}
1459
1460
}
1460
1461
1462
+ private void failOutstandingRequests () {
1463
+ try {
1464
+ Exception cause = null ;
1465
+ for (OutstandingRequest <?> request : this .outstandingRequests .values ()) {
1466
+ if (cause == null ) {
1467
+ cause = new ConnectionStreamException ("Connection is closed" );
1468
+ }
1469
+ try {
1470
+ request .completeExceptionally (cause );
1471
+ } catch (Exception e ) {
1472
+ LOGGER .debug ("Error while failing outstanding request: {}" , e .getMessage ());
1473
+ }
1474
+ }
1475
+ } catch (Exception e ) {
1476
+ LOGGER .debug ("Error while failing outstanding requests: {}" , e .getMessage ());
1477
+ }
1478
+ }
1479
+
1461
1480
private void closeNetty () {
1462
1481
try {
1463
1482
if (this .channel != null && this .channel .isOpen ()) {
@@ -1567,10 +1586,10 @@ public List<String> route(String routingKey, String superStream) {
1567
1586
request .block ();
1568
1587
return request .response .get ();
1569
1588
} catch (StreamException e ) {
1570
- outstandingRequests . remove (correlationId );
1589
+ this . handleRpcError (correlationId , e );
1571
1590
throw e ;
1572
1591
} catch (RuntimeException e ) {
1573
- outstandingRequests . remove (correlationId );
1592
+ this . handleRpcError (correlationId , e );
1574
1593
throw new StreamException (
1575
1594
format (
1576
1595
"Error while querying route for routing key '%s' on super stream '%s'" ,
@@ -1600,10 +1619,10 @@ public List<String> partitions(String superStream) {
1600
1619
request .block ();
1601
1620
return request .response .get ();
1602
1621
} catch (StreamException e ) {
1603
- outstandingRequests . remove (correlationId );
1622
+ this . handleRpcError (correlationId , e );
1604
1623
throw e ;
1605
1624
} catch (RuntimeException e ) {
1606
- outstandingRequests . remove (correlationId );
1625
+ this . handleRpcError (correlationId , e );
1607
1626
throw new StreamException (
1608
1627
format ("Error while querying partitions for super stream '%s'" , superStream ), e );
1609
1628
}
@@ -1632,10 +1651,10 @@ List<FrameHandlerInfo> exchangeCommandVersions() {
1632
1651
request .block ();
1633
1652
return request .response .get ();
1634
1653
} catch (StreamException e ) {
1635
- outstandingRequests . remove (correlationId );
1654
+ this . handleRpcError (correlationId , e );
1636
1655
throw e ;
1637
1656
} catch (RuntimeException e ) {
1638
- outstandingRequests . remove (correlationId );
1657
+ this . handleRpcError (correlationId , e );
1639
1658
throw new StreamException ("Error while exchanging command version" , e );
1640
1659
}
1641
1660
}
@@ -1661,10 +1680,10 @@ StreamStatsResponse streamStats(String stream) {
1661
1680
request .block ();
1662
1681
return request .response .get ();
1663
1682
} catch (StreamException e ) {
1664
- outstandingRequests . remove (correlationId );
1683
+ this . handleRpcError (correlationId , e );
1665
1684
throw e ;
1666
1685
} catch (RuntimeException e ) {
1667
- outstandingRequests . remove (correlationId );
1686
+ this . handleRpcError (correlationId , e );
1668
1687
throw new StreamException (
1669
1688
format ("Error while querying statistics for stream '%s'" , stream ), e );
1670
1689
}
@@ -2950,4 +2969,11 @@ private void debug(Supplier<String> format, Object... args) {
2950
2969
LOGGER .debug ("Connection '" + this .clientConnectionName + "': " + format .get (), args );
2951
2970
}
2952
2971
}
2972
+
2973
+ private void handleRpcError (int correlationId , Exception e ) {
2974
+ OutstandingRequest <?> request = this .outstandingRequests .remove (correlationId );
2975
+ if (request != null ) {
2976
+ request .completeExceptionally (e );
2977
+ }
2978
+ }
2953
2979
}
0 commit comments