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

Drop some convienience extensions #1283

Merged
merged 1 commit into from
Jun 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,12 @@ public class RetryResilienceStrategyBuilderExtensionsTests

public static readonly TheoryData<Action<ResilienceStrategyBuilder<int>>> OverloadsDataGeneric = new()
{
builder =>
{
builder.AddRetry(retry => retry.HandleResult(10));
AssertStrategy(builder, RetryBackoffType.Constant, 3, TimeSpan.FromSeconds(2));
},
builder =>
{
builder.AddRetry(retry => retry.HandleResult(10), RetryBackoffType.Linear);
AssertStrategy(builder, RetryBackoffType.Linear, 3, TimeSpan.FromSeconds(2));
},
builder =>
{
builder.AddRetry(retry => retry.HandleResult(10), RetryBackoffType.Linear, 2, TimeSpan.FromSeconds(1));
AssertStrategy(builder, RetryBackoffType.Linear, 2, TimeSpan.FromSeconds(1));
},
builder =>
{
builder.AddRetry(retry => retry.HandleResult(10), attempt => TimeSpan.FromMilliseconds(attempt));

AssertStrategy(builder, RetryBackoffType.Constant, 3, TimeSpan.FromSeconds(2), strategy =>
{
var args = new RetryDelayArguments(8, TimeSpan.Zero);
var context = ResilienceContext.Get().Initialize<int>(true);

strategy.DelayGenerator!.HandleAsync<int>(new(context, new Outcome<int>(new InvalidOperationException()), args)).Result.Should().Be(TimeSpan.FromMilliseconds(8));
});
},
builder =>
{
builder.AddRetry(new RetryStrategyOptions<int>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,12 @@ public class TimeoutResilienceStrategyBuilderExtensionsTests
public static IEnumerable<object[]> AddTimeout_Ok_Data()
{
var timeout = TimeSpan.FromSeconds(4);

yield return new object[]
{
timeout,
(ResilienceStrategyBuilder<int> builder) => { builder.AddTimeout(timeout); },
(TimeoutResilienceStrategy strategy) => { GetTimeout(strategy).Should().Be(timeout); }
};

bool called = false;

yield return new object[]
{
timeout,
(ResilienceStrategyBuilder<int> builder) => { builder.AddTimeout(timeout, _=> called = true); },
(TimeoutResilienceStrategy strategy) =>
{
GetTimeout(strategy).Should().Be(timeout);
OnTimeout(strategy);
called.Should().BeTrue();
}
};
}

[MemberData(nameof(TimeoutTestUtils.InvalidTimeouts), MemberType = typeof(TimeoutTestUtils))]
Expand All @@ -38,7 +23,6 @@ public void AddTimeout_InvalidTimeout_EnsureValidated(TimeSpan timeout)
var builder = new ResilienceStrategyBuilder<int>();

Assert.Throws<ValidationException>(() => builder.AddTimeout(timeout));
Assert.Throws<ValidationException>(() => builder.AddTimeout(timeout, args => { }));
}

[MemberData(nameof(AddTimeout_Ok_Data))]
Expand Down Expand Up @@ -79,9 +63,4 @@ private static TimeSpan GetTimeout(TimeoutResilienceStrategy strategy)

return strategy.GenerateTimeoutAsync(ResilienceContext.Get()).Preserve().GetAwaiter().GetResult();
}

private static void OnTimeout(TimeoutResilienceStrategy strategy)
{
strategy.OnTimeout?.Invoke(TimeoutTestUtils.OnTimeoutArguments()).AsTask().Wait();
}
}
78 changes: 0 additions & 78 deletions src/Polly.Core/Retry/RetryResilienceStrategyBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,6 @@ namespace Polly;
/// </summary>
public static class RetryResilienceStrategyBuilderExtensions
{
/// <summary>
/// Adds a retry strategy to the builder.
/// </summary>
/// <typeparam name="TResult">The type of result the retry strategy handles.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="shouldRetry">A predicate that defines the retry conditions.</param>
/// <returns>The builder instance with the retry strategy added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="shouldRetry"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when the options constructed from the arguments are invalid.</exception>
public static ResilienceStrategyBuilder<TResult> AddRetry<TResult>(
this ResilienceStrategyBuilder<TResult> builder,
Action<PredicateBuilder<TResult>> shouldRetry)
{
Guard.NotNull(builder);
Guard.NotNull(shouldRetry);

var options = new RetryStrategyOptions<TResult>();
ConfigureShouldRetry(shouldRetry, options);

return builder.AddRetry(options);
}

/// <summary>
/// Adds a retry strategy to the builder.
/// </summary>
/// <typeparam name="TResult">The type of result the retry strategy handles.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="shouldRetry">A predicate that defines the retry conditions.</param>
/// <param name="retryDelayGenerator">The generator for retry delays. The argument is zero-based attempt number.</param>
/// <returns>The builder instance with the retry strategy added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/>, <paramref name="shouldRetry"/> or <paramref name="retryDelayGenerator"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when the options constructed from the arguments are invalid.</exception>
public static ResilienceStrategyBuilder<TResult> AddRetry<TResult>(
this ResilienceStrategyBuilder<TResult> builder,
Action<PredicateBuilder<TResult>> shouldRetry,
Func<int, TimeSpan> retryDelayGenerator)
{
Guard.NotNull(builder);
Guard.NotNull(shouldRetry);
Guard.NotNull(retryDelayGenerator);

var options = new RetryStrategyOptions<TResult>();
ConfigureShouldRetry(shouldRetry, options);
options.RetryDelayGenerator = args => new ValueTask<TimeSpan>(retryDelayGenerator(args.Arguments.Attempt));

return builder.AddRetry(options);
}

/// <summary>
/// Adds a retry strategy to the builder.
/// </summary>
/// <typeparam name="TResult">The type of result the retry strategy handles.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="shouldRetry">A predicate that defines the retry conditions.</param>
/// <param name="backoffType">The backoff type to use for the retry strategy.</param>
/// <returns>The builder instance with the retry strategy added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="shouldRetry"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when the options constructed from the arguments are invalid.</exception>
public static ResilienceStrategyBuilder<TResult> AddRetry<TResult>(
this ResilienceStrategyBuilder<TResult> builder,
Action<PredicateBuilder<TResult>> shouldRetry,
RetryBackoffType backoffType)
{
Guard.NotNull(builder);
Guard.NotNull(shouldRetry);

var options = new RetryStrategyOptions<TResult>
{
BackoffType = backoffType,
RetryCount = RetryConstants.DefaultRetryCount,
BaseDelay = RetryConstants.DefaultBaseDelay
};

ConfigureShouldRetry(shouldRetry, options);

return builder.AddRetry(options);
}

/// <summary>
/// Adds a retry strategy to the builder.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,6 @@ public static TBuilder AddTimeout<TBuilder>(this TBuilder builder, TimeSpan time
});
}

/// <summary>
/// Adds a timeout resilience strategy to the builder.
/// </summary>
/// <typeparam name="TBuilder">The builder type.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="timeout">The timeout value. This value should be greater than <see cref="TimeSpan.Zero"/> or <see cref="System.Threading.Timeout.InfiniteTimeSpan"/>.</param>
/// <param name="onTimeout">The callback that is executed when timeout happens.</param>
/// <returns>The same builder instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="onTimeout"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when the options produced from the arguments are invalid.</exception>
public static TBuilder AddTimeout<TBuilder>(this TBuilder builder, TimeSpan timeout, Action<OnTimeoutArguments> onTimeout)
where TBuilder : ResilienceStrategyBuilderBase
{
Guard.NotNull(builder);
Guard.NotNull(onTimeout);

builder.AddTimeout(new TimeoutStrategyOptions
{
Timeout = timeout,
OnTimeout = (args) =>
{
onTimeout(args);
return default;
}
});

return builder;
}

/// <summary>
/// Adds a timeout resilience strategy to the builder.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,9 @@ public class RateLimiterResilienceStrategyBuilderExtensionsTests
AssertConcurrencyLimiter(builder, hasEvents: false);
},
builder =>
{
var called = false;

builder.AddConcurrencyLimiter(
new ConcurrencyLimiterOptions
{
PermitLimit = 2,
QueueLimit = 2
},
args => called = true);

AssertConcurrencyLimiter(builder, hasEvents: true);
called.Should().BeTrue();
},
builder =>
{
builder.AddRateLimiter(Mock.Of<RateLimiter>());
AssertRateLimiter(builder, hasEvents: false);
},
builder =>
{
var called = false;
builder.AddRateLimiter(Mock.Of<RateLimiter>(), args => called = true);
AssertRateLimiter(builder, hasEvents: true);
called.Should().BeTrue();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,6 @@ public static TBuilder AddConcurrencyLimiter<TBuilder>(
});
}

/// <summary>
/// Adds the concurrency limiter strategy.
/// </summary>
/// <typeparam name="TBuilder">The builder type.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="options">The concurrency limiter options.</param>
/// <param name="onRejected">The callback that is raised when rate limiter is rejected.</param>
/// <returns>The builder instance with the concurrency limiter strategy added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/>, <paramref name="options"/> or <paramref name="onRejected"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when the options constructed from the arguments are invalid.</exception>
public static TBuilder AddConcurrencyLimiter<TBuilder>(
this TBuilder builder,
ConcurrencyLimiterOptions options,
Action<OnRateLimiterRejectedArguments> onRejected)
where TBuilder : ResilienceStrategyBuilderBase
{
Guard.NotNull(builder);
Guard.NotNull(options);
Guard.NotNull(onRejected);

return builder.AddRateLimiter(new RateLimiterStrategyOptions
{
RateLimiter = new ConcurrencyLimiter(options),
OnRejected = args =>
{
onRejected(args);
return default;
}
});
}

/// <summary>
/// Adds the rate limiter strategy.
/// </summary>
Expand All @@ -112,37 +81,6 @@ public static TBuilder AddRateLimiter<TBuilder>(
});
}

/// <summary>
/// Adds the rate limiter strategy.
/// </summary>
/// <typeparam name="TBuilder">The builder type.</typeparam>
/// <param name="builder">The builder instance.</param>
/// <param name="limiter">The rate limiter to use.</param>
/// <param name="onRejected">The callback that is raised when rate limiter is rejected.</param>
/// <returns>The builder instance with the rate limiter strategy added.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> is <see langword="null"/>.</exception>
/// <exception cref="ValidationException">Thrown when the options constructed from the arguments are invalid.</exception>
public static TBuilder AddRateLimiter<TBuilder>(
this TBuilder builder,
RateLimiter limiter,
Action<OnRateLimiterRejectedArguments> onRejected)
where TBuilder : ResilienceStrategyBuilderBase
{
Guard.NotNull(builder);
Guard.NotNull(limiter);
Guard.NotNull(onRejected);

return builder.AddRateLimiter(new RateLimiterStrategyOptions
{
RateLimiter = limiter,
OnRejected = args =>
{
onRejected(args);
return default;
}
});
}

/// <summary>
/// Adds the rate limiter strategy.
/// </summary>
Expand Down