11using Polly ;
22using System ;
3- using System . Collections . Generic ;
43using System . Threading . Tasks ;
54using Titanium . Web . Proxy . Network . Tcp ;
65
@@ -14,10 +13,14 @@ internal class RetryPolicy<T> where T : Exception
1413 private TcpServerConnection currentConnection ;
1514 private Exception exception ;
1615
16+ private Policy policy ;
17+
1718 internal RetryPolicy ( int retries , TcpConnectionFactory tcpConnectionFactory )
1819 {
1920 this . retries = retries ;
2021 this . tcpConnectionFactory = tcpConnectionFactory ;
22+
23+ policy = getRetryPolicy ( ) ;
2124 }
2225
2326 /// <summary>
@@ -31,11 +34,13 @@ internal async Task<RetryResult> ExecuteAsync(Func<TcpServerConnection, Task> ac
3134 Func < Task < TcpServerConnection > > generator , TcpServerConnection initialConnection )
3235 {
3336 currentConnection = initialConnection ;
37+ exception = null ;
38+
3439 try
3540 {
3641 //retry on error with polly policy
3742 //do not use polly context to store connection; it does not save states b/w attempts
38- await getRetryPolicy ( ) . ExecuteAsync ( async ( ) =>
43+ await policy . ExecuteAsync ( async ( ) =>
3944 {
4045 //setup connection
4146 currentConnection = currentConnection as TcpServerConnection ??
@@ -54,17 +59,17 @@ await getRetryPolicy().ExecuteAsync(async () =>
5459 private Policy getRetryPolicy ( )
5560 {
5661 return Policy . Handle < T > ( )
57- . RetryAsync ( retries ,
58- onRetryAsync : async ( ex , i , context ) =>
59- {
60- if ( currentConnection != null )
62+ . RetryAsync ( retries ,
63+ onRetryAsync : async ( ex , i , context ) =>
6164 {
62- //close connection on error
63- await tcpConnectionFactory . Release ( currentConnection , true ) ;
64- currentConnection = null ;
65- }
65+ if ( currentConnection != null )
66+ {
67+ //close connection on error
68+ await tcpConnectionFactory . Release ( currentConnection , true ) ;
69+ currentConnection = null ;
70+ }
6671
67- } ) ;
72+ } ) ;
6873 }
6974 }
7075
0 commit comments