Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Refactored cancellation-token contexts. Ensure context is forwarded to linked cts.

nzbdav-deve8410714aaef
diff --git a/backend/Clients/Usenet/Connections/ConnectionPool.cs b/backend/Clients/Usenet/Connections/ConnectionPool.cs
index 4a743ea1..bf036f33 100644
--- a/backend/Clients/Usenet/Connections/ConnectionPool.cs
+++ b/backend/Clients/Usenet/Connections/ConnectionPool.cs
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using NzbWebDAV.Clients.Usenet.Concurrency;
+using NzbWebDAV.Clients.Usenet.Contexts;
namespace NzbWebDAV.Clients.Usenet.Connections;
diff --git a/backend/Clients/Usenet/Contexts/CancellationTokenContext.cs b/backend/Clients/Usenet/Contexts/CancellationTokenContext.cs
new file mode 100644
index 00000000..73da6dab
--- /dev/null
+++ b/backend/Clients/Usenet/Contexts/CancellationTokenContext.cs
@@ -0,0 +1,39 @@
+using System.Collections.Concurrent;
+
+namespace NzbWebDAV.Clients.Usenet.Contexts;
+
+public class CancellationTokenContext : IDisposable
+{
+ private static readonly ConcurrentDictionary<LookupKey, object?> Context = new();
+
+ private LookupKey _lookupKey;
+
+ private CancellationTokenContext(LookupKey lookupKey)
+ {
+ _lookupKey = lookupKey;
+ }
+
+ public static CancellationTokenContext SetContext<T>(CancellationToken ct, T? value)
+ {
+ var lookupKey = new LookupKey() { CancellationToken = ct, Type = typeof(T) };
+ Context[lookupKey] = value;
+ return new CancellationTokenContext(lookupKey);
+ }
+
+ public static T? GetContext<T>(CancellationToken ct)
+ {
+ var lookupKey = new LookupKey() { CancellationToken = ct, Type = typeof(T) };
+ return Context.TryGetValue(lookupKey, out var result) && result is T context ? context : default;
+ }
+
+ public void Dispose()
+ {
+ Context.Remove(_lookupKey, out _);
+ }
+
+ private record struct LookupKey
+ {
+ public CancellationToken CancellationToken;
+ public Type Type;
+ }
+}
\ No newline at end of file
diff --git a/backend/Clients/Usenet/Contexts/ContextualCancellationTokenSource.cs b/backend/Clients/Usenet/Contexts/ContextualCancellationTokenSource.cs
new file mode 100644
index 00000000..6b0e62c2
--- /dev/null
+++ b/backend/Clients/Usenet/Contexts/ContextualCancellationTokenSource.cs
@@ -0,0 +1,65 @@
+using NzbWebDAV.Extensions;
+
+namespace NzbWebDAV.Clients.Usenet.Contexts;
+
+public class ContextualCancellationTokenSource : IDisposable
+{
+ private readonly CancellationTokenSource _cts;
+ private readonly List<CancellationTokenContext> _contexts;
+ private bool _disposed;
+
+ public CancellationToken Token => _cts.Token;
+
+ private ContextualCancellationTokenSource(CancellationTokenSource cts)
+ {
+ _cts = cts;
+ _contexts = [];
+ }
+
+ public static ContextualCancellationTokenSource CreateLinkedTokenSource(CancellationToken linkedToken)
+ {
+ var cts = CancellationTokenSource.CreateLinkedTokenSource(linkedToken);
+ var contextualCts = new ContextualCancellationTokenSource(cts);
+ contextualCts.SetContext(linkedToken.GetContext<DownloadPriorityContext>());
+ return contextualCts;
+ }
+
+ public static ContextualCancellationTokenSource CreateLinkedTokenSource
+ (
+ CancellationToken linkedToken1,
+ CancellationToken linkedToken2
+ )
+ {
+ var cts = CancellationTokenSource.CreateLinkedTokenSource(linkedToken1, linkedToken2);
+ var contextualCts = new ContextualCancellationTokenSource(cts);
+ contextualCts.SetContext(linkedToken1.GetContext<DownloadPriorityContext>());
+ contextualCts.SetContext(linkedToken2.GetContext<DownloadPriorityContext>());
+ return contextualCts;
+ }
+
+ private void SetContext<T>(T? value)
+ {
+ if (value == null) return;
+ ObjectDisposedException.ThrowIf(_disposed, nameof(ContextualCancellationTokenSource));
+ _contexts.Add(CancellationTokenContext.SetContext(_cts.Token, value));
+ }
+
+ public void Cancel()
+ {
+ _cts.Cancel();
+ }
+
+ public Task CancelAsync()
+ {
+ return _cts.CancelAsync();
+ }
+
+ public void Dispose()
+ {
+ if (Interlocked.Exchange(ref _disposed, true)) return;
+ foreach (var context in _contexts) context.Dispose();
+ _contexts.Clear();
+ _cts.Dispose();
+ GC.SuppressFinalize(this);
+ }
+}
\ No newline at end of file
diff --git a/backend/Clients/Usenet/Contexts/DownloadPriorityContext.cs b/backend/Clients/Usenet/Contexts/DownloadPriorityContext.cs
index d775f0bb..b0857f7d 100644
--- a/backend/Clients/Usenet/Contexts/DownloadPriorityContext.cs
+++ b/backend/Clients/Usenet/Contexts/DownloadPriorityContext.cs
@@ -1,7 +1,8 @@
-namespace NzbWebDAV.Clients.Usenet.Contexts;
+using NzbWebDAV.Clients.Usenet.Concurrency;
-public enum DownloadPriorityContext
+namespace NzbWebDAV.Clients.Usenet.Contexts;
+
+public record DownloadPriorityContext
{
- Low,
- High,
+ public required SemaphorePriority Priority { get; init; }
}
\ No newline at end of file
diff --git a/backend/Clients/Usenet/DownloadingNntpClient.cs b/backend/Clients/Usenet/DownloadingNntpClient.cs
index 137ef255..d393a93b 100644
--- a/backend/Clients/Usenet/DownloadingNntpClient.cs
+++ b/backend/Clients/Usenet/DownloadingNntpClient.cs
@@ -97,9 +97,7 @@ public class DownloadingNntpClient : WrappingNntpClient
private Task AcquireExclusiveConnectionAsync(CancellationToken cancellationToken)
{
var downloadPriorityContext = cancellationToken.GetContext<DownloadPriorityContext>();
- var semaphorePriority = downloadPriorityContext == DownloadPriorityContext.Low
- ? SemaphorePriority.Low
- : SemaphorePriority.High;
+ var semaphorePriority = downloadPriorityContext?.Priority ?? SemaphorePriority.Low;
return _semaphore.WaitAsync(semaphorePriority, cancellationToken);
}
diff --git a/backend/Clients/Usenet/NntpClient.cs b/backend/Clients/Usenet/NntpClient.cs
index 28f2af01..2ac0aa1d 100644
--- a/backend/Clients/Usenet/NntpClient.cs
+++ b/backend/Clients/Usenet/NntpClient.cs
@@ -1,3 +1,4 @@
+using NzbWebDAV.Clients.Usenet.Contexts;
using NzbWebDAV.Clients.Usenet.Models;
using NzbWebDAV.Exceptions;
using NzbWebDAV.Extensions;
diff --git a/backend/Extensions/CancellationTokenExtensions.cs b/backend/Extensions/CancellationTokenExtensions.cs
index 65c4a39b..379fd010 100644
--- a/backend/Extensions/CancellationTokenExtensions.cs
+++ b/backend/Extensions/CancellationTokenExtensions.cs
@@ -1,35 +1,17 @@
-using System.Collections.Concurrent;
+using NzbWebDAV.Clients.Usenet.Concurrency;
+using NzbWebDAV.Clients.Usenet.Contexts;
namespace NzbWebDAV.Extensions;
public static class CancellationTokenExtensions
{
- private static readonly ConcurrentDictionary<LookupKey, object?> Context = new();
-
- public static CancellationTokenScopedContext SetScopedContext<T>(this CancellationToken ct, T? value)
+ public static CancellationTokenContext SetContext<T>(this CancellationToken ct, T? value)
{
- var lookupKey = new LookupKey() { CancellationToken = ct, Type = typeof(T) };
- Context[lookupKey] = value;
- return new CancellationTokenScopedContext(lookupKey, value);
+ return CancellationTokenContext.SetContext(ct, value);
}
public static T? GetContext<T>(this CancellationToken ct)
{
- var lookupKey = new LookupKey() { CancellationToken = ct, Type = typeof(T) };
- return Context.TryGetValue(lookupKey, out var result) && result is T context ? context : default;
- }
-
- public class CancellationTokenScopedContext(LookupKey lookupKey, object? value) : IDisposable
- {
- public void Dispose()
- {
- Context.Remove(lookupKey, out _);
- }
- }
-
- public record struct LookupKey
- {
- public CancellationToken CancellationToken;
- public Type Type;
+ return CancellationTokenContext.GetContext<T>(ct);
}
}
\ No newline at end of file
diff --git a/backend/Streams/MultiSegmentStream.cs b/backend/Streams/MultiSegmentStream.cs
index cdcb655d..1d7b56dc 100644
--- a/backend/Streams/MultiSegmentStream.cs
+++ b/backend/Streams/MultiSegmentStream.cs
@@ -1,5 +1,7 @@
using System.Threading.Channels;
using NzbWebDAV.Clients.Usenet;
+using NzbWebDAV.Clients.Usenet.Concurrency;
+using NzbWebDAV.Clients.Usenet.Contexts;
using NzbWebDAV.Clients.Usenet.Models;
using UsenetSharp.Streams;
@@ -10,23 +12,35 @@ public class MultiSegmentStream : FastReadOnlyNonSeekableStream
private readonly Memory<string> _segmentIds;
private readonly INntpClient _usenetClient;
private readonly Channel<Task<Stream>> _streamTasks;
- private readonly CancellationTokenSource _cts;
+ private readonly ContextualCancellationTokenSource _cts;
private Stream? _stream;
private bool _disposed;
- public static Stream Create(Memory<string> segmentIds, INntpClient usenetClient, int articleBufferSize)
+ public static Stream Create
+ (
+ Memory<string> segmentIds,
+ INntpClient usenetClient,
+ int articleBufferSize,
+ CancellationToken cancellationToken
+ )
{
return articleBufferSize == 0
? new UnbufferedMultiSegmentStream(segmentIds, usenetClient)
- : new MultiSegmentStream(segmentIds, usenetClient, articleBufferSize);
+ : new MultiSegmentStream(segmentIds, usenetClient, articleBufferSize, cancellationToken);
}
- private MultiSegmentStream(Memory<string> segmentIds, INntpClient usenetClient, int articleBufferSize)
+ private MultiSegmentStream
+ (
+ Memory<string> segmentIds,
+ INntpClient usenetClient,
+ int articleBufferSize,
+ CancellationToken cancellationToken
+ )
{
_segmentIds = segmentIds;
_usenetClient = usenetClient;
_streamTasks = Channel.CreateBounded<Task<Stream>>(articleBufferSize);
- _cts = new CancellationTokenSource();
+ _cts = ContextualCancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_ = DownloadSegments(_cts.Token);
}
diff --git a/backend/Streams/NzbFileStream.cs b/backend/Streams/NzbFileStream.cs
index db2e58c0..cda37a50 100644
--- a/backend/Streams/NzbFileStream.cs
+++ b/backend/Streams/NzbFileStream.cs
@@ -69,18 +69,18 @@ public class NzbFileStream(
private async Task<Stream> GetFileStream(long rangeStart, CancellationToken cancellationToken)
{
- if (rangeStart == 0) return GetMultiSegmentStream(0);
+ if (rangeStart == 0) return GetMultiSegmentStream(0, cancellationToken);
var foundSegment = await SeekSegment(rangeStart, cancellationToken).ConfigureAwait(false);
- var stream = GetMultiSegmentStream(foundSegment.FoundIndex);
+ var stream = GetMultiSegmentStream(foundSegment.FoundIndex, cancellationToken);
await stream.DiscardBytesAsync(rangeStart - foundSegment.FoundByteRange.StartInclusive, cancellationToken)
.ConfigureAwait(false);
return stream;
}
- private Stream GetMultiSegmentStream(int firstSegmentIndex)
+ private Stream GetMultiSegmentStream(int firstSegmentIndex, CancellationToken cancellationToken)
{
var segmentIds = fileSegmentIds.AsMemory()[firstSegmentIndex..];
- return MultiSegmentStream.Create(segmentIds, usenetClient, articleBufferSize);
+ return MultiSegmentStream.Create(segmentIds, usenetClient, articleBufferSize, cancellationToken);
}
protected override void Dispose(bool disposing)
diff --git a/backend/WebDav/Base/BaseStoreStreamFile.cs b/backend/WebDav/Base/BaseStoreStreamFile.cs
index 2fec2402..18432fe7 100644
--- a/backend/WebDav/Base/BaseStoreStreamFile.cs
+++ b/backend/WebDav/Base/BaseStoreStreamFile.cs
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
+using NzbWebDAV.Clients.Usenet.Concurrency;
using NzbWebDAV.Clients.Usenet.Contexts;
using NzbWebDAV.Extensions;
@@ -10,10 +11,11 @@ public abstract class BaseStoreStreamFile(HttpContext context) : BaseStoreReadon
public override Task<Stream> GetReadableStreamAsync(CancellationToken cancellationToken)
{
- var downloadPriorityContext = cancellationToken.SetScopedContext(DownloadPriorityContext.High);
+ var downloadPriorityContext = new DownloadPriorityContext() { Priority = SemaphorePriority.High };
+ var scopedDownloadPriorityContext = cancellationToken.SetContext(downloadPriorityContext);
context.Response.OnCompleted(() =>
{
- downloadPriorityContext.Dispose();
+ scopedDownloadPriorityContext.Dispose();
return Task.CompletedTask;
});