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 RetryPolicy.Handle Property to Allow for Exception Filtering on Retries #314

Merged
merged 11 commits into from
Jun 3, 2024
9 changes: 8 additions & 1 deletion src/Abstractions/RetryPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.ComponentModel;
using Microsoft.DurableTask.Abstractions;

namespace Microsoft.DurableTask;
Expand Down Expand Up @@ -88,7 +89,9 @@ public RetryPolicy(
this.BackoffCoefficient = backoffCoefficient;
this.MaxRetryInterval = maxRetryInterval ?? TimeSpan.FromHours(1);
this.RetryTimeout = retryTimeout ?? Timeout.InfiniteTimeSpan;
#pragma warning disable CS0618 // Type or member is obsolete
this.Handle = (ex) => true;
#pragma warning restore CS0618 // Type or member is obsolete
}

/// <summary>
Expand Down Expand Up @@ -132,6 +135,8 @@ public RetryPolicy(
/// <value>
/// Defaults delegate that always returns true (i.e., all exceptions are retried).
/// </value>
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This functionality is not implemented. Will be removed in the future. Use TaskOptions.FromRetryHandler or HandleFailure instead.")]
tomseida marked this conversation as resolved.
Show resolved Hide resolved
public Func<Exception, bool> Handle { get; private init; }
jviau marked this conversation as resolved.
Show resolved Hide resolved

#pragma warning disable SA1623 // Property summary documentation should match accessors
Expand All @@ -153,10 +158,11 @@ public RetryPolicy(
/// <see cref="global::DurableTask.Core.Exceptions.TaskFailedException"/> or
/// <see cref="global::DurableTask.Core.Exceptions.SubOrchestrationFailedException"/>.
/// </exception>
public Func<TaskFailureDetails, bool> HandleTaskFailureDetails
public Func<TaskFailureDetails, bool> HandleFailure
{
init
{
#pragma warning disable CS0618 // Type or member is obsolete
this.Handle = ex =>
{
TaskFailureDetails? taskFailureDetails = null;
Expand All @@ -176,6 +182,7 @@ public Func<TaskFailureDetails, bool> HandleTaskFailureDetails

return value.Invoke(taskFailureDetails);
};
#pragma warning restore CS0618 // Type or member is obsolete
}
}
}
2 changes: 2 additions & 0 deletions src/Shared/Core/RetryPolicyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public static CoreRetryOptions ToDurableTaskCoreRetryOptions(this RetryPolicy re
// to TimeSpan.MaxValue when encountered.
static TimeSpan ConvertInfiniteTimeSpans(TimeSpan timeout) =>
timeout == Timeout.InfiniteTimeSpan ? TimeSpan.MaxValue : timeout;
#pragma warning disable CS0618 // Type or member is obsolete
return new CoreRetryOptions(retry.FirstRetryInterval, retry.MaxNumberOfAttempts)
{
BackoffCoefficient = retry.BackoffCoefficient,
MaxRetryInterval = ConvertInfiniteTimeSpans(retry.MaxRetryInterval),
RetryTimeout = ConvertInfiniteTimeSpans(retry.RetryTimeout),
Handle = retry.Handle,
};
#pragma warning restore CS0618 // Type or member is obsolete
}
}
4 changes: 2 additions & 2 deletions test/Grpc.IntegrationTests/OrchestrationErrorHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public async Task RetryActivityFailuresCustomLogicAndPolicy(
backoffCoefficient: 2,
retryTimeout: retryTimeout.HasValue ? TimeSpan.FromMilliseconds(retryTimeout.Value) : null)
{
HandleTaskFailureDetails = taskFailureDetails =>
HandleFailure = taskFailureDetails =>
{
retryHandlerCalls++;
return taskFailureDetails.IsCausedBy(exceptionType) && retryException;
Expand Down Expand Up @@ -355,7 +355,7 @@ public async Task RetrySubOrchestratorFailuresCustomLogicAndPolicy(
backoffCoefficient: 2,
retryTimeout: retryTimeout.HasValue ? TimeSpan.FromMilliseconds(retryTimeout.Value) : null)
{
HandleTaskFailureDetails = taskFailureDetails =>
HandleFailure = taskFailureDetails =>
{
retryHandlerCalls++;
return taskFailureDetails.IsCausedBy(exceptionType) && retryException;
Expand Down