Repository navigation
Add eventual provider diversity with safe additive STRMs #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace InfiniteDrive.Models; | ||
|
|
||
| /// <summary>One durable queue entry per coverage identity/episode, never a playback claim.</summary> | ||
| public sealed class ImportDiversityEntry | ||
| { | ||
| public string Status { get; set; } = "waiting"; | ||
| public string Profile { get; set; } = ""; | ||
| public long Generation { get; set; } | ||
| public string Reason { get; set; } = "single_source"; | ||
| public int Streak { get; set; } | ||
| public int Attempts { get; set; } | ||
| public DateTimeOffset CreatedAt { get; set; } | ||
| public DateTimeOffset? CheckedAt { get; set; } | ||
| public DateTimeOffset? NextAttempt { get; set; } | ||
| } | ||
|
|
||
| public static class ImportDiversityPolicy | ||
| { | ||
| private static readonly string[] Labels = { "TorBox", "Usenet", "Real-Debrid", "AllDebrid", "Premiumize", | ||
| "Debrid-Link", "Offcloud", "EasyDebrid", "Debrider", "Easynews", "NZBDav", "AltMount", "StremThru" }; | ||
| public static string? Provider(string? label) => label?.ToLowerInvariant() is "nntp" or "stremionntp" | ||
| ? "Usenet" : Labels.FirstOrDefault(x => string.Equals(x, label, StringComparison.OrdinalIgnoreCase)); | ||
|
|
||
| // Every retained path must have one unambiguous known label. Historical paths don't count. | ||
| public static List<string>? Providers(ImportEpisode ep) | ||
| { | ||
| if (ep.Paths.Count == 0) return null; | ||
| var result = new HashSet<string>(StringComparer.Ordinal); | ||
| foreach (var path in ep.Paths.Distinct(StringComparer.Ordinal)) | ||
| { | ||
| var labels = ep.Versions.Where(x => x.Path == path).Select(x => Provider(x.ServiceLabel)).Distinct().ToList(); | ||
| if (labels.Count != 1 || labels[0] == null) return null; | ||
| result.Add(labels[0]!); | ||
| } | ||
| return result.OrderBy(x => x, StringComparer.Ordinal).ToList(); | ||
| } | ||
|
|
||
| public static bool Eligible(ImportCoverage coverage, ImportEpisode ep) => | ||
| coverage.Exclusion.Length == 0 && coverage.SnapshotStatus == "success" && ep.Expected && ep.Eligible && | ||
| ep.State == "indexed" && ep.Failure.Length == 0 && Providers(ep)?.Count == 1; | ||
|
|
||
| public static void Reconcile(ImportCoverage coverage, ImportEpisode ep, string profile, DateTimeOffset now) | ||
| { | ||
| var providers = Providers(ep); | ||
| if (!Eligible(coverage, ep)) | ||
| { | ||
| if (ep.Diversity != null) | ||
| { ep.Diversity.Status = providers?.Count > 1 ? "resolved" : "retired"; | ||
| ep.Diversity.Reason = providers?.Count > 1 ? "multiple_sources" : "ineligible"; } | ||
| return; | ||
| } | ||
| if (ep.Diversity == null || ep.Diversity.Profile != profile || ep.Diversity.Generation != coverage.Generation) | ||
| ep.Diversity = new() { Profile = profile, Generation = coverage.Generation, CreatedAt = now, | ||
| NextAttempt = now.AddHours(6) }; | ||
| else if (ep.Diversity.Status is "retired" or "resolved") | ||
| { ep.Diversity.Status = "waiting"; ep.Diversity.Reason = "single_source"; | ||
| ep.Diversity.NextAttempt = now.AddHours(6); } | ||
| if (ep.Diversity.Status == "in_flight" && (ep.LeaseUntil == null || ep.LeaseUntil <= now)) | ||
| { ep.Diversity.Status = "waiting"; ep.Diversity.Reason = "interrupted"; | ||
| ep.Diversity.NextAttempt = now.AddMinutes(5); } | ||
| if (ep.Paths.Distinct().Count() >= 8) | ||
| { ep.Diversity.Status = "capacity"; ep.Diversity.Reason = "version_limit"; } | ||
| else if (ep.Diversity.Status == "capacity") | ||
| { ep.Diversity.Status = "waiting"; ep.Diversity.Reason = "single_source"; } | ||
| } | ||
|
|
||
| public static void Defer(ImportDiversityEntry entry, string reason, DateTimeOffset now, | ||
| double jitter, DateTimeOffset cooldown) | ||
| { | ||
| entry.Streak++; entry.Status = "waiting"; entry.Reason = reason; entry.CheckedAt = now; | ||
| entry.NextAttempt = ImportCoveragePolicy.RetryAt(entry.Streak, | ||
| reason is "same_source" or "source_unavailable" or "no_valid_addition", now, jitter, cooldown); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a single-source item receives its normal hourly refresh,
CompleteAsynctemporarily changes its state toawaiting_indexing, so reconciliation retires the diversity entry; once Emby reports it indexed again, this branch resetsNextAttemptto another six hours. BecauseImportWorkBudget.NeedsRefreshmakes the same item refreshable again after only one hour, small libraries that revisit items within six hours repeat this cycle indefinitely and never dispatch the new diversity lookup. Preserve the existing due time across this transient refresh/indexing state, or only restart the window when the saved provider set actually changes.Useful? React with 👍 / 👎.