Skip to content

feat: make RetriableServerErrorStatusCodes public #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/SendGrid/Reliability/ReliabilitySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Net;

namespace SendGrid.Helpers.Reliability
{
Expand Down Expand Up @@ -89,5 +91,21 @@ public ReliabilitySettings(int maximumNumberOfRetries, TimeSpan minimumBackoff,
/// Gets the value that will be used to calculate a random delta in the exponential delay between retries. Defaults to 1 second.
/// </summary>
public TimeSpan DeltaBackOff { get; }

/// <summary>
/// Gets status codes for which request would be retied.
/// </summary>
public List<HttpStatusCode> RetriableServerErrorStatusCodes { get; } = new List<HttpStatusCode>(DefaultRetriableServerErrorStatusCodes);

/// <summary>
/// Gets default status codes for which request would be retied.
/// </summary>
public static List<HttpStatusCode> DefaultRetriableServerErrorStatusCodes { get; } = new List<HttpStatusCode>()
{
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.ServiceUnavailable,
HttpStatusCode.GatewayTimeout,
};
}
}
18 changes: 4 additions & 14 deletions src/SendGrid/Reliability/RetryDelegatingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -17,15 +16,6 @@ namespace SendGrid.Helpers.Reliability
/// </summary>
public class RetryDelegatingHandler : DelegatingHandler
{
private static readonly List<HttpStatusCode> RetriableServerErrorStatusCodes =
new List<HttpStatusCode>()
{
HttpStatusCode.InternalServerError,
HttpStatusCode.BadGateway,
HttpStatusCode.ServiceUnavailable,
HttpStatusCode.GatewayTimeout,
};

private readonly ReliabilitySettings settings;

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

ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(responseMessage);
this.ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(responseMessage);

sent = true;
}
Expand Down Expand Up @@ -101,9 +91,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
return responseMessage;
}

private static void ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(HttpResponseMessage responseMessage)
private void ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(HttpResponseMessage responseMessage)
{
if (RetriableServerErrorStatusCodes.Contains(responseMessage.StatusCode))
if (this.settings.RetriableServerErrorStatusCodes.Contains(responseMessage.StatusCode))
{
throw new HttpRequestException(string.Format("Http status code '{0}' indicates server error", responseMessage.StatusCode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void ShouldPassValidValuesFromDefaultConstruct()
Assert.Equal(TimeSpan.Zero, defaultSettings.MinimumBackOff);
Assert.Equal(TimeSpan.Zero, defaultSettings.DeltaBackOff);
Assert.Equal(0, defaultSettings.MaximumNumberOfRetries);
Assert.Equal(ReliabilitySettings.DefaultRetriableServerErrorStatusCodes, defaultSettings.RetriableServerErrorStatusCodes);
}

[Fact]
Expand Down