39
39
//---------------------------------------------------------------------------
40
40
41
41
using RabbitMQ . Client . Events ;
42
- using RabbitMQ . Client . Exceptions ;
43
42
using RabbitMQ . Client . Impl ;
43
+
44
44
using System ;
45
- using System . Collections . Generic ;
46
45
using System . Collections . Concurrent ;
46
+ using System . Collections . Generic ;
47
47
using System . Linq ;
48
48
using System . Threading ;
49
49
using System . Threading . Tasks ;
@@ -93,7 +93,7 @@ public AutorecoveringConnection(ConnectionFactory factory, string clientProvided
93
93
m_factory = factory ;
94
94
this . ClientProvidedName = clientProvidedName ;
95
95
}
96
-
96
+
97
97
public event EventHandler < EventArgs > RecoverySucceeded
98
98
{
99
99
add
@@ -551,12 +551,8 @@ private void Init(IFrameHandler fh)
551
551
{
552
552
m_delegate = new Connection ( m_factory , false ,
553
553
fh , this . ClientProvidedName ) ;
554
-
555
- m_recoveryThread = new Thread ( MainRecoveryLoop )
556
- {
557
- IsBackground = true
558
- } ;
559
- m_recoveryThread . Start ( ) ;
554
+
555
+ m_recoveryTask = Task . Run ( MainRecoveryLoop ) ;
560
556
561
557
EventHandler < ShutdownEventArgs > recoveryListener = ( _ , args ) =>
562
558
{
@@ -587,7 +583,7 @@ public void UpdateSecret(string newSecret, string reason)
587
583
public void Abort ( )
588
584
{
589
585
StopRecoveryLoop ( ) ;
590
- if ( m_delegate . IsOpen )
586
+ if ( m_delegate . IsOpen )
591
587
m_delegate . Abort ( ) ;
592
588
}
593
589
@@ -606,7 +602,7 @@ public void Abort(int timeout)
606
602
if ( m_delegate . IsOpen )
607
603
m_delegate . Abort ( timeout ) ;
608
604
}
609
-
605
+
610
606
///<summary>API-side invocation of connection abort with timeout.</summary>
611
607
public void Abort ( TimeSpan timeout )
612
608
{
@@ -622,7 +618,7 @@ public void Abort(ushort reasonCode, string reasonText, int timeout)
622
618
if ( m_delegate . IsOpen )
623
619
m_delegate . Abort ( reasonCode , reasonText , timeout ) ;
624
620
}
625
-
621
+
626
622
///<summary>API-side invocation of connection abort with timeout.</summary>
627
623
public void Abort ( ushort reasonCode , string reasonText , TimeSpan timeout )
628
624
{
@@ -990,7 +986,7 @@ private bool ShouldTriggerConnectionRecovery(ShutdownEventArgs args)
990
986
// connectivity loss or abrupt shutdown
991
987
args . Initiator == ShutdownInitiator . Library ) ;
992
988
}
993
-
989
+
994
990
private enum RecoveryCommand
995
991
{
996
992
/// <summary>
@@ -1017,7 +1013,7 @@ private enum RecoveryConnectionState
1017
1013
Recovering
1018
1014
}
1019
1015
1020
- private Thread m_recoveryThread ;
1016
+ private Task m_recoveryTask ;
1021
1017
private RecoveryConnectionState m_recoveryLoopState = RecoveryConnectionState . Connected ;
1022
1018
1023
1019
private readonly BlockingCollection < RecoveryCommand > m_recoveryLoopCommandQueue = new BlockingCollection < RecoveryCommand > ( ) ;
@@ -1027,7 +1023,7 @@ private enum RecoveryConnectionState
1027
1023
/// <summary>
1028
1024
/// This is the main loop for the auto-recovery thread.
1029
1025
/// </summary>
1030
- private void MainRecoveryLoop ( )
1026
+ private async Task MainRecoveryLoop ( )
1031
1027
{
1032
1028
try
1033
1029
{
@@ -1036,10 +1032,10 @@ private void MainRecoveryLoop()
1036
1032
switch ( m_recoveryLoopState )
1037
1033
{
1038
1034
case RecoveryConnectionState . Connected :
1039
- RecoveryLoopConnectedHandler ( command ) ;
1035
+ await RecoveryLoopConnectedHandler ( command ) . ConfigureAwait ( false ) ;
1040
1036
break ;
1041
1037
case RecoveryConnectionState . Recovering :
1042
- RecoveryLoopRecoveringHandler ( command ) ;
1038
+ await RecoveryLoopRecoveringHandler ( command ) . ConfigureAwait ( false ) ;
1043
1039
break ;
1044
1040
default :
1045
1041
ESLog . Warn ( "RecoveryLoop state is out of range." ) ;
@@ -1055,6 +1051,7 @@ private void MainRecoveryLoop()
1055
1051
{
1056
1052
ESLog . Error ( "Main recovery loop threw unexpected exception." , e ) ;
1057
1053
}
1054
+
1058
1055
m_recoveryLoopComplete . SetResult ( 0 ) ;
1059
1056
}
1060
1057
@@ -1075,7 +1072,7 @@ private void StopRecoveryLoop()
1075
1072
/// Handles commands when in the Recovering state.
1076
1073
/// </summary>
1077
1074
/// <param name="command"></param>
1078
- private void RecoveryLoopRecoveringHandler ( RecoveryCommand command )
1075
+ private async Task RecoveryLoopRecoveringHandler ( RecoveryCommand command )
1079
1076
{
1080
1077
switch ( command )
1081
1078
{
@@ -1089,7 +1086,8 @@ private void RecoveryLoopRecoveringHandler(RecoveryCommand command)
1089
1086
}
1090
1087
else
1091
1088
{
1092
- Task . Delay ( m_factory . NetworkRecoveryInterval ) . ContinueWith ( t => { m_recoveryLoopCommandQueue . TryAdd ( RecoveryCommand . PerformAutomaticRecovery ) ; } ) ;
1089
+ await Task . Delay ( m_factory . NetworkRecoveryInterval ) ;
1090
+ m_recoveryLoopCommandQueue . TryAdd ( RecoveryCommand . PerformAutomaticRecovery ) ;
1093
1091
}
1094
1092
1095
1093
break ;
@@ -1103,7 +1101,7 @@ private void RecoveryLoopRecoveringHandler(RecoveryCommand command)
1103
1101
/// Handles commands when in the Connected state.
1104
1102
/// </summary>
1105
1103
/// <param name="command"></param>
1106
- private void RecoveryLoopConnectedHandler ( RecoveryCommand command )
1104
+ private async Task RecoveryLoopConnectedHandler ( RecoveryCommand command )
1107
1105
{
1108
1106
switch ( command )
1109
1107
{
@@ -1112,7 +1110,8 @@ private void RecoveryLoopConnectedHandler(RecoveryCommand command)
1112
1110
break ;
1113
1111
case RecoveryCommand . BeginAutomaticRecovery :
1114
1112
m_recoveryLoopState = RecoveryConnectionState . Recovering ;
1115
- Task . Delay ( m_factory . NetworkRecoveryInterval ) . ContinueWith ( t => { m_recoveryLoopCommandQueue . TryAdd ( RecoveryCommand . PerformAutomaticRecovery ) ; } ) ;
1113
+ await Task . Delay ( m_factory . NetworkRecoveryInterval ) . ConfigureAwait ( false ) ;
1114
+ m_recoveryLoopCommandQueue . TryAdd ( RecoveryCommand . PerformAutomaticRecovery ) ;
1116
1115
break ;
1117
1116
default :
1118
1117
ESLog . Warn ( $ "RecoveryLoop command { command } is out of range.") ;
0 commit comments