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 +++++++++++++++++++++++++++++ src/slskd/Shares/ISharedFileCacheWorker.cs | 42 --------- src/slskd/Shares/SharedFileCache.cs | 8 +- src/slskd/Shares/SharedFileCacheWorker.cs | 96 -------------------- 4 files changed, 143 insertions(+), 142 deletions(-) create mode 100644 src/slskd/Common/ChannelReader.cs delete mode 100644 src/slskd/Shares/ISharedFileCacheWorker.cs delete mode 100644 src/slskd/Shares/SharedFileCacheWorker.cs (limited to 'src') 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(); + } + } + } +} diff --git a/src/slskd/Shares/ISharedFileCacheWorker.cs b/src/slskd/Shares/ISharedFileCacheWorker.cs deleted file mode 100644 index b6b85c5d..00000000 --- a/src/slskd/Shares/ISharedFileCacheWorker.cs +++ /dev/null @@ -1,42 +0,0 @@ -// -// 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.Threading.Tasks; - - /// - /// Shared file cache worker. - /// - public interface ISharedFileCacheWorker - { - /// - /// Gets the that completes when the worker has completed all of its work. - /// - Task Completed { get; } - - /// - /// Gets the Id of the worker. - /// - int Id { get; } - - /// - /// Starts the worker. - /// - void Start(); - } -} diff --git a/src/slskd/Shares/SharedFileCache.cs b/src/slskd/Shares/SharedFileCache.cs index 1c2b61aa..6a0cf87c 100644 --- a/src/slskd/Shares/SharedFileCache.cs +++ b/src/slskd/Shares/SharedFileCache.cs @@ -42,6 +42,7 @@ namespace slskd.Shares /// Initializes a new instance of the class. /// /// + /// /// public SharedFileCache(StorageMode storageMode, int workerCount, ISoulseekFileFactory soulseekFileFactory = null) { @@ -241,13 +242,12 @@ namespace slskd.Shares }); // create workers to perform the fan out - var workers = new List(); + var workers = new List>(); foreach (var id in Enumerable.Range(0, WorkerCount)) { - workers.Add(new SharedFileCacheWorker( - id: id, - directoryChannel: DirectoryChannel, + workers.Add(new ChannelReader( + channel: DirectoryChannel, cancellationToken: cancellationToken, handler: (directory) => { diff --git a/src/slskd/Shares/SharedFileCacheWorker.cs b/src/slskd/Shares/SharedFileCacheWorker.cs deleted file mode 100644 index 6ac5452b..00000000 --- a/src/slskd/Shares/SharedFileCacheWorker.cs +++ /dev/null @@ -1,96 +0,0 @@ -// -// 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; - using Serilog; - - /// - /// Shared file cache worker. - /// - public class SharedFileCacheWorker : ISharedFileCacheWorker - { - /// - /// Initializes a new instance of the class. - /// - /// The worker's unique Id. - /// The channel from which the worker will receive directories to scan. - /// A to monitor for cancellation requests. - /// An to be performed on each item read from the channel. - public SharedFileCacheWorker( - int id, - Channel directoryChannel, - CancellationToken cancellationToken, - Action handler) - { - Id = id; - DirectoryChannel = directoryChannel; - CancellationToken = cancellationToken; - Handler = handler; - } - - /// - /// Gets the that completes when the worker has completed all of its work. - /// - public Task Completed => TaskCompletionSource.Task; - - /// - /// Gets the Id of the worker. - /// - public int Id { get; } - - private Channel DirectoryChannel { get; } - private Action Handler { get; } - private TaskCompletionSource TaskCompletionSource { get; } = new TaskCompletionSource(); - private CancellationToken CancellationToken { get; } - private ILogger Log { get; } = Serilog.Log.ForContext(); - - /// - /// Starts the worker. - /// - public void Start() - { - _ = Read(); - Log.Debug("Shared file cache worker {Id} started", Id); - } - - private async Task Read() - { - try - { - while (!CancellationToken.IsCancellationRequested && !DirectoryChannel.Reader.Completion.IsCompleted) - { - var directory = await DirectoryChannel.Reader.ReadAsync(CancellationToken); - Handler(directory); - } - } - 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. - } - finally - { - Log.Debug($"Shared file cache worker {Id}'s work is complete.", Id); - TaskCompletionSource.SetResult(); - } - } - } -} -- cgit v1.2.3