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

Use cancellation in async Retrier methods. #65

Merged
merged 1 commit into from
Feb 3, 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 src/SenseNet.Tools/Tools/Retrier/DefaultRetrier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Task<T> RetryAsync<T>(int count, int waitMilliseconds, Func<Task<T>> acti
}

return true;
});
}, cancel);
}
}
}
33 changes: 25 additions & 8 deletions src/SenseNet.Tools/Tools/Retrier/Retrier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using SenseNet.Diagnostics;

Expand All @@ -25,6 +26,7 @@ public static class Retrier
/// <param name="caughtExceptionType">Type of exception that is suppressed and triggers the next attempt.</param>
/// <param name="callback">Void, parameterless method that the retrier executes.</param>
[SuppressMessage("ReSharper", "CommentTypo")]
[Obsolete("Use the async methods in the IRetrier service instead.")]
public static void Retry(int count, int waitMilliseconds, Type caughtExceptionType, Action callback)
{
var retryCount = count;
Expand All @@ -46,7 +48,7 @@ public static void Retry(int count, int waitMilliseconds, Type caughtExceptionTy
SnTrace.System.Write($"Retrier caught an exception (countdown: {retryCount}): {e.GetType().Name}: {e.Message}");
lastException = e;
retryCount--;
System.Threading.Thread.Sleep(waitMilliseconds);
Thread.Sleep(waitMilliseconds);
}
}

Expand All @@ -66,6 +68,7 @@ public static void Retry(int count, int waitMilliseconds, Type caughtExceptionTy
/// <param name="callback">Parameterless method with T return type.</param>
/// <returns>Result of the callback method.</returns>
// ReSharper disable once UnusedMember.Global
[Obsolete("Use the async methods in the IRetrier service instead.")]
public static T Retry<T>(int count, int waitMilliseconds, Type caughtExceptionType, Func<T> callback)
{
var retryCount = count;
Expand All @@ -86,7 +89,7 @@ public static T Retry<T>(int count, int waitMilliseconds, Type caughtExceptionTy
SnTrace.System.Write($"Retrier caught an exception (countdown: {retryCount}): {e.GetType().Name}: {e.Message}");
lastException = e;
retryCount--;
System.Threading.Thread.Sleep(waitMilliseconds);
Thread.Sleep(waitMilliseconds);
}
}

Expand All @@ -111,6 +114,7 @@ public static T Retry<T>(int count, int waitMilliseconds, Type caughtExceptionTy
/// If the decider method returns with true, the main method returns immediately. Otherwise the next
/// attempt will be performed.
/// </param>
[Obsolete("Use the async methods in the IRetrier service instead.")]
public static void Retry(int count, int waitMilliseconds, Action callback, Func<int, Exception, bool> checkCondition)
{
var retryCount = count;
Expand All @@ -135,7 +139,7 @@ public static void Retry(int count, int waitMilliseconds, Action callback, Func<
break;

retryCount--;
System.Threading.Thread.Sleep(waitMilliseconds);
Thread.Sleep(waitMilliseconds);
}
}

Expand All @@ -156,6 +160,7 @@ public static void Retry(int count, int waitMilliseconds, Action callback, Func<
/// Otherwise the next attempt will be performed.
/// </param>
/// <returns>Result of the callback method.</returns>
[Obsolete("Use the async methods in the IRetrier service instead.")]
public static T Retry<T>(int count, int waitMilliseconds, Func<T> callback, Func<T, int, Exception, bool> checkCondition)
{
var retryCount = count;
Expand All @@ -181,7 +186,7 @@ public static T Retry<T>(int count, int waitMilliseconds, Func<T> callback, Func
break;

retryCount--;
System.Threading.Thread.Sleep(waitMilliseconds);
Thread.Sleep(waitMilliseconds);
}

return result;
Expand All @@ -203,9 +208,11 @@ public static T Retry<T>(int count, int waitMilliseconds, Func<T> callback, Func
/// returns with true, the main method returns with the callback's result immediately.
/// Otherwise the next attempt will be performed.
/// </param>
/// <param name="cancel">The token to monitor for cancellation requests.</param>
/// <returns>Result of the callback method.</returns>
// ReSharper disable once UnusedMember.Global
public static async Task<T> RetryAsync<T>(int count, int waitMilliseconds, Func<Task<T>> callback, Func<T, int, Exception, bool> checkCondition)
public static async Task<T> RetryAsync<T>(int count, int waitMilliseconds, Func<Task<T>> callback,
Func<T, int, Exception, bool> checkCondition, CancellationToken cancel = default)
{
var retryCount = count;
var result = default(T);
Expand All @@ -214,6 +221,9 @@ public static async Task<T> RetryAsync<T>(int count, int waitMilliseconds, Func<
{
Exception error = null;

// check if the operation was cancelled before going any further
cancel.ThrowIfCancellationRequested();

try
{
result = await callback().ConfigureAwait(false);
Expand All @@ -230,7 +240,8 @@ public static async Task<T> RetryAsync<T>(int count, int waitMilliseconds, Func<
break;

retryCount--;
System.Threading.Thread.Sleep(waitMilliseconds);

await Task.Delay(waitMilliseconds, cancel).ConfigureAwait(false);
}

return result;
Expand All @@ -250,14 +261,19 @@ public static async Task<T> RetryAsync<T>(int count, int waitMilliseconds, Func<
/// If this checker method returns with true, the main method returns immediately.
/// Otherwise the next attempt will be performed.
/// </param>
public static async Task RetryAsync(int count, int waitMilliseconds, Func<Task> callback, Func<int, Exception, bool> checkCondition)
/// <param name="cancel">The token to monitor for cancellation requests.</param>
public static async Task RetryAsync(int count, int waitMilliseconds, Func<Task> callback,
Func<int, Exception, bool> checkCondition, CancellationToken cancel = default)
{
var retryCount = count;

while (retryCount > 0)
{
Exception error = null;

// check if the operation was cancelled before going any further
cancel.ThrowIfCancellationRequested();

try
{
await callback().ConfigureAwait(false);
Expand All @@ -274,7 +290,8 @@ public static async Task RetryAsync(int count, int waitMilliseconds, Func<Task>
break;

retryCount--;
System.Threading.Thread.Sleep(waitMilliseconds);

await Task.Delay(waitMilliseconds, cancel).ConfigureAwait(false);
}
}
}
Expand Down