aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Retry.cs73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/slskd/Common/Retry.cs b/src/slskd/Common/Retry.cs
new file mode 100644
index 00000000..c0e1bd83
--- /dev/null
+++ b/src/slskd/Common/Retry.cs
@@ -0,0 +1,73 @@
+// <copyright file="Retry.cs" company="slskd Team">
+// Copyright (c) slskd Team. All rights reserved.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published
+// by the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see https://www.gnu.org/licenses/.
+// </copyright>
+
+namespace slskd
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Threading.Tasks;
+
+ public class Retry
+ {
+ public static async Task Do(Func<Task> task, TimeSpan retryInterval, int maxAttemptCount = 3)
+ {
+ var exceptions = new List<Exception>();
+ for (int attempted = 0; attempted < maxAttemptCount; attempted++)
+ {
+ try
+ {
+ if (attempted > 0)
+ {
+ await Task.Delay(retryInterval);
+ }
+
+ await task();
+ return;
+ }
+ catch (Exception ex)
+ {
+ exceptions.Add(ex);
+ }
+ }
+
+ throw new AggregateException(exceptions);
+ }
+
+ public static async Task<T> Do<T>(Func<Task<T>> task, TimeSpan retryInterval, int maxAttemptCount = 3)
+ {
+ var exceptions = new List<Exception>();
+ for (int attempted = 0; attempted < maxAttemptCount; attempted++)
+ {
+ try
+ {
+ if (attempted > 0)
+ {
+ await Task.Delay(retryInterval);
+ }
+
+ return await task();
+ }
+ catch (Exception ex)
+ {
+ exceptions.Add(ex);
+ }
+ }
+
+ throw new AggregateException(exceptions);
+ }
+ }
+} \ No newline at end of file