From 938e610419bbadd0e194c4b789409453399e85ba Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 18 Sep 2022 13:03:33 -0500 Subject: refactor SharedFileCacheWorker into ChannelReader --- src/slskd/Common/ChannelReader.cs | 139 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/slskd/Common/ChannelReader.cs (limited to 'src/slskd/Common/ChannelReader.cs') diff --git a/src/slskd/Common/ChannelReader.cs b/src/slskd/Common/ChannelReader.cs new file mode 100644 index 00000000..f358fb07 --- /dev/null +++ b/src/slskd/Common/ChannelReader.cs @@ -0,0 +1,139 @@ +// +// 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/. +// + +namespace slskd.Shares +{ + using System; + using System.Threading; + using System.Threading.Channels; + using System.Threading.Tasks; + + /// + /// Reads and handles items from a channel. + /// + public interface IChannelReader + { + /// + /// Gets the that completes when the reader has read all available items from the channel. + /// + Task Completed { get; } + + /// + /// Gets a value indicating whether the reader has started reading. + /// + bool Started { get; } + + /// + /// Starts the reader. + /// + void Start(); + } + + /// + /// Shared file cache worker. + /// + /// The type of the underlying channel. + public class ChannelReader : IChannelReader + { + /// + /// Initializes a new instance of the class. + /// + /// The channel from which the reader will read. + /// An to be invoked for each item read from the channel. + /// An optional to be invoked if the reader encounters an . + /// An optional value indicating whether to automatically start the reader. + /// An optional to monitor for cancellation. + public ChannelReader( + Channel channel, + Action handler, + Action exceptionHandler = null, + bool automaticallyStart = true, + CancellationToken cancellationToken = default) + { + Channel = channel; + Handler = handler; + ExceptionHandler = exceptionHandler; + + CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + CancellationToken = CancellationTokenSource.Token; + + if (automaticallyStart) + { + Start(); + } + } + + /// + /// Gets the that completes when the worker has completed all of its work. + /// + public Task Completed => TaskCompletionSource.Task; + + /// + /// Gets a value indicating whether the reader has started reading. + /// + public bool Started { get; private set; } + + private Channel Channel { get; } + private Action Handler { get; } + private Action ExceptionHandler { get; } + private TaskCompletionSource TaskCompletionSource { get; } = new TaskCompletionSource(); + private CancellationTokenSource CancellationTokenSource { get; } + private CancellationToken CancellationToken { get; } + private object SyncRoot { get; } = new object(); + + /// + /// Starts the reader. + /// + public void Start() + { + lock (SyncRoot) + { + if (!Started) + { + _ = Read(); + Started = true; + } + } + } + + private async Task Read() + { + try + { + while (Started && !CancellationTokenSource.Token.IsCancellationRequested && !Channel.Reader.Completion.IsCompleted) + { + var item = await Channel.Reader.ReadAsync(CancellationToken); + Handler(item); + } + } + catch (ChannelClosedException) + { + // noop. the channel might close between the time we check and when we go to read; this just means there is no more data. + } + catch (Exception ex) + { + ExceptionHandler?.Invoke(ex); + TaskCompletionSource.SetException(ex); + throw; + } + finally + { + TaskCompletionSource.SetResult(); + } + } + } +} -- cgit v1.2.3 From 270d10645a1725d67a4e14ab828451ec4c4d7338 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 18 Sep 2022 13:29:35 -0500 Subject: bug --- src/slskd/Common/ChannelReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Common/ChannelReader.cs') diff --git a/src/slskd/Common/ChannelReader.cs b/src/slskd/Common/ChannelReader.cs index f358fb07..a6a8b9a3 100644 --- a/src/slskd/Common/ChannelReader.cs +++ b/src/slskd/Common/ChannelReader.cs @@ -114,7 +114,7 @@ namespace slskd.Shares { try { - while (Started && !CancellationTokenSource.Token.IsCancellationRequested && !Channel.Reader.Completion.IsCompleted) + while (!CancellationTokenSource.Token.IsCancellationRequested && !Channel.Reader.Completion.IsCompleted) { var item = await Channel.Reader.ReadAsync(CancellationToken); Handler(item); -- cgit v1.2.3