aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Common/CommonExtensions.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2023-01-08 16:58:50 -0600
committerGitHub <noreply@github.com>2023-01-08 16:58:50 -0600
commit475e59702d0dff2cbc50dcd3b3c2654487da2d2b (patch)
treeee047ecb27dacc77de98f922e740fc693a78b921 /src/slskd/Common/CommonExtensions.cs
parent25c21ef7f87bfdeac155171371848fe686dc9462 (diff)
parent6e787074c427773acb40f7191c68bf2afaaf7635 (diff)
downloadslskd-475e59702d0dff2cbc50dcd3b3c2654487da2d2b.tar.gz
Merge pull request #844 from slskd/reconnect
Improve Relay agent reconnect logic
Diffstat (limited to 'src/slskd/Common/CommonExtensions.cs')
-rw-r--r--src/slskd/Common/CommonExtensions.cs53
1 files changed, 51 insertions, 2 deletions
diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs
index 3dddd3b4..6a472b9b 100644
--- a/src/slskd/Common/CommonExtensions.cs
+++ b/src/slskd/Common/CommonExtensions.cs
@@ -23,12 +23,12 @@ namespace slskd
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Linq;
- using System.Numerics;
using System.Reflection;
- using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
+ using System.Threading;
+ using System.Threading.Tasks;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
@@ -38,6 +38,55 @@ namespace slskd
public static class CommonExtensions
{
/// <summary>
+ /// Times this Task out after the specified TimeSpan.
+ /// </summary>
+ /// <typeparam name="T">The result type.</typeparam>
+ /// <param name="task">The task.</param>
+ /// <param name="timeout">The timeout.</param>
+ /// <returns>The operation context.</returns>
+ /// <exception cref="TimeoutException">Thrown when the task times out.</exception>
+ public static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan timeout)
+ {
+ using var timeoutCancellationTokenSource = new CancellationTokenSource();
+
+ var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
+
+ if (completedTask == task)
+ {
+ timeoutCancellationTokenSource.Cancel();
+ return await task;
+ }
+ else
+ {
+ throw new TimeoutException("The operation has timed out.");
+ }
+ }
+
+ /// <summary>
+ /// Times this Task out after the specified TimeSpan.
+ /// </summary>
+ /// <param name="task">The task.</param>
+ /// <param name="timeout">The timeout.</param>
+ /// <returns>The operation context.</returns>
+ /// <exception cref="TimeoutException">Thrown when the task times out.</exception>
+ public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
+ {
+ using var timeoutCancellationTokenSource = new CancellationTokenSource();
+
+ var completedTask = await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token));
+
+ if (completedTask == task)
+ {
+ timeoutCancellationTokenSource.Cancel();
+ await task;
+ }
+ else
+ {
+ throw new TimeoutException("The operation has timed out.");
+ }
+ }
+
+ /// <summary>
/// Deeply compares this object with the specified object and returns a list of properties that are different.
/// </summary>
/// <param name="left">The left side of the comparison.</param>