Skip to content

Commit fb4a618

Browse files
committed
Make SonarQube happy :)
1 parent 7e3f3f1 commit fb4a618

File tree

2 files changed

+88
-76
lines changed

2 files changed

+88
-76
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Hi3Helper;
2+
using System;
3+
using System.Runtime.CompilerServices;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
// ReSharper disable UnusedMember.Global
7+
8+
#nullable enable
9+
namespace CollapseLauncher.Extension
10+
{
11+
public delegate ConfiguredTaskAwaitable<TResult?> ActionTimeoutTaskAwaitableCallback<TResult>(CancellationToken token);
12+
internal static partial class TaskExtensions
13+
{
14+
internal static Task<TResult?>
15+
WaitForRetryAsync<TResult>(this ActionTimeoutTaskAwaitableCallback<TResult?> funcCallback,
16+
int? timeout = null,
17+
int? timeoutStep = null,
18+
int? retryAttempt = null,
19+
ActionOnTimeOutRetry? actionOnRetry = null,
20+
CancellationToken fromToken = default)
21+
=> WaitForRetryAsync(() => funcCallback, timeout, timeoutStep, retryAttempt, actionOnRetry, fromToken);
22+
23+
internal static async Task<TResult?>
24+
WaitForRetryAsync<TResult>(Func<ActionTimeoutTaskAwaitableCallback<TResult?>> funcCallback,
25+
int? timeout = null,
26+
int? timeoutStep = null,
27+
int? retryAttempt = null,
28+
ActionOnTimeOutRetry? actionOnRetry = null,
29+
CancellationToken fromToken = default)
30+
{
31+
timeout ??= DefaultTimeoutSec;
32+
timeoutStep ??= 0;
33+
retryAttempt ??= DefaultRetryAttempt;
34+
35+
int retryAttemptCurrent = 1;
36+
Exception? lastException = null;
37+
while (retryAttemptCurrent < retryAttempt)
38+
{
39+
fromToken.ThrowIfCancellationRequested();
40+
CancellationTokenSource? innerCancellationToken = null;
41+
CancellationTokenSource? consolidatedToken = null;
42+
43+
try
44+
{
45+
innerCancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(timeout ?? DefaultTimeoutSec));
46+
consolidatedToken = CancellationTokenSource.CreateLinkedTokenSource(innerCancellationToken.Token, fromToken);
47+
48+
ActionTimeoutTaskAwaitableCallback<TResult?> delegateCallback = funcCallback();
49+
return await delegateCallback(consolidatedToken.Token);
50+
}
51+
catch (OperationCanceledException) when (fromToken.IsCancellationRequested) { throw; }
52+
catch (Exception ex)
53+
{
54+
lastException = ex;
55+
actionOnRetry?.Invoke(retryAttemptCurrent, (int)retryAttempt, timeout ?? 0, timeoutStep ?? 0);
56+
57+
if (ex is TimeoutException)
58+
{
59+
string msg = $"The operation has timed out! Retrying attempt left: {retryAttemptCurrent}/{retryAttempt}";
60+
Logger.LogWriteLine(msg, LogType.Warning, true);
61+
}
62+
else
63+
{
64+
string msg = $"The operation has thrown an exception! Retrying attempt left: {retryAttemptCurrent}/{retryAttempt}\r\n{ex}";
65+
Logger.LogWriteLine(msg, LogType.Error, true);
66+
}
67+
68+
retryAttemptCurrent++;
69+
timeout += timeoutStep;
70+
}
71+
finally
72+
{
73+
innerCancellationToken?.Dispose();
74+
consolidatedToken?.Dispose();
75+
}
76+
}
77+
78+
if (lastException is not null
79+
&& !fromToken.IsCancellationRequested)
80+
throw lastException is TaskCanceledException ?
81+
new TimeoutException("The operation has timed out with inner exception!", lastException) :
82+
lastException;
83+
84+
throw new TimeoutException("The operation has timed out!");
85+
}
86+
}
87+
}

CollapseLauncher/Classes/Extension/TaskExtensions.cs

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Hi3Helper;
22
using System;
3-
using System.Runtime.CompilerServices;
43
using System.Threading;
54
using System.Threading.Tasks;
65
// ReSharper disable UnusedMember.Global
@@ -9,86 +8,12 @@
98
namespace CollapseLauncher.Extension
109
{
1110
public delegate Task<TResult?> ActionTimeoutTaskCallback<TResult>(CancellationToken token);
12-
public delegate ConfiguredTaskAwaitable<TResult?> ActionTimeoutTaskAwaitableCallback<TResult>(CancellationToken token);
1311
public delegate void ActionOnTimeOutRetry(int retryAttemptCount, int retryAttemptTotal, int timeOutSecond, int timeOutStep);
14-
internal static class TaskExtensions
12+
internal static partial class TaskExtensions
1513
{
1614
internal const int DefaultTimeoutSec = 10;
1715
internal const int DefaultRetryAttempt = 5;
1816

19-
internal static Task<TResult?>
20-
WaitForRetryAsync<TResult>(this ActionTimeoutTaskAwaitableCallback<TResult?> funcCallback,
21-
int? timeout = null,
22-
int? timeoutStep = null,
23-
int? retryAttempt = null,
24-
ActionOnTimeOutRetry? actionOnRetry = null,
25-
CancellationToken fromToken = default)
26-
=> WaitForRetryAsync(() => funcCallback, timeout, timeoutStep, retryAttempt, actionOnRetry, fromToken);
27-
28-
internal static async Task<TResult?>
29-
WaitForRetryAsync<TResult>(Func<ActionTimeoutTaskAwaitableCallback<TResult?>> funcCallback,
30-
int? timeout = null,
31-
int? timeoutStep = null,
32-
int? retryAttempt = null,
33-
ActionOnTimeOutRetry? actionOnRetry = null,
34-
CancellationToken fromToken = default)
35-
{
36-
timeout ??= DefaultTimeoutSec;
37-
timeoutStep ??= 0;
38-
retryAttempt ??= DefaultRetryAttempt;
39-
40-
int retryAttemptCurrent = 1;
41-
Exception? lastException = null;
42-
while (retryAttemptCurrent < retryAttempt)
43-
{
44-
fromToken.ThrowIfCancellationRequested();
45-
CancellationTokenSource? innerCancellationToken = null;
46-
CancellationTokenSource? consolidatedToken = null;
47-
48-
try
49-
{
50-
innerCancellationToken = new CancellationTokenSource(TimeSpan.FromSeconds(timeout ?? DefaultTimeoutSec));
51-
consolidatedToken = CancellationTokenSource.CreateLinkedTokenSource(innerCancellationToken.Token, fromToken);
52-
53-
ActionTimeoutTaskAwaitableCallback<TResult?> delegateCallback = funcCallback();
54-
return await delegateCallback(consolidatedToken.Token);
55-
}
56-
catch (OperationCanceledException) when (fromToken.IsCancellationRequested) { throw; }
57-
catch (Exception ex)
58-
{
59-
lastException = ex;
60-
actionOnRetry?.Invoke(retryAttemptCurrent, (int)retryAttempt, timeout ?? 0, timeoutStep ?? 0);
61-
62-
if (ex is TimeoutException)
63-
{
64-
string msg = $"The operation has timed out! Retrying attempt left: {retryAttemptCurrent}/{retryAttempt}";
65-
Logger.LogWriteLine(msg, LogType.Warning, true);
66-
}
67-
else
68-
{
69-
string msg = $"The operation has thrown an exception! Retrying attempt left: {retryAttemptCurrent}/{retryAttempt}\r\n{ex}";
70-
Logger.LogWriteLine(msg, LogType.Error, true);
71-
}
72-
73-
retryAttemptCurrent++;
74-
timeout += timeoutStep;
75-
}
76-
finally
77-
{
78-
innerCancellationToken?.Dispose();
79-
consolidatedToken?.Dispose();
80-
}
81-
}
82-
83-
if (lastException is not null
84-
&& !fromToken.IsCancellationRequested)
85-
throw lastException is TaskCanceledException ?
86-
new TimeoutException("The operation has timed out with inner exception!", lastException) :
87-
lastException;
88-
89-
throw new TimeoutException("The operation has timed out!");
90-
}
91-
9217
internal static async Task<TResult?>
9318
WaitForRetryAsync<TResult>(this ActionTimeoutTaskCallback<TResult?> funcCallback,
9419
int? timeout = null,

0 commit comments

Comments
 (0)