@@ -12,10 +12,14 @@ namespace Microsoft.Azure.WebJobs.Script.Grpc
12
12
internal sealed class RetryProxyHandler : DelegatingHandler
13
13
{
14
14
// The maximum number of retries
15
- private readonly int _maxRetries = 3 ;
15
+ internal const int MaxRetries = 10 ;
16
16
17
17
// The initial delay in milliseconds
18
- private readonly int _initialDelay = 50 ;
18
+ internal const int InitialDelay = 50 ;
19
+
20
+ // The maximum delay in milliseconds
21
+ internal const int MaximumDelay = 250 ;
22
+
19
23
private readonly ILogger _logger ;
20
24
21
25
public RetryProxyHandler ( HttpMessageHandler innerHandler , ILogger logger )
@@ -26,32 +30,29 @@ public RetryProxyHandler(HttpMessageHandler innerHandler, ILogger logger)
26
30
27
31
protected override async Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
28
32
{
29
- var currentDelay = _initialDelay ;
30
- for ( int attemptCount = 1 ; attemptCount <= _maxRetries ; attemptCount ++ )
33
+ var currentDelay = InitialDelay ;
34
+ for ( int attemptCount = 1 ; attemptCount <= MaxRetries ; attemptCount ++ )
31
35
{
32
36
try
33
37
{
34
38
return await base . SendAsync ( request , cancellationToken ) ;
35
39
}
36
- catch ( HttpRequestException ) when ( attemptCount < _maxRetries )
40
+ catch ( HttpRequestException ) when ( attemptCount < MaxRetries )
37
41
{
38
- currentDelay *= attemptCount ;
39
-
40
42
_logger . LogWarning ( "Failed to proxy request to the worker. Retrying in {delay}ms. Attempt {attemptCount} of {maxRetries}." ,
41
- currentDelay , attemptCount , _maxRetries ) ;
43
+ currentDelay , attemptCount , MaxRetries ) ;
42
44
43
45
await Task . Delay ( currentDelay , cancellationToken ) ;
46
+
47
+ currentDelay = Math . Min ( currentDelay * 2 , MaximumDelay ) ;
44
48
}
45
49
catch ( Exception ex )
46
50
{
47
- if ( attemptCount == _maxRetries )
48
- {
49
- _logger . LogWarning ( "Reached the maximum retry count for worker request proxying. Error: {exception}" , ex ) ;
50
- }
51
- else
52
- {
53
- _logger . LogWarning ( $ "Unsupported exception type in { nameof ( RetryProxyHandler ) } . Request will not be retried. Exception: {{exception}}", ex ) ;
54
- }
51
+ var message = attemptCount == MaxRetries
52
+ ? "Reached the maximum retry count for worker request proxying. Error: {exception}"
53
+ : $ "Unsupported exception type in { nameof ( RetryProxyHandler ) } . Request will not be retried. Exception: {{exception}}";
54
+
55
+ _logger . LogWarning ( message , ex ) ;
55
56
56
57
throw ;
57
58
}
0 commit comments