Skip to content

Commit e4e7dff

Browse files
committed
Limiting retry behaviour to certain 50x errors (+1 squashed commits)
Squashed commits: [2ef4f3e] Fixed type in test name
1 parent 4478cba commit e4e7dff

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/SendGrid/Reliability/RetryDelegatingHandler.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace SendGrid.Helpers.Reliability
22
{
33
using System;
4+
using System.Collections.Generic;
45
using System.Net.Http;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -10,6 +11,8 @@
1011
/// </summary>
1112
public class RetryDelegatingHandler : DelegatingHandler
1213
{
14+
private static List<int> retriableStatusCodes = new List<int>() { 500, 502, 503, 504 };
15+
1316
private readonly ReliabilitySettings settings;
1417

1518
/// <summary>
@@ -44,7 +47,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
4447
{
4548
responseMessage = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
4649

47-
ThrowHttpRequestExceptionIfResponseIsWithinTheServerErrorRange(responseMessage);
50+
ThrowHttpRequestExceptionIfResponseCodeIsRetriable(responseMessage);
4851

4952
sent = true;
5053
}
@@ -76,11 +79,13 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
7679
return responseMessage;
7780
}
7881

79-
private static void ThrowHttpRequestExceptionIfResponseIsWithinTheServerErrorRange(HttpResponseMessage responseMessage)
82+
private static void ThrowHttpRequestExceptionIfResponseCodeIsRetriable(HttpResponseMessage responseMessage)
8083
{
81-
if ((int)responseMessage.StatusCode >= 500 && (int)responseMessage.StatusCode <= 504)
84+
int statusCode = (int)responseMessage.StatusCode;
85+
86+
if (retriableStatusCodes.Contains(statusCode))
8287
{
83-
throw new HttpRequestException(string.Format("Response Http Status code {0} indicates server error", responseMessage.StatusCode));
88+
throw new HttpRequestException(string.Format("Http status code '{0}' indicates server error", statusCode));
8489
}
8590
}
8691
}

tests/SendGrid.Tests/Reliability/RetryDelegatingHandlerTests.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,23 @@ public async Task Invoke_ShouldReturnErrorWithoutRetryWhen500ErrorStatusIsNotTra
6565

6666
var response = await client.SendAsync(new HttpRequestMessage());
6767

68-
Assert.Equal((HttpStatusCode)505, response.StatusCode);
68+
Assert.Equal(HttpStatusCode.HttpVersionNotSupported, response.StatusCode);
6969
Assert.Equal(1, innerHandler.InvocationCount);
7070
}
7171

7272
[Fact]
73-
public async Task Invoke_ShoulddRetryOnceWhenFailedOnFirstAttemptThenSuccessful()
73+
public async Task Invoke_ShouldReturnErrorWithoutRetryWhen501ErrorStatus()
74+
{
75+
innerHandler.AddBehaviour(innerHandler.NotImplemented);
76+
77+
var response = await client.SendAsync(new HttpRequestMessage());
78+
79+
Assert.Equal(HttpStatusCode.NotImplemented, response.StatusCode);
80+
Assert.Equal(1, innerHandler.InvocationCount);
81+
}
82+
83+
[Fact]
84+
public async Task Invoke_ShouldRetryOnceWhenFailedOnFirstAttemptThenSuccessful()
7485
{
7586
innerHandler.AddBehaviour(innerHandler.TaskCancelled);
7687
innerHandler.AddBehaviour(innerHandler.OK);
@@ -96,7 +107,7 @@ public async Task Invoke_ShouldRetryTheExpectedAmountOfTimesAndReturnTimeoutExce
96107
public async Task Invoke_ShouldRetryTheExpectedAmountOfTimesAndReturnExceptionWhenInternalServerErrorsEncountered()
97108
{
98109
innerHandler.AddBehaviour(innerHandler.InternalServerError);
99-
innerHandler.AddBehaviour(innerHandler.InternalServerError);
110+
innerHandler.AddBehaviour(innerHandler.ServiceUnavailable);
100111

101112
await Assert.ThrowsAsync<HttpRequestException>(() => client.SendAsync(new HttpRequestMessage()));
102113

tests/SendGrid.Tests/Reliability/RetryTestBehaviourDelegatingHandler.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,22 @@ public Task<HttpResponseMessage> InternalServerError()
3838
{
3939
return CreateHttpResponse(HttpStatusCode.InternalServerError);
4040
}
41+
public Task<HttpResponseMessage> ServiceUnavailable()
42+
{
43+
return CreateHttpResponse(HttpStatusCode.ServiceUnavailable);
44+
}
4145

4246
public Task<HttpResponseMessage> AuthenticationError()
4347
{
4448
return CreateHttpResponse(HttpStatusCode.Unauthorized);
4549
}
4650
public Task<HttpResponseMessage> HttpVersionNotSupported()
4751
{
48-
return CreateHttpResponse((HttpStatusCode)505);
52+
return CreateHttpResponse(HttpStatusCode.HttpVersionNotSupported);
53+
}
54+
public Task<HttpResponseMessage> NotImplemented()
55+
{
56+
return CreateHttpResponse(HttpStatusCode.NotImplemented);
4957
}
5058

5159
public Task<HttpResponseMessage> CreateHttpResponse(HttpStatusCode statusCode)

0 commit comments

Comments
 (0)