Usenet streaming with a WebDAV server and a SABnzbd-compatible API
C#
Added logic to replace unhealthy Nntp connections.
nzbdav-devb51ae7e1125d
diff --git a/backend/Clients/Usenet/MultiConnectionNntpClient.cs b/backend/Clients/Usenet/MultiConnectionNntpClient.csindex 18882b85..b5fb7cf1 100644--- a/backend/Clients/Usenet/MultiConnectionNntpClient.cs+++ b/backend/Clients/Usenet/MultiConnectionNntpClient.cs@@ -1,7 +1,11 @@-using NzbWebDAV.Clients.Usenet.Concurrency;+using System.Diagnostics.CodeAnalysis;+using NzbWebDAV.Clients.Usenet.Concurrency;using NzbWebDAV.Clients.Usenet.Connections;using NzbWebDAV.Clients.Usenet.Models;+using NzbWebDAV.Exceptions;+using NzbWebDAV.Extensions;using NzbWebDAV.Models;+using Serilog;using UsenetSharp.Models;namespace NzbWebDAV.Clients.Usenet;@@ -16,6 +20,7 @@ namespace NzbWebDAV.Clients.Usenet;/// </summary>/// <param name="connectionPool"></param>/// <param name="type"></param>+[SuppressMessage("ReSharper", "AccessToDisposedClosure")]public class MultiConnectionNntpClient(ConnectionPool<INntpClient> connectionPool, ProviderType type) : NntpClient{public ProviderType ProviderType { get; } = type;@@ -29,93 +34,218 @@ public class MultiConnectionNntpClient(ConnectionPool<INntpClient> connectionPoothrow new NotSupportedException("Please connect within the connectionFactory");}- public override Task<UsenetResponse> AuthenticateAsync(string user, string pass, CancellationToken cancellationToken)+ public override Task<UsenetResponse> AuthenticateAsync(string user, string pass,+ CancellationToken cancellationToken){throw new NotSupportedException("Please authenticate within the connectionFactory");}- public override async Task<UsenetStatResponse> StatAsync(SegmentId segmentId, CancellationToken ct)+ public override Task<UsenetStatResponse> StatAsync(SegmentId segmentId, CancellationToken ct){- const SemaphorePriority priority = SemaphorePriority.Low;- using var connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);- return await connectionLock.Connection.StatAsync(segmentId, ct).ConfigureAwait(false);+ return RunWithConnection(+ SemaphorePriority.Low,+ connection => connection.StatAsync(segmentId, ct),+ ct+ );}- public override async Task<UsenetHeadResponse> HeadAsync(SegmentId segmentId, CancellationToken ct)+ public override Task<UsenetHeadResponse> HeadAsync(SegmentId segmentId, CancellationToken ct){- const SemaphorePriority priority = SemaphorePriority.Low;- using var connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);- return await connectionLock.Connection.HeadAsync(segmentId, ct).ConfigureAwait(false);+ return RunWithConnection(+ SemaphorePriority.Low,+ connection => connection.HeadAsync(segmentId, ct),+ ct+ );}- public override async Task<UsenetDecodedBodyResponse> DecodedBodyAsync(SegmentId segmentId, CancellationToken ct)+ public override Task<UsenetDecodedBodyResponse> DecodedBodyAsync(SegmentId segmentId, CancellationToken ct){- const SemaphorePriority priority = SemaphorePriority.High;- var connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);- return await connectionLock.Connection.DecodedBodyAsync(segmentId, OnDone, ct).ConfigureAwait(false);-- void OnDone(ArticleBodyResult articleBodyResult)- {- connectionLock.Dispose();- }+ return RunWithConnection(+ SemaphorePriority.High,+ (connection, onDone) => connection.DecodedBodyAsync(segmentId, onDone, ct),+ onConnectionReadyAgain: null,+ ct+ );}- public override async Task<UsenetDecodedArticleResponse> DecodedArticleAsync+ public override Task<UsenetDecodedArticleResponse> DecodedArticleAsync(- SegmentId segmentId,+ SegmentId segmentId,CancellationToken ct){- const SemaphorePriority priority = SemaphorePriority.High;- var connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);- return await connectionLock.Connection.DecodedArticleAsync(segmentId, OnDone, ct).ConfigureAwait(false);+ return RunWithConnection(+ SemaphorePriority.High,+ (connection, onDone) => connection.DecodedArticleAsync(segmentId, onDone, ct),+ onConnectionReadyAgain: null,+ ct+ );+ }- void OnDone(ArticleBodyResult articleBodyResult)- {- connectionLock.Dispose();- }+ public override Task<UsenetDateResponse> DateAsync(CancellationToken ct)+ {+ return RunWithConnection(+ SemaphorePriority.Low,+ connection => connection.DateAsync(ct),+ ct+ );}- public override async Task<UsenetDateResponse> DateAsync(CancellationToken ct)+ public override Task<UsenetDecodedBodyResponse> DecodedBodyAsync+ (+ SegmentId segmentId,+ Action<ArticleBodyResult>? onConnectionReadyAgain,+ CancellationToken ct+ ){- const SemaphorePriority priority = SemaphorePriority.Low;- using var connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);- return await connectionLock.Connection.DateAsync(ct).ConfigureAwait(false);+ return RunWithConnection(+ SemaphorePriority.High,+ (connection, onDone) => connection.DecodedBodyAsync(segmentId, onDone, ct),+ onConnectionReadyAgain,+ ct+ );}- public override async Task<UsenetDecodedBodyResponse> DecodedBodyAsync+ public override Task<UsenetDecodedArticleResponse> DecodedArticleAsync(SegmentId segmentId,Action<ArticleBodyResult>? onConnectionReadyAgain,CancellationToken ct){- const SemaphorePriority priority = SemaphorePriority.High;- var connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);- return await connectionLock.Connection.DecodedBodyAsync(segmentId, OnDone, ct).ConfigureAwait(false);+ return RunWithConnection(+ SemaphorePriority.High,+ (connection, onDone) => connection.DecodedArticleAsync(segmentId, onDone, ct),+ onConnectionReadyAgain,+ ct+ );+ }- void OnDone(ArticleBodyResult articleBodyResult)+ private async Task<T> RunWithConnection<T>+ (+ SemaphorePriority priority,+ Func<INntpClient, Task<T>> command,+ CancellationToken ct,+ int retryCount = 1+ )+ {+ while (retryCount >= 0){- connectionLock.Dispose();- onConnectionReadyAgain?.Invoke(articleBodyResult);+ ConnectionLock<INntpClient>? connectionLock = null;+ try+ {+ connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);+ var result = await command(connectionLock.Connection).ConfigureAwait(false);+ return result;+ }+ catch (Exception e)+ {+ connectionLock?.Replace();+ var message = "Error executing nntp command.";+ if (retryCount > 0) message += " Retrying with a new connection.";+ Log.Warning(e, message);+ if (retryCount <= 0) throw;+ else retryCount--;+ }+ finally+ {+ connectionLock?.Dispose();+ }}++ Log.Error("Unreachable code reached");+ throw new InvalidOperationException("Unreachable code ");}- public override async Task<UsenetDecodedArticleResponse> DecodedArticleAsync+ private async Task<T> RunWithConnection<T>(- SegmentId segmentId,+ SemaphorePriority priority,+ Func<INntpClient, Action<ArticleBodyResult>, Task<T>> command,Action<ArticleBodyResult>? onConnectionReadyAgain,- CancellationToken ct- )+ CancellationToken ct,+ int retryCount = 1+ ) where T : UsenetResponse{- const SemaphorePriority priority = SemaphorePriority.High;- var connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);- return await connectionLock.Connection.DecodedArticleAsync(segmentId, OnDone, ct).ConfigureAwait(false);+ while (retryCount >= 0)+ {+ ConnectionLock<INntpClient>? connectionLock = null;+ try+ {+ connectionLock = await connectionPool.GetConnectionLockAsync(priority, ct).ConfigureAwait(false);+ }+ catch (Exception e)+ {+ LogException(() => connectionLock?.Replace());+ LogException(() => connectionLock?.Dispose());+ if (retryCount > 0)+ {+ Log.Warning(e, "Error getting connection-lock. Retrying with a new connection.");+ retryCount--;+ continue;+ }++ Log.Warning(e, "Error getting connection-lock.");+ LogException(() => onConnectionReadyAgain?.Invoke(ArticleBodyResult.NotRetrieved));+ throw;+ }++ T? result;+ try+ {+ result = await command(connectionLock.Connection, OnConnectionReadyAgain).ConfigureAwait(false);+ }+ catch (Exception e) when (e.TryGetInnerException(out UsenetArticleNotFoundException _))+ {+ LogException(() => connectionLock?.Dispose());+ LogException(() => onConnectionReadyAgain?.Invoke(ArticleBodyResult.NotRetrieved));+ throw;+ }+ catch (Exception e) when (!e.TryGetInnerException(out UsenetArticleNotFoundException _))+ {+ LogException(() => connectionLock?.Replace());+ LogException(() => connectionLock?.Dispose());+ if (retryCount > 0)+ {+ Log.Warning(e, "Error executing nntp command. Retrying with a new connection.");+ retryCount--;+ continue;+ }++ Log.Warning(e, "Error executing nntp command.");+ LogException(() => onConnectionReadyAgain?.Invoke(ArticleBodyResult.NotRetrieved));+ throw;+ }++ if ((result?.Success ?? false) == false)+ {+ LogException(() => connectionLock?.Dispose());+ LogException(() => onConnectionReadyAgain?.Invoke(ArticleBodyResult.NotRetrieved));+ }++ return result!;- void OnDone(ArticleBodyResult articleBodyResult)+ void OnConnectionReadyAgain(ArticleBodyResult articleBodyResult)+ {+ if (articleBodyResult != ArticleBodyResult.Retrieved) return;++ LogException(() => connectionLock?.Dispose());+ LogException(() => onConnectionReadyAgain?.Invoke(articleBodyResult));+ }+ }++ Log.Error("Unreachable code reached");+ throw new InvalidOperationException("Unreachable code ");+ }++ private static void LogException(Action? action)+ {+ try+ {+ action?.Invoke();+ }+ catch (Exception e){- connectionLock.Dispose();- onConnectionReadyAgain?.Invoke(articleBodyResult);+ Log.Warning(e, "Unhandled exception");}}