*atomize* download status!
This commit is contained in:
@@ -18,8 +18,7 @@ namespace LightlessSync.WebAPI.Files;
|
||||
|
||||
public partial class FileDownloadManager : DisposableMediatorSubscriberBase
|
||||
{
|
||||
private readonly Dictionary<string, FileDownloadStatus> _downloadStatus;
|
||||
private readonly object _downloadStatusLock = new();
|
||||
private readonly ConcurrentDictionary<string, FileDownloadStatus> _downloadStatus;
|
||||
|
||||
private readonly FileCompactor _fileCompactor;
|
||||
private readonly FileCacheManager _fileDbManager;
|
||||
@@ -46,7 +45,7 @@ public partial class FileDownloadManager : DisposableMediatorSubscriberBase
|
||||
ModelDecimationService modelDecimationService,
|
||||
TextureMetadataHelper textureMetadataHelper) : base(logger, mediator)
|
||||
{
|
||||
_downloadStatus = new Dictionary<string, FileDownloadStatus>(StringComparer.Ordinal);
|
||||
_downloadStatus = new ConcurrentDictionary<string, FileDownloadStatus>(StringComparer.Ordinal);
|
||||
_orchestrator = orchestrator;
|
||||
_fileDbManager = fileCacheManager;
|
||||
_fileCompactor = fileCompactor;
|
||||
@@ -86,10 +85,7 @@ public partial class FileDownloadManager : DisposableMediatorSubscriberBase
|
||||
public void ClearDownload()
|
||||
{
|
||||
CurrentDownloads.Clear();
|
||||
lock (_downloadStatusLock)
|
||||
{
|
||||
_downloadStatus.Clear();
|
||||
}
|
||||
_downloadStatus.Clear();
|
||||
CurrentOwnerToken = null;
|
||||
}
|
||||
|
||||
@@ -156,29 +152,20 @@ public partial class FileDownloadManager : DisposableMediatorSubscriberBase
|
||||
|
||||
private void SetStatus(string key, DownloadStatus status)
|
||||
{
|
||||
lock (_downloadStatusLock)
|
||||
{
|
||||
if (_downloadStatus.TryGetValue(key, out var st))
|
||||
st.DownloadStatus = status;
|
||||
}
|
||||
if (_downloadStatus.TryGetValue(key, out var st))
|
||||
st.DownloadStatus = status;
|
||||
}
|
||||
|
||||
private void AddTransferredBytes(string key, long delta)
|
||||
{
|
||||
lock (_downloadStatusLock)
|
||||
{
|
||||
if (_downloadStatus.TryGetValue(key, out var st))
|
||||
st.TransferredBytes += delta;
|
||||
}
|
||||
if (_downloadStatus.TryGetValue(key, out var st))
|
||||
st.AddTransferredBytes(delta);
|
||||
}
|
||||
|
||||
private void MarkTransferredFiles(string key, int files)
|
||||
{
|
||||
lock (_downloadStatusLock)
|
||||
{
|
||||
if (_downloadStatus.TryGetValue(key, out var st))
|
||||
st.TransferredFiles = files;
|
||||
}
|
||||
if (_downloadStatus.TryGetValue(key, out var st))
|
||||
st.SetTransferredFiles(files);
|
||||
}
|
||||
|
||||
private static byte MungeByte(int byteOrEof)
|
||||
@@ -712,34 +699,31 @@ public partial class FileDownloadManager : DisposableMediatorSubscriberBase
|
||||
.ToArray();
|
||||
|
||||
// init statuses
|
||||
lock (_downloadStatusLock)
|
||||
_downloadStatus.Clear();
|
||||
|
||||
// direct downloads and batch downloads tracked separately
|
||||
foreach (var d in directDownloads)
|
||||
{
|
||||
_downloadStatus.Clear();
|
||||
|
||||
// direct downloads and batch downloads tracked separately
|
||||
foreach (var d in directDownloads)
|
||||
_downloadStatus[d.DirectDownloadUrl!] = new FileDownloadStatus
|
||||
{
|
||||
_downloadStatus[d.DirectDownloadUrl!] = new FileDownloadStatus
|
||||
{
|
||||
DownloadStatus = DownloadStatus.WaitingForSlot,
|
||||
TotalBytes = d.Total,
|
||||
TotalFiles = 1,
|
||||
TransferredBytes = 0,
|
||||
TransferredFiles = 0
|
||||
};
|
||||
}
|
||||
DownloadStatus = DownloadStatus.WaitingForSlot,
|
||||
TotalBytes = d.Total,
|
||||
TotalFiles = 1,
|
||||
TransferredBytes = 0,
|
||||
TransferredFiles = 0
|
||||
};
|
||||
}
|
||||
|
||||
foreach (var chunk in batchChunks)
|
||||
foreach (var chunk in batchChunks)
|
||||
{
|
||||
_downloadStatus[chunk.StatusKey] = new FileDownloadStatus
|
||||
{
|
||||
_downloadStatus[chunk.StatusKey] = new FileDownloadStatus
|
||||
{
|
||||
DownloadStatus = DownloadStatus.WaitingForQueue,
|
||||
TotalBytes = chunk.Items.Sum(x => x.Total),
|
||||
TotalFiles = 1,
|
||||
TransferredBytes = 0,
|
||||
TransferredFiles = 0
|
||||
};
|
||||
}
|
||||
DownloadStatus = DownloadStatus.WaitingForQueue,
|
||||
TotalBytes = chunk.Items.Sum(x => x.Total),
|
||||
TotalFiles = 1,
|
||||
TransferredBytes = 0,
|
||||
TransferredFiles = 0
|
||||
};
|
||||
}
|
||||
|
||||
if (directDownloads.Count > 0 || batchChunks.Length > 0)
|
||||
|
||||
@@ -1,10 +1,46 @@
|
||||
namespace LightlessSync.WebAPI.Files.Models;
|
||||
using System.Threading;
|
||||
|
||||
namespace LightlessSync.WebAPI.Files.Models;
|
||||
|
||||
public class FileDownloadStatus
|
||||
{
|
||||
public DownloadStatus DownloadStatus { get; set; }
|
||||
public long TotalBytes { get; set; }
|
||||
public int TotalFiles { get; set; }
|
||||
public long TransferredBytes { get; set; }
|
||||
public int TransferredFiles { get; set; }
|
||||
}
|
||||
private int _downloadStatus;
|
||||
private long _totalBytes;
|
||||
private int _totalFiles;
|
||||
private long _transferredBytes;
|
||||
private int _transferredFiles;
|
||||
|
||||
public DownloadStatus DownloadStatus
|
||||
{
|
||||
get => (DownloadStatus)Volatile.Read(ref _downloadStatus);
|
||||
set => Volatile.Write(ref _downloadStatus, (int)value);
|
||||
}
|
||||
|
||||
public long TotalBytes
|
||||
{
|
||||
get => Interlocked.Read(ref _totalBytes);
|
||||
set => Interlocked.Exchange(ref _totalBytes, value);
|
||||
}
|
||||
|
||||
public int TotalFiles
|
||||
{
|
||||
get => Volatile.Read(ref _totalFiles);
|
||||
set => Volatile.Write(ref _totalFiles, value);
|
||||
}
|
||||
|
||||
public long TransferredBytes
|
||||
{
|
||||
get => Interlocked.Read(ref _transferredBytes);
|
||||
set => Interlocked.Exchange(ref _transferredBytes, value);
|
||||
}
|
||||
|
||||
public int TransferredFiles
|
||||
{
|
||||
get => Volatile.Read(ref _transferredFiles);
|
||||
set => Volatile.Write(ref _transferredFiles, value);
|
||||
}
|
||||
|
||||
public void AddTransferredBytes(long delta) => Interlocked.Add(ref _transferredBytes, delta);
|
||||
|
||||
public void SetTransferredFiles(int files) => Volatile.Write(ref _transferredFiles, files);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user