Skip to content
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

Add API allowing to disable retries for a given list of HTTP methods #5634

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Shared.DiagnosticIds;
using Microsoft.Shared.Diagnostics;
using Polly;

namespace Microsoft.Extensions.Http.Resilience;

/// <summary>
/// Extensions for <see cref="HttpRetryStrategyOptions"/>.
/// </summary>
[Experimental(diagnosticId: DiagnosticIds.Experiments.Resilience, UrlFormat = DiagnosticIds.UrlFormat)]
public static class HttpRetryStrategyOptionsExtensions
{
#if !NET8_0_OR_GREATER
private static readonly HttpMethod _connect = new("CONNECT");
private static readonly HttpMethod _patch = new("PATCH");
#endif

/// <summary>
/// Disables retry attempts for POST, PATCH, PUT, DELETE, and CONNECT HTTP methods.
/// </summary>
/// <param name="options">The retry strategy options.</param>
public static void DisableForUnsafeHttpMethods(this HttpRetryStrategyOptions options)
iliar-turdushev marked this conversation as resolved.
Show resolved Hide resolved
{
options.DisableFor(
HttpMethod.Delete, HttpMethod.Post, HttpMethod.Put,
#if !NET8_0_OR_GREATER
_connect, _patch);
#else
HttpMethod.Connect, HttpMethod.Patch);
#endif
}

/// <summary>
/// Disables retry attempts for the given list of HTTP methods.
/// </summary>
/// <param name="options">The retry strategy options.</param>
/// <param name="methods">The list of HTTP methods.</param>
public static void DisableFor(this HttpRetryStrategyOptions options, params HttpMethod[] methods)
{
_ = Throw.IfNullOrEmpty(methods);

var shouldHandle = Throw.IfNullOrMemberNull(options, options?.ShouldHandle);

options.ShouldHandle = async args =>
{
var result = await shouldHandle(args).ConfigureAwait(args.Context.ContinueOnCapturedContext);

if (result &&
args.Outcome.Result is HttpResponseMessage response &&
response.RequestMessage is HttpRequestMessage request)
iliar-turdushev marked this conversation as resolved.
Show resolved Hide resolved
{
return !methods.Contains(request.Method);
}

return result;
};
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Polly;
using Polly.Retry;
using Xunit;

namespace Microsoft.Extensions.Http.Resilience.Test.Polly;

public class HttpRetryStrategyOptionsExtensionsTests
{
[Fact]
public void DisableFor_RetryOptionsIsNull_Throws()
{
Assert.Throws<ArgumentNullException>(() => ((HttpRetryStrategyOptions)null!).DisableFor(HttpMethod.Get));
}

[Fact]
public void DisableFor_HttpMethodsIsNull_Throws()
{
Assert.Throws<ArgumentNullException>(() => new HttpRetryStrategyOptions().DisableFor(null!));
}

[Fact]
public void DisableFor_HttpMethodsIsEmptry_Throws()
{
Assert.Throws<ArgumentException>(() => new HttpRetryStrategyOptions().DisableFor([]));
}

[Fact]
public void DisableFor_ShouldHandleIsNull_Throws()
{
var options = new HttpRetryStrategyOptions { ShouldHandle = null! };
Assert.Throws<ArgumentException>(() => options.DisableFor(HttpMethod.Get));
}

[Theory]
[InlineData("POST", false)]
[InlineData("DELETE", false)]
[InlineData("GET", true)]
public async Task DisableFor_PositiveScenario(string httpMethod, bool shouldHandle)
{
var options = new HttpRetryStrategyOptions { ShouldHandle = _ => PredicateResult.True() };
options.DisableFor(HttpMethod.Post, HttpMethod.Delete);

using var request = new HttpRequestMessage { Method = new HttpMethod(httpMethod) };
using var response = new HttpResponseMessage { RequestMessage = request };

Assert.Equal(shouldHandle, await options.ShouldHandle(CreatePredicateArguments(response)));
}

[Fact]
public async Task DisableFor_RespectsOriginalShouldHandlePredicate()
{
var options = new HttpRetryStrategyOptions { ShouldHandle = _ => PredicateResult.False() };
options.DisableFor(HttpMethod.Post);

using var request = new HttpRequestMessage { Method = HttpMethod.Get };
using var response = new HttpResponseMessage { RequestMessage = request };

Assert.False(await options.ShouldHandle(CreatePredicateArguments(response)));
}

[Fact]
public async Task DisableFor_ResponseMessageIsNull_DoesNotDisableRetries()
{
var options = new HttpRetryStrategyOptions { ShouldHandle = _ => PredicateResult.True() };
options.DisableFor(HttpMethod.Post);

Assert.True(await options.ShouldHandle(CreatePredicateArguments(null)));
}

[Fact]
public async Task DisableFor_RequestMessageIsNull_DoesNotDisableRetries()
{
var options = new HttpRetryStrategyOptions { ShouldHandle = _ => PredicateResult.True() };
options.DisableFor(HttpMethod.Post);

using var response = new HttpResponseMessage { RequestMessage = null };

Assert.True(await options.ShouldHandle(CreatePredicateArguments(response)));
}

[Theory]
[InlineData("POST", false)]
[InlineData("DELETE", false)]
[InlineData("PUT", false)]
[InlineData("PATCH", false)]
[InlineData("CONNECT", false)]
[InlineData("GET", true)]
[InlineData("HEAD", true)]
[InlineData("TRACE", true)]
[InlineData("OPTIONS", true)]
public async Task DisableForUnsafeHttpMethods_PositiveScenario(string httpMethod, bool shouldHandle)
{
var options = new HttpRetryStrategyOptions { ShouldHandle = _ => PredicateResult.True() };
options.DisableForUnsafeHttpMethods();

using var request = new HttpRequestMessage { Method = new HttpMethod(httpMethod) };
using var response = new HttpResponseMessage { RequestMessage = request };

Assert.Equal(shouldHandle, await options.ShouldHandle(CreatePredicateArguments(response)));
}

private static RetryPredicateArguments<HttpResponseMessage> CreatePredicateArguments(HttpResponseMessage? response)
{
return new RetryPredicateArguments<HttpResponseMessage>(
ResilienceContextPool.Shared.Get(),
Outcome.FromResult(response),
attemptNumber: 1);
}
}
Loading