Skip to content

Commit 9088127

Browse files
authored
feat: make RetriableServerErrorStatusCodes public (#1156)
1 parent 2afcd7f commit 9088127

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/SendGrid/Reliability/ReliabilitySettings.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// </copyright>
55

66
using System;
7+
using System.Collections.Generic;
8+
using System.Net;
79

810
namespace SendGrid.Helpers.Reliability
911
{
@@ -89,5 +91,21 @@ public ReliabilitySettings(int maximumNumberOfRetries, TimeSpan minimumBackoff,
8991
/// Gets the value that will be used to calculate a random delta in the exponential delay between retries. Defaults to 1 second.
9092
/// </summary>
9193
public TimeSpan DeltaBackOff { get; }
94+
95+
/// <summary>
96+
/// Gets status codes for which request would be retied.
97+
/// </summary>
98+
public List<HttpStatusCode> RetriableServerErrorStatusCodes { get; } = new List<HttpStatusCode>(DefaultRetriableServerErrorStatusCodes);
99+
100+
/// <summary>
101+
/// Gets default status codes for which request would be retied.
102+
/// </summary>
103+
public static List<HttpStatusCode> DefaultRetriableServerErrorStatusCodes { get; } = new List<HttpStatusCode>()
104+
{
105+
HttpStatusCode.InternalServerError,
106+
HttpStatusCode.BadGateway,
107+
HttpStatusCode.ServiceUnavailable,
108+
HttpStatusCode.GatewayTimeout,
109+
};
92110
}
93111
}

src/SendGrid/Reliability/RetryDelegatingHandler.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
// </copyright>
55

66
using System;
7-
using System.Collections.Generic;
8-
using System.Net;
7+
using System.Linq;
98
using System.Net.Http;
109
using System.Threading;
1110
using System.Threading.Tasks;
@@ -17,15 +16,6 @@ namespace SendGrid.Helpers.Reliability
1716
/// </summary>
1817
public class RetryDelegatingHandler : DelegatingHandler
1918
{
20-
private static readonly List<HttpStatusCode> RetriableServerErrorStatusCodes =
21-
new List<HttpStatusCode>()
22-
{
23-
HttpStatusCode.InternalServerError,
24-
HttpStatusCode.BadGateway,
25-
HttpStatusCode.ServiceUnavailable,
26-
HttpStatusCode.GatewayTimeout,
27-
};
28-
2919
private readonly ReliabilitySettings settings;
3020

3121
/// <summary>
@@ -69,7 +59,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
6959
{
7060
responseMessage = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
7161

72-
ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(responseMessage);
62+
this.ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(responseMessage);
7363

7464
sent = true;
7565
}
@@ -101,9 +91,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
10191
return responseMessage;
10292
}
10393

104-
private static void ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(HttpResponseMessage responseMessage)
94+
private void ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(HttpResponseMessage responseMessage)
10595
{
106-
if (RetriableServerErrorStatusCodes.Contains(responseMessage.StatusCode))
96+
if (this.settings.RetriableServerErrorStatusCodes.Contains(responseMessage.StatusCode))
10797
{
10898
throw new HttpRequestException(string.Format("Http status code '{0}' indicates server error", responseMessage.StatusCode));
10999
}

tests/SendGrid.Tests/Reliability/ReliabilitySettingsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public void ShouldPassValidValuesFromDefaultConstruct()
9999
Assert.Equal(TimeSpan.Zero, defaultSettings.MinimumBackOff);
100100
Assert.Equal(TimeSpan.Zero, defaultSettings.DeltaBackOff);
101101
Assert.Equal(0, defaultSettings.MaximumNumberOfRetries);
102+
Assert.Equal(ReliabilitySettings.DefaultRetriableServerErrorStatusCodes, defaultSettings.RetriableServerErrorStatusCodes);
102103
}
103104

104105
[Fact]

0 commit comments

Comments
 (0)