@@ -11,8 +11,6 @@ internal class RetryPolicy<T> where T : Exception
1111 private readonly TcpConnectionFactory tcpConnectionFactory ;
1212
1313 private TcpServerConnection currentConnection ;
14- private Exception exception ;
15-
1614 private Policy policy ;
1715
1816 internal RetryPolicy ( int retries , TcpConnectionFactory tcpConnectionFactory )
@@ -26,15 +24,16 @@ internal RetryPolicy(int retries, TcpConnectionFactory tcpConnectionFactory)
2624 /// <summary>
2725 /// Execute and retry the given action until retry number of times.
2826 /// </summary>
29- /// <param name="action">The action to retry.</param>
27+ /// <param name="action">The action to retry with return value specifying whether caller should continue execution .</param>
3028 /// <param name="generator">The Tcp connection generator to be invoked to get new connection for retry.</param>
3129 /// <param name="initialConnection">Initial Tcp connection to use.</param>
3230 /// <returns>Returns the latest connection used and the latest exception if any.</returns>
33- internal async Task < RetryResult > ExecuteAsync ( Func < TcpServerConnection , Task > action ,
31+ internal async Task < RetryResult > ExecuteAsync ( Func < TcpServerConnection , Task < bool > > action ,
3432 Func < Task < TcpServerConnection > > generator , TcpServerConnection initialConnection )
3533 {
3634 currentConnection = initialConnection ;
37- exception = null ;
35+ Exception exception = null ;
36+ bool @continue = true ;
3837
3938 try
4039 {
@@ -46,30 +45,32 @@ await policy.ExecuteAsync(async () =>
4645 currentConnection = currentConnection as TcpServerConnection ??
4746 await generator ( ) ;
4847 //try
49- await action ( currentConnection ) ;
48+ @continue = await action ( currentConnection ) ;
5049
5150 } ) ;
5251 }
5352 catch ( Exception e ) { exception = e ; }
5453
55- return new RetryResult ( currentConnection , exception ) ;
54+ return new RetryResult ( currentConnection , exception , @continue ) ;
5655 }
5756
5857 //get the policy
5958 private Policy getRetryPolicy ( )
6059 {
6160 return Policy . Handle < T > ( )
6261 . RetryAsync ( retries ,
63- onRetryAsync : async ( ex , i , context ) =>
64- {
65- if ( currentConnection != null )
66- {
67- //close connection on error
68- await tcpConnectionFactory . Release ( currentConnection , true ) ;
69- currentConnection = null ;
70- }
62+ onRetryAsync : onRetry ) ;
63+ }
7164
72- } ) ;
65+ //before retry clear connection
66+ private async Task onRetry ( Exception ex , int attempt )
67+ {
68+ if ( currentConnection != null )
69+ {
70+ //close connection on error
71+ await tcpConnectionFactory . Release ( currentConnection , true ) ;
72+ currentConnection = null ;
73+ }
7374 }
7475 }
7576
@@ -78,11 +79,13 @@ internal class RetryResult
7879 internal bool IsSuccess => Exception == null ;
7980 internal TcpServerConnection LatestConnection { get ; }
8081 internal Exception Exception { get ; }
82+ internal bool Continue { get ; }
8183
82- internal RetryResult ( TcpServerConnection lastConnection , Exception exception )
84+ internal RetryResult ( TcpServerConnection lastConnection , Exception exception , bool @continue )
8385 {
8486 LatestConnection = lastConnection ;
8587 Exception = exception ;
88+ Continue = @continue ;
8689 }
8790 }
8891}
0 commit comments