@@ -12,25 +12,21 @@ namespace Microsoft.Data.SqlClient
1212 /// <summary>
1313 /// Apply a retry logic on an operation.
1414 /// </summary>
15- public abstract class SqlRetryLogicProvider : SqlRetryLogicBaseProvider
15+ internal class SqlRetryLogicProvider : SqlRetryLogicBaseProvider
1616 {
1717 // safety switch for the preview version
1818 private const string EnableRetryLogicSwitch = "Switch.Microsoft.Data.SqlClient.EnableRetryLogic" ;
19- private bool EnableRetryLogic = false ;
19+ private readonly bool enableRetryLogic = false ;
2020
2121 ///
22- public SqlRetryLogicProvider ( )
22+ public SqlRetryLogicProvider ( SqlRetryLogicBase retryLogic )
2323 {
24- AppContext . TryGetSwitch ( EnableRetryLogicSwitch , out EnableRetryLogic ) ;
25- }
26-
27- private void OnRetrying ( SqlRetryingEventArgs eventArgs )
28- {
29- Retrying ? . Invoke ( this , eventArgs ) ;
24+ AppContext . TryGetSwitch ( EnableRetryLogicSwitch , out enableRetryLogic ) ;
25+ RetryLogic = retryLogic ;
3026 }
3127
3228 ///
33- public override TResult Execute < TResult > ( Func < TResult > function )
29+ public override TResult Execute < TResult > ( object sender , Func < TResult > function )
3430 {
3531 var exceptions = new List < Exception > ( ) ;
3632 retry :
@@ -40,12 +36,13 @@ public override TResult Execute<TResult>(Func<TResult> function)
4036 }
4137 catch ( Exception e )
4238 {
43- if ( EnableRetryLogic && RetryLogic . TransientPredicate ( e ) )
39+ if ( enableRetryLogic && RetryLogic . RetryCondition ( sender ) && RetryLogic . TransientPredicate ( e ) )
4440 {
4541 exceptions . Add ( e ) ;
4642 if ( RetryLogic . TryNextInterval ( out TimeSpan intervalTime ) )
4743 {
48- ApplyRetryEvent ( RetryLogic . Current , intervalTime , exceptions ) ;
44+ // The retrying event raises on each retry.
45+ ApplyRetryingEvent ( sender , RetryLogic . Current , intervalTime , exceptions ) ;
4946
5047 // TODO: log the retried execution and the throttled exception
5148 Thread . Sleep ( intervalTime ) ;
@@ -64,7 +61,7 @@ public override TResult Execute<TResult>(Func<TResult> function)
6461 }
6562
6663 ///
67- public override async Task < TResult > ExecuteAsync < TResult > ( Func < Task < TResult > > function , CancellationToken cancellationToken = default )
64+ public override async Task < TResult > ExecuteAsync < TResult > ( object sender , Func < Task < TResult > > function , CancellationToken cancellationToken = default )
6865 {
6966 var exceptions = new List < Exception > ( ) ;
7067 retry :
@@ -74,12 +71,13 @@ public override async Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> fu
7471 }
7572 catch ( Exception e )
7673 {
77- if ( EnableRetryLogic && RetryLogic . TransientPredicate ( e ) )
74+ if ( enableRetryLogic && RetryLogic . RetryCondition ( sender ) && RetryLogic . TransientPredicate ( e ) )
7875 {
7976 exceptions . Add ( e ) ;
8077 if ( RetryLogic . TryNextInterval ( out TimeSpan intervalTime ) )
8178 {
82- ApplyRetryEvent ( RetryLogic . Current , intervalTime , exceptions ) ;
79+ // The retrying event raises on each retry.
80+ ApplyRetryingEvent ( sender , RetryLogic . Current , intervalTime , exceptions ) ;
8381
8482 // TODO: log the retried execution and the throttled exception
8583 await Task . Delay ( intervalTime , cancellationToken ) ;
@@ -98,7 +96,7 @@ public override async Task<TResult> ExecuteAsync<TResult>(Func<Task<TResult>> fu
9896 }
9997
10098 ///
101- public override async Task ExecuteAsync ( Func < Task > function , CancellationToken cancellationToken = default )
99+ public override async Task ExecuteAsync ( object sender , Func < Task > function , CancellationToken cancellationToken = default )
102100 {
103101 var exceptions = new List < Exception > ( ) ;
104102 retry :
@@ -108,12 +106,13 @@ public override async Task ExecuteAsync(Func<Task> function, CancellationToken c
108106 }
109107 catch ( Exception e )
110108 {
111- if ( EnableRetryLogic && RetryLogic . TransientPredicate ( e ) )
109+ if ( enableRetryLogic && RetryLogic . RetryCondition ( sender ) && RetryLogic . TransientPredicate ( e ) )
112110 {
113111 exceptions . Add ( e ) ;
114112 if ( RetryLogic . TryNextInterval ( out TimeSpan intervalTime ) )
115113 {
116- ApplyRetryEvent ( RetryLogic . Current , intervalTime , exceptions ) ;
114+ // The retrying event raises on each retry.
115+ ApplyRetryingEvent ( sender , RetryLogic . Current , intervalTime , exceptions ) ;
117116
118117 // TODO: log the retried execution and the throttled exception
119118 await Task . Delay ( intervalTime , cancellationToken ) ;
@@ -130,6 +129,8 @@ public override async Task ExecuteAsync(Func<Task> function, CancellationToken c
130129 }
131130 }
132131 }
132+
133+ #region private methods
133134
134135 private Exception CreateException ( IList < Exception > exceptions , bool manualCancellation = false )
135136 {
@@ -148,17 +149,20 @@ private Exception CreateException(IList<Exception> exceptions, bool manualCancel
148149 return new AggregateException ( message , exceptions ) ;
149150 }
150151
151- private void ApplyRetryEvent ( int retryCount , TimeSpan intervalTime , List < Exception > exceptions )
152+ private void OnRetrying ( object sender , SqlRetryingEventArgs eventArgs ) => Retrying ? . Invoke ( sender , eventArgs ) ;
153+
154+ private void ApplyRetryingEvent ( object sender , int retryCount , TimeSpan intervalTime , List < Exception > exceptions )
152155 {
153156 if ( Retrying != null )
154157 {
155158 var retryEventArgs = new SqlRetryingEventArgs ( retryCount - 1 , intervalTime , exceptions ) ;
156- OnRetrying ( retryEventArgs ) ;
159+ OnRetrying ( sender , retryEventArgs ) ;
157160 if ( retryEventArgs . Cancel )
158161 {
159162 throw CreateException ( exceptions , true ) ;
160163 }
161164 }
162165 }
166+ #endregion
163167 }
164168}
0 commit comments