@@ -79,7 +79,7 @@ public async Task ConnectSessionAsync(
7979 }
8080 catch ( UnauthorizedAccessException uaex ) // Tunnel access token validation failed.
8181 {
82- if ( ! IsRetryAllowed ( uaex , SshDisconnectReason . AuthCancelledByUser , delayNeeded : false ) )
82+ if ( ! IsRetryAllowed ( uaex , attempt , SshDisconnectReason . AuthCancelledByUser , delayNeeded : false ) )
8383 {
8484 throw ;
8585 }
@@ -88,7 +88,7 @@ public async Task ConnectSessionAsync(
8888 }
8989 catch ( SshReconnectException srex )
9090 {
91- if ( ! IsRetryAllowed ( srex , SshDisconnectReason . ProtocolError , delayNeeded : false ) )
91+ if ( ! IsRetryAllowed ( srex , attempt , SshDisconnectReason . ProtocolError , delayNeeded : false ) )
9292 {
9393 throw ;
9494 }
@@ -99,7 +99,7 @@ public async Task ConnectSessionAsync(
9999 when ( scex . DisconnectReason == SshDisconnectReason . ConnectionLost )
100100 {
101101 // Recoverable
102- if ( ! IsRetryAllowed ( scex , scex . DisconnectReason ) )
102+ if ( ! IsRetryAllowed ( scex , attempt , scex . DisconnectReason ) )
103103 {
104104 throw ;
105105 }
@@ -127,7 +127,7 @@ public async Task ConnectSessionAsync(
127127 $ "Unauthorized (401). Provide a fresh tunnel access token with '{ this . relayClient . TunnelAccessScope } ' scope.",
128128 wse ) ;
129129
130- ThrowIfRetryNotAllowed ( exception , SshDisconnectReason . AuthCancelledByUser , delayNeeded : false ) ;
130+ ThrowIfRetryNotAllowed ( exception , attempt , SshDisconnectReason . AuthCancelledByUser , delayNeeded : false ) ;
131131
132132 // Unauthorized error may happen when the tunnel access token is no longer valid, e.g. expired.
133133 // Try refreshing it.
@@ -166,7 +166,7 @@ public async Task ConnectSessionAsync(
166166 attemptDelayMs = RetryMaxDelayMs / 2 ;
167167 }
168168
169- if ( ! IsRetryAllowed ( exception , SshDisconnectReason . ServiceNotAvailable ) ||
169+ if ( ! IsRetryAllowed ( exception , attempt , SshDisconnectReason . ServiceNotAvailable ) ||
170170 attempt > 3 )
171171 {
172172 throw exception ;
@@ -181,7 +181,7 @@ public async Task ConnectSessionAsync(
181181 }
182182
183183 // Other web socket errors may be recoverable
184- else if ( ! IsRetryAllowed ( wse ) )
184+ else if ( ! IsRetryAllowed ( wse , attempt ) )
185185 {
186186 throw ;
187187 }
@@ -222,7 +222,7 @@ ex is ArgumentNullException ||
222222 throw ;
223223 }
224224
225- if ( ! IsRetryAllowed ( ex , SshDisconnectReason . ProtocolError ) )
225+ if ( ! IsRetryAllowed ( ex , attempt , SshDisconnectReason . ProtocolError ) )
226226 {
227227 throw ;
228228 }
@@ -265,8 +265,8 @@ ex is ArgumentNullException ||
265265 }
266266 }
267267
268- var retryTiming = isDelayNeeded ? $ " in { ( attemptDelayMs < 1000 ? $ "0.{ attemptDelayMs / 100 } s" : $ "{ attemptDelayMs / 1000 } s") } " : string . Empty ;
269- Trace . Verbose ( $ "Error connecting to tunnel SSH session, retrying{ retryTiming } { ( errorDescription != null ? $ ": { errorDescription } " : string . Empty ) } ") ;
268+ var retryTiming = isDelayNeeded ? $ "{ ( attemptDelayMs < 1000 ? $ "0.{ attemptDelayMs / 100 } s" : $ "{ attemptDelayMs / 1000 } s") } " : string . Empty ;
269+ Trace . Verbose ( $ "Error connecting to tunnel SSH session, retrying in { retryTiming } { ( errorDescription != null ? $ ": { errorDescription } " : string . Empty ) } ") ;
270270
271271 if ( isDelayNeeded )
272272 {
@@ -361,30 +361,28 @@ async Task RefreshTunnelAccessTokenAsync(Exception exception)
361361 return null ;
362362 }
363363
364- void ThrowIfRetryNotAllowed ( Exception ex , SshDisconnectReason reason , bool delayNeeded = true )
364+ void ThrowIfRetryNotAllowed ( Exception ex , int attemptNumber , SshDisconnectReason reason , bool delayNeeded = true )
365365 {
366- if ( ! IsRetryAllowed ( ex , reason , delayNeeded ) )
366+ if ( ! IsRetryAllowed ( ex , attemptNumber , reason , delayNeeded ) )
367367 {
368368 throw ex ;
369369 }
370370 }
371371
372372 bool IsRetryAllowed (
373373 Exception ex ,
374+ int attemptNumber ,
374375 SshDisconnectReason reason = SshDisconnectReason . ConnectionLost ,
375376 bool delayNeeded = true )
376377 {
377378 disconnectReason = reason ;
378379 errorDescription = ex . Message ;
379380 exception = ex ;
380- if ( options ? . EnableRetry == false )
381- {
382- return false ;
383- }
384381
385382 isDelayNeeded = delayNeeded ;
386383 var retryDelay = TimeSpan . FromMilliseconds ( isDelayNeeded ? attemptDelayMs : 0 ) ;
387- var retryingArgs = new RetryingTunnelConnectionEventArgs ( ex , retryDelay ) ;
384+ var retryingArgs = new RetryingTunnelConnectionEventArgs ( ex , attemptNumber , retryDelay ) ;
385+ retryingArgs . Retry = options ? . EnableRetry ?? true ;
388386 this . relayClient . OnRetrying ( retryingArgs ) ;
389387 if ( ! retryingArgs . Retry )
390388 {
0 commit comments