diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-03-04 07:12:01 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-04 07:12:01 -0600 |
| commit | 3af712067a03640fd251f804ca532bd4cd5f7b10 (patch) | |
| tree | 5e0ce1932f9d959b17d49fbc0a32b27434f13419 /src/slskd/Application.cs | |
| parent | b39be34b05d6dd8e8d57e06512263d30634b9e1a (diff) | |
| parent | 264019f11042fbffd789560635bf8943d4de9aa2 (diff) | |
| download | slskd-3af712067a03640fd251f804ca532bd4cd5f7b10.tar.gz | |
Merge branch 'master' into chore/codestyle
Diffstat (limited to 'src/slskd/Application.cs')
| -rw-r--r-- | src/slskd/Application.cs | 82 |
1 files changed, 50 insertions, 32 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index b105cb56..75b2eee6 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -181,6 +181,8 @@ namespace slskd Client.DownloadDenied += (e, args) => Log.Information("Download of {Filename} from {Username} was denied: {Message}", args.Filename, args.Username, args.Message); Client.DownloadFailed += (e, args) => Log.Information("Download of {Filename} from {Username} failed", args.Filename, args.Username); + Client.ExcludedSearchPhrasesReceived += Client_ExcludedSearchPhrasesReceived; + ConnectionWatchdog = connectionWatchdog; Clock.EveryMinute += Clock_EveryMinute; @@ -221,6 +223,7 @@ namespace slskd private IEnumerable<Regex> CompiledSearchResponseFilters { get; set; } private IEnumerable<Guid> ActiveDownloadIdsAtPreviousShutdown { get; set; } = Enumerable.Empty<Guid>(); private Options.FlagsOptions Flags { get; set; } + private IReadOnlyCollection<string> ExcludedSearchPhrases { get; set; } = Enumerable.Empty<string>().ToList(); public void CollectGarbage() { @@ -511,9 +514,11 @@ namespace slskd return; } - // resolve the limits for this user's group. there's no 'fallthrough' here, if limits for a group - // are unset, then that group is unlimited. - Options.GroupsOptions.LimitsOptions limits; + // we'll fall back to global limits for any limit that isn't set at the group level + var global = Options.Global.Limits; + + // resolve the limits for this user's group. + Options.LimitsOptions limits; if (Options.Groups.UserDefined.TryGetValue(group, out var userDefinedOptions)) { @@ -525,22 +530,12 @@ namespace slskd { DefaultGroup => Options.Groups.Default.Limits, LeecherGroup => Options.Groups.Leechers.Limits, - _ => Options.Groups.Default.Limits, + _ => Options.Groups.Default.Limits, // that's weird! we'll just go with defaults.. }; } - bool IsNull(Options.GroupsOptions.LimitsExtendedOptions l) => l is null || (l?.Files is null && l?.Megabytes is null); - - // if no limits are set, we're good to go; just enqueue the file. - if (limits is null || (IsNull(limits.Daily) && IsNull(limits.Weekly) && IsNull(limits.Queued))) - { - Log.Debug("Limits bypassed for {Username} and {File}; no limits are set", username, filename); - await Transfers.Uploads.EnqueueAsync(username, filename); - return; - } - - var sw = new Stopwatch(); - sw.Start(); + bool IsNull(Options.LimitsOptions.Limits lim, Options.LimitsOptions.Limits global) + => (lim is null && global is null) || ((lim?.Files ?? global?.Files ?? lim?.Megabytes ?? global?.Megabytes ?? lim.Failures ?? global.Failures) is null); /* * we have limits set, so now we have to fetch the data and compare to see if any would be hit if we allow this transfer to be enqueued. @@ -549,38 +544,47 @@ namespace slskd * 2) that were started within the time period * 3) that did not end due to an error (state includes errored, exception column is set) */ - (bool Files, bool Megabytes) OverLimits((int Files, long Bytes) stats, Options.GroupsOptions.LimitsExtendedOptions options, long size) + (bool Files, bool Megabytes) OverLimits( + (int Files, long Bytes) stats, + Options.LimitsOptions.Limits options, + Options.LimitsOptions.Limits defaults, + long size) { var files = false; var megabytes = false; - var byteLimit = (options?.Megabytes ?? 0) * 1000 * 1000; + var byteLimit = options?.Megabytes ?? defaults?.Megabytes; - if (options?.Megabytes is not null && (stats.Bytes + size) > byteLimit) + if (byteLimit is not null && (stats.Bytes + size) > (byteLimit * 1000 * 1000)) { - Log.Debug("Projected bytes {Bytes} exceeds limit {Limit}", stats.Bytes + size, byteLimit); + Log.Debug("Projected bytes {Bytes} exceeds limit {Limit}", stats.Bytes + size, byteLimit * 1000 * 1000); megabytes = true; } - if (options?.Files is not null && (stats.Files + 1) > options.Files) + var fileLimit = options?.Files ?? defaults?.Files; + + if (fileLimit is not null && (stats.Files + 1) > fileLimit) { - Log.Debug("Projected file count {Files} exceeds limit {Limit}", stats.Files + 1, options.Files); + Log.Debug("Projected file count {Files} exceeds limit {Limit}", stats.Files + 1, fileLimit); files = true; } return (files, megabytes); } + var sw = new Stopwatch(); + sw.Start(); + // start with the queue, since that should contain the fewest files and should be the least expensive to check // "queued" includes both queued and in progress; records with a null EndedAt property, which is guaranteed to be set // for terminal transfers. - if (!IsNull(limits.Queued)) + if (!IsNull(limits?.Queued, global?.Queued)) { var queued = Transfers.Uploads.Summarize( expression: t => t.Username == username && t.EndedAt == null); Log.Debug("Fetched queue stats: files: {Files}, bytes: {Bytes} ({Time}ms)", queued.Files, queued.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(queued, limits!.Queued, resolved.Size); + var over = OverLimits(queued, limits?.Queued, global?.Queued, resolved.Size); if (over.Files || over.Megabytes) { @@ -588,7 +592,7 @@ namespace slskd // note: return exactly 'Too many files' or 'Too many megabytes' to ensure interop with other clients. // these messages are retryable, while anything else is not - throw new DownloadEnqueueException($"Too many {(over.Files ? "files" : "megabytes")}"); + throw new DownloadEnqueueException($"Too many {(over.Megabytes ? "megabytes" : "files")}"); } } @@ -597,7 +601,7 @@ namespace slskd // * started within the last week // * which have or have not ended (assuming queued files will complete) // * that were not errored - if (!IsNull(limits.Weekly)) + if (!IsNull(limits?.Weekly, global?.Weekly)) { var erroredState = TransferStates.Completed | TransferStates.Errored; var cutoffDateTime = DateTime.UtcNow.AddDays(-7); @@ -610,7 +614,9 @@ namespace slskd Log.Debug("Fetched weekly failures: {Failures} ({Time}ms)", failures.Files, sw.ElapsedMilliseconds); - if (failures.Files >= limits.Weekly.Failures) + var failureLimit = limits?.Weekly?.Failures ?? global?.Weekly?.Failures; + + if (failureLimit is not null && failures.Files >= failureLimit) { Log.Information("Rejected enqueue request for user {Username}: Weekly failure limit met or exceeded", username); throw new DownloadEnqueueException("Too many failed transfers this week"); @@ -625,7 +631,7 @@ namespace slskd Log.Debug("Fetched weekly stats: files: {Files}, bytes: {Bytes} ({Time}ms)", weekly.Files, weekly.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(weekly, limits!.Weekly, resolved.Size); + var over = OverLimits(weekly, limits?.Weekly, global?.Weekly, resolved.Size); if (over.Files || over.Megabytes) { @@ -636,7 +642,7 @@ namespace slskd // lastly, check daily limits. the criteria for this is the same as weekly, just looking over the previous day instead // of the previous 7. - if (!IsNull(limits.Daily)) + if (!IsNull(limits?.Daily, global?.Daily)) { var erroredState = TransferStates.Completed | TransferStates.Errored; var cutoffDateTime = DateTime.UtcNow.AddDays(-1); @@ -649,7 +655,9 @@ namespace slskd Log.Debug("Fetched daily failures: {Failures} ({Time}ms)", failures.Files, sw.ElapsedMilliseconds); - if (failures.Files >= limits.Daily.Failures) + var failureLimit = limits?.Daily?.Failures ?? global?.Daily?.Failures; + + if (failureLimit is not null && failures.Files >= failureLimit) { Log.Information("Rejected enqueue request for user {Username}: Daily failure limit met or exceeded", username); throw new DownloadEnqueueException("Too many failed transfers today"); @@ -664,7 +672,7 @@ namespace slskd Log.Debug("Fetched daily stats: files: {Files}, bytes: {Bytes} ({Time}ms)", daily.Files, daily.Bytes, sw.ElapsedMilliseconds); - var over = OverLimits(daily, limits!.Daily, resolved.Size); + var over = OverLimits(daily, limits?.Daily, global?.Daily, resolved.Size); if (over.Files || over.Megabytes) { @@ -1088,6 +1096,12 @@ namespace slskd } } + private void Client_ExcludedSearchPhrasesReceived(object sender, IReadOnlyCollection<string> e) + { + Log.Debug("Excluded search phrases: {Phrases}", string.Join(", ", e)); + ExcludedSearchPhrases = e; + } + private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args) { // no-op. this is really verbose, use for troubleshooting. @@ -1361,11 +1375,15 @@ namespace slskd // sometimes clients send search queries consisting only of exclusions; drop them. // no other clients send search results for these, even though it is technically possible. - if (!query.Terms.Any()) + if (query.Terms.Count == 0) { return null; } + // append the list of excluded search phrases supplied by the server + // see https://github.com/jpdillingham/Soulseek.NET/issues/803 + query = new SearchQuery(terms: query.Terms, exclusions: query.Exclusions.Concat(ExcludedSearchPhrases).Distinct()); + try { var sw = new Stopwatch(); |
