aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-08-10 16:55:11 -0500
committerJP Dillingham <jp@dillingham.me>2025-08-10 16:55:11 -0500
commit9353755d0ee0f2764dcf6e5574758ee57ff73cd6 (patch)
tree66a1f8dd25cf23e3ab2b712c348f8657f1f9e37e
parentb6e7db55157e106deba5ba21271e86ce44d74312 (diff)
downloadslskd-9353755d0ee0f2764dcf6e5574758ee57ff73cd6.tar.gz
use readonlylist in a few places
-rw-r--r--src/slskd/Application.cs23
-rw-r--r--src/slskd/Events/EventService.cs5
-rw-r--r--src/slskd/Shares/ShareScanner.cs9
-rw-r--r--src/slskd/Shares/ShareService.cs4
4 files changed, 27 insertions, 14 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs
index 57e5bcf2..56d26276 100644
--- a/src/slskd/Application.cs
+++ b/src/slskd/Application.cs
@@ -135,7 +135,10 @@ namespace slskd
regexOptions |= RegexOptions.IgnoreCase;
}
- CompiledSearchRequestFilters = OptionsAtStartup.Filters.Search.Request.Select(f => new Regex(f, regexOptions)).ToArray();
+ CompiledSearchRequestFilters = OptionsAtStartup.Filters.Search.Request
+ .Select(f => new Regex(f, regexOptions))
+ .ToList()
+ .AsReadOnly();
State = state;
State.OnChange(state => State_OnChange(state));
@@ -228,10 +231,10 @@ namespace slskd
private ISearchService Search { get; set; }
private IRelayService Relay { get; set; }
private IMemoryCache Cache { get; set; } = new MemoryCache(new MemoryCacheOptions());
- private Regex[] CompiledSearchRequestFilters { get; set; }
- private IEnumerable<Guid> ActiveDownloadIdsAtPreviousShutdown { get; set; } = Enumerable.Empty<Guid>();
+ private IReadOnlyList<Regex> CompiledSearchRequestFilters { get; set; } = [];
+ private IReadOnlyList<Guid> ActiveDownloadIdsAtPreviousShutdown { get; set; } = [];
private Options.FlagsOptions Flags { get; set; }
- private IReadOnlyCollection<string> ExcludedSearchPhrases { get; set; } = Enumerable.Empty<string>().ToList();
+ private IReadOnlyList<string> ExcludedSearchPhrases { get; set; } = [];
public void CollectGarbage()
{
@@ -352,7 +355,7 @@ namespace slskd
// save the ids of any downloads that were active, so we can re-enqueue them after we've connected and logged in.
// we need to check the database before we re-request to make sure the user didn't remove them from the UI while
// the application was running, but before it was logged in. so just save the ids.
- ActiveDownloadIdsAtPreviousShutdown = activeDownloads.Select(d => d.Id);
+ ActiveDownloadIdsAtPreviousShutdown = activeDownloads.Select(d => d.Id).ToList().AsReadOnly();
Log.Debug("Downloads to resume upon connection: {Ids}", ActiveDownloadIdsAtPreviousShutdown.ToJson());
Log.Debug("Configuring client");
@@ -924,7 +927,7 @@ namespace slskd
}
// clear the ids we saved at startup; we don't want to re-request these again if the connection is cycled
- ActiveDownloadIdsAtPreviousShutdown = Enumerable.Empty<Guid>();
+ ActiveDownloadIdsAtPreviousShutdown = [];
}
catch (Exception ex)
{
@@ -1178,7 +1181,7 @@ namespace slskd
private void Client_ExcludedSearchPhrasesReceived(object sender, IReadOnlyCollection<string> e)
{
Log.Debug("Excluded search phrases: {Phrases}", string.Join(", ", e));
- ExcludedSearchPhrases = e;
+ ExcludedSearchPhrases = e.ToList();
}
private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args)
@@ -1319,7 +1322,11 @@ namespace slskd
if (PreviousOptions.Filters.Search.Request.Except(newOptions.Filters.Search.Request).Any()
|| newOptions.Filters.Search.Request.Except(PreviousOptions.Filters.Search.Request).Any())
{
- CompiledSearchRequestFilters = newOptions.Filters.Search.Request.Select(f => new Regex(f, RegexOptions.Compiled)).ToArray();
+ CompiledSearchRequestFilters = newOptions.Filters.Search.Request
+ .Select(f => new Regex(f, RegexOptions.Compiled))
+ .ToList()
+ .AsReadOnly();
+
Log.Information("Updated and re-compiled search response filters");
}
diff --git a/src/slskd/Events/EventService.cs b/src/slskd/Events/EventService.cs
index d6f1a3f5..6dc61c13 100644
--- a/src/slskd/Events/EventService.cs
+++ b/src/slskd/Events/EventService.cs
@@ -48,7 +48,7 @@ public class EventService
/// <returns>The retrieved list.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified <paramref name="offset"/> is less than zero.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified <paramref name="limit"/> is zero.</exception>
- public virtual IReadOnlyCollection<EventRecord> Get(int offset = 0, int limit = int.MaxValue)
+ public virtual IReadOnlyList<EventRecord> Get(int offset = 0, int limit = int.MaxValue)
{
if (offset < 0)
{
@@ -68,7 +68,8 @@ public class EventService
.OrderByDescending(e => e.Timestamp)
.Skip(offset)
.Take(limit)
- .ToList();
+ .ToList()
+ .AsReadOnly();
return events;
}
diff --git a/src/slskd/Shares/ShareScanner.cs b/src/slskd/Shares/ShareScanner.cs
index bf069991..6e32f81e 100644
--- a/src/slskd/Shares/ShareScanner.cs
+++ b/src/slskd/Shares/ShareScanner.cs
@@ -88,7 +88,7 @@ namespace slskd.Shares
private FileService Files { get; }
private int WorkerCount { get; }
private ILogger Log { get; } = Serilog.Log.ForContext<ShareScanner>();
- private List<Share> Shares { get; set; }
+ private IReadOnlyList<Share> Shares { get; set; }
private ISoulseekFileFactory SoulseekFileFactory { get; }
private IManagedState<SharedFileCacheState> State { get; } = new ManagedState<SharedFileCacheState>();
private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1);
@@ -161,6 +161,8 @@ namespace slskd.Shares
var swSnapshot = 0L;
sw.Start();
+ // note: this should be the only place where we are assigning Shares, so that this is 1) synchronized by SyncRoot
+ // and 2) stable throughout the scan. if this ever changes we'll have to snapshot the value and use it instead
Shares = shares.ToList();
if (!Shares.Any())
@@ -168,7 +170,10 @@ namespace slskd.Shares
Log.Warning("Aborting shared file scan; no shares configured.");
}
- Shares.ForEach(s => Log.Debug(s.ToJson()));
+ foreach (var share in Shares)
+ {
+ Log.Debug(share.ToJson());
+ }
Shares.Where(s => !s.IsExcluded).ToList()
.ForEach(s => Log.Information("Sharing {Local} as {Remote}", s.LocalPath, s.RemotePath));
diff --git a/src/slskd/Shares/ShareService.cs b/src/slskd/Shares/ShareService.cs
index 7b3e949b..6068a744 100644
--- a/src/slskd/Shares/ShareService.cs
+++ b/src/slskd/Shares/ShareService.cs
@@ -59,7 +59,7 @@ namespace slskd.Shares
Local = (host, repository);
- AllRepositories = new List<IShareRepository>(new[] { repository });
+ AllRepositories = [repository];
Scanner = scanner ?? new ShareScanner(
workerCount: options.Shares.Cache.Workers,
@@ -116,7 +116,7 @@ namespace slskd.Shares
private StorageMode CacheStorageMode { get; }
private ILogger Log { get; } = Serilog.Log.ForContext<ShareService>();
private (Host Host, IShareRepository Repository) Local { get; set; }
- private IReadOnlyCollection<IShareRepository> AllRepositories { get; set; }
+ private IReadOnlyList<IShareRepository> AllRepositories { get; set; }
/// <summary>
/// Adds a new, or updates an existing, share host.