Raccolta: 429 del CDN come rallentamento, attese crescenti e recupero dei falliti, storico e seguiti senza pagina di ripresa, lacuna sotto la prima pagina, eliminazioni durante lo scaricamento (regole-3, cdn-429, dl-1, post-loop, walk-1, check-1, scroll-1, reader-1, h9-1, ring-1, library-2, dedup-1, tmp-1, budget-1, regole-4, kind-1, vocab-1)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
Claude committed 2026-10-14 00:23:03 +00:00
1 parent 78c92eb2a4
commit b16e3a837d
28 files changed
+2619 -200

No files matched your search

+31 -2
View File
@@ -1,4 +1,5 @@
using System.Buffers;
using System.Collections.Concurrent;
using System.Globalization;
using System.Security.Cryptography;
@@ -71,6 +72,23 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
/// <summary>Serializes «is the name free? then rename» across all writers of the process.</summary>
private static readonly Lock MoveGate = new();
/// <summary>Temporary files this process is writing now: the clean-up of a previous run never touches them.</summary>
private static readonly ConcurrentDictionary<string, byte> LiveTemporaryFiles =
new(OperatingSystem.IsWindows() ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal);
/// <summary>
/// Marks a temporary file this process is about to write: <see cref="CleanupTemporaryFiles"/> leaves it alone
/// until the returned handle is disposed. The clean-up of an interrupted run can still be going through a large
/// library while the first downloads of this run are written (on Linux a deleted open file would be lost).
/// </summary>
public static IDisposable ReserveTemporaryFile(string path)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path);
var key = Path.GetFullPath(path);
LiveTemporaryFiles[key] = 0;
return new TemporaryFileReservation(key);
}
/// <summary>Writes <paramref name="source"/> to <paramref name="relativePath"/> inside <paramref name="rootFolder"/>.</summary>
public Task<SavedFile> SaveAsync(Stream source, string rootFolder, string relativePath, SafeWriteOptions? options = null, CancellationToken ct = default)
{
@@ -120,6 +138,7 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
var tempFolder = string.IsNullOrEmpty(tempDirectory) ? root : LibraryLayout.FullPath(root, tempDirectory);
Directory.CreateDirectory(tempFolder);
var tempPath = Path.Combine(tempFolder, TempPrefix + Guid.NewGuid().ToString("N")[..12] + TempSuffix);
using var reservation = ReserveTemporaryFile(tempPath);
try
{
@@ -156,8 +175,8 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
}
/// <summary>
/// Removes temporary files left by an interrupted process (never library files: only <c>.febe-*.part</c>).
/// Returns how many were removed.
/// Removes temporary files left by an interrupted process (never library files: only <c>.febe-*.part</c>), except
/// those this process is writing (<see cref="ReserveTemporaryFile"/>). Returns how many were removed.
/// </summary>
public static int CleanupTemporaryFiles(string folder, bool recursive = true)
{
@@ -173,6 +192,11 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
new EnumerationOptions { RecurseSubdirectories = recursive, IgnoreInaccessible = true, AttributesToSkip = 0 });
foreach (var file in files)
{
if (LiveTemporaryFiles.ContainsKey(Path.GetFullPath(file)))
{
continue; // being written by this process right now
}
if (TryDelete(file))
{
removed++;
@@ -272,6 +296,11 @@ public sealed class SafeFileWriter(int maxPathLength = WindowsFileNames.MaxPathL
throw new IOException($"Nessun nome libero per {desiredRelativePath} dopo {MaxNameAttempts} tentativi.");
}
private sealed class TemporaryFileReservation(string key) : IDisposable
{
public void Dispose() => LiveTemporaryFiles.TryRemove(key, out _);
}
private static bool TryDelete(string path)
{
try