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

Improve the docs and behavior around infinite retries #1574

Merged
merged 1 commit into from
Sep 8, 2023
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
2 changes: 1 addition & 1 deletion README_V8.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions
}
});

// To keep retrying indefinitely until successful
// To keep retrying indefinitely or until success use int.MaxValue.
new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = int.MaxValue,
Expand Down
2 changes: 1 addition & 1 deletion docs/strategies/retry.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions
}
});

// To keep retrying indefinitely until successful
// To keep retrying indefinitely or until success use int.MaxValue.
new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = int.MaxValue,
Expand Down
19 changes: 16 additions & 3 deletions src/Polly.Core/Retry/RetryResilienceStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected internal override async ValueTask<Outcome<T>> ExecuteCore<TState>(Func

TelemetryUtil.ReportExecutionAttempt(_telemetry, context, outcome, attempt, executionTime, handle);

if (context.CancellationToken.IsCancellationRequested || IsLastAttempt(attempt) || !handle)
if (context.CancellationToken.IsCancellationRequested || IsLastAttempt(attempt, out bool incrementAttempts) || !handle)
{
return outcome;
}
Expand Down Expand Up @@ -98,9 +98,22 @@ protected internal override async ValueTask<Outcome<T>> ExecuteCore<TState>(Func
}
}

attempt++;
if (incrementAttempts)
{
attempt++;
}
}
}

private bool IsLastAttempt(int attempt) => attempt >= RetryCount;
internal bool IsLastAttempt(int attempt, out bool incrementAttempts)
{
if (attempt == int.MaxValue)
{
incrementAttempts = false;
return false;
}

incrementAttempts = true;
return attempt >= RetryCount;
}
}
3 changes: 3 additions & 0 deletions src/Polly.Core/Retry/RetryStrategyOptions.TResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class RetryStrategyOptions<TResult> : ResilienceStrategyOptions
/// <value>
/// The default value is 3 retries.
/// </value>
/// <remarks>
/// To retry indefinitely use <see cref="int.MaxValue"/>. Note that the reported attempt number is capped at <see cref="int.MaxValue"/>.
/// </remarks>
[Range(1, RetryConstants.MaxRetryCount)]
public int MaxRetryAttempts { get; set; } = RetryConstants.DefaultRetryCount;

Expand Down
2 changes: 1 addition & 1 deletion src/Snippets/Docs/Retry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void Usage()
}
});

// To keep retrying indefinitely until successful
// To keep retrying indefinitely or until success use int.MaxValue.
new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions
{
MaxRetryAttempts = int.MaxValue,
Expand Down
10 changes: 10 additions & 0 deletions test/Polly.Core.Tests/Retry/RetryResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Time.Testing;
using Polly.Retry;
using Polly.Telemetry;
using Polly.Testing;

namespace Polly.Core.Tests.Retry;

Expand Down Expand Up @@ -190,6 +191,15 @@ public async Task RetryDelayGenerator_ZeroDelay_NoTimeProviderCalls()
generatedValues.Should().Be(3);
}

[Fact]
public void IsLastAttempt_Ok()
{
var sut = (RetryResilienceStrategy<object>)CreateSut().GetPipelineDescriptor().FirstStrategy.StrategyInstance;

sut.IsLastAttempt(int.MaxValue, out var increment).Should().BeFalse();
increment.Should().BeFalse();
}

private sealed class ThrowingFakeTimeProvider : FakeTimeProvider
{
public override DateTimeOffset GetUtcNow() => throw new AssertionFailedException("TimeProvider should not be used.");
Expand Down