-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Samuel Abraham
committed
Nov 26, 2024
1 parent
d248801
commit f7f413f
Showing
24 changed files
with
460 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2021 Samuel Abraham | ||
|
||
using TypeCache.Utilities; | ||
|
||
namespace TypeCache.Extensions; | ||
|
||
public static class ActionExtensions | ||
{ | ||
/// <summary> | ||
/// Retry a failed <see cref="Action"/>. The # of <c><paramref name="retryDelays"/></c> dictates the # of retry attempts.<br/> | ||
/// Some built-in interval sequences to use for retry delays:<br/> | ||
/// <list type="table"> | ||
/// <item><c><see cref="Sequence.ExponentialSeconds()"/></c></item> | ||
/// <item><c><see cref="Sequence.ExponentialSeconds(uint)"/></c></item> | ||
/// <item><c><see cref="Sequence.LinearTime(TimeSpan)"/></c></item> | ||
/// </list> | ||
/// These are increasing infinite sequences, hence an infinite # of retries will be attempted.<br/> | ||
/// To limit the number of retries, call Linq's Take(...) method on the returned collection. | ||
/// </summary> | ||
public static async Task Retry(this Action @this, IEnumerable<TimeSpan> retryDelays, TimeProvider? timeProvider = default, CancellationToken token = default) | ||
{ | ||
@this.ThrowIfNull(); | ||
|
||
try | ||
{ | ||
await Task.Run(@this, token); | ||
} | ||
catch (Exception lastError) | ||
{ | ||
timeProvider ??= TimeProvider.System; | ||
|
||
foreach (var delay in retryDelays) | ||
{ | ||
await Task.Delay(delay, timeProvider, token); | ||
try | ||
{ | ||
await Task.Run(@this, token); | ||
return; | ||
} | ||
catch (Exception ex) | ||
{ | ||
lastError = ex; | ||
} | ||
} | ||
|
||
await Task.FromException(lastError); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2021 Samuel Abraham | ||
|
||
namespace TypeCache.Extensions; | ||
|
||
public static class FuncExtensions | ||
{ | ||
/// <summary> | ||
/// Retry a failed <see cref="Func{TResult}"/>. The # of <c><paramref name="retryDelays"/></c> dictates the # of retry attempts.<br/> | ||
/// Some built-in interval sequences to use for retry delays:<br/> | ||
/// <list type="table"> | ||
/// <item><c><see cref="Sequence.ExponentialSeconds()"/></c></item> | ||
/// <item><c><see cref="Sequence.ExponentialSeconds(uint)"/></c></item> | ||
/// <item><c><see cref="Sequence.LinearTime(TimeSpan)"/></c></item> | ||
/// </list> | ||
/// These are increasing infinite sequences, hence an infinite # of retries will be attempted.<br/> | ||
/// To limit the number of retries, call Linq's Take(...) method on the returned collection. | ||
/// </summary> | ||
public static async Task<T> Retry<T>(this Func<T> @this, IEnumerable<TimeSpan> retryDelays, TimeProvider? timeProvider = default, CancellationToken token = default) | ||
{ | ||
@this.ThrowIfNull(); | ||
|
||
try | ||
{ | ||
return await Task.Run(@this, token); | ||
} | ||
catch (Exception lastError) | ||
{ | ||
timeProvider ??= TimeProvider.System; | ||
foreach (var delay in retryDelays) | ||
{ | ||
await Task.Delay(delay, timeProvider, token); | ||
try | ||
{ | ||
return await Task.Run(@this, token); | ||
} | ||
catch (Exception ex) | ||
{ | ||
lastError = ex; | ||
} | ||
} | ||
|
||
return await Task.FromException<T>(lastError); | ||
} | ||
} | ||
} |
Oops, something went wrong.