mirror of
https://github.com/SoPat712/allstarr.git
synced 2026-02-09 23:55:10 -05:00
fix: improve download speed and handle concurrent requests better
- Reduced wait interval from 500ms to 100ms when waiting for in-progress downloads - Added cancellation token checks during wait loops to handle client timeouts immediately - Added detailed timing logs to track download performance - Better error messages when downloads fail or are cancelled - Prevents OperationCanceledException when client times out (typically 10 seconds) This fixes the issue where concurrent requests for the same track would timeout because they were waiting too long for the first download to complete.
This commit is contained in:
@@ -95,23 +95,51 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
|
||||
public async Task<Stream> DownloadAndStreamAsync(string externalProvider, string externalId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var startTime = DateTime.UtcNow;
|
||||
|
||||
// Check if already downloaded locally
|
||||
var localPath = await LocalLibraryService.GetLocalPathForExternalSongAsync(externalProvider, externalId);
|
||||
if (localPath != null && IOFile.Exists(localPath))
|
||||
{
|
||||
Logger.LogInformation("Streaming from local cache: {Path}", localPath);
|
||||
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
|
||||
Logger.LogInformation("Streaming from local cache ({ElapsedMs}ms): {Path}", elapsed, localPath);
|
||||
|
||||
// Update access time for cache cleanup
|
||||
if (SubsonicSettings.StorageMode == StorageMode.Cache)
|
||||
{
|
||||
IOFile.SetLastAccessTime(localPath, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
return IOFile.OpenRead(localPath);
|
||||
}
|
||||
|
||||
// For on-demand streaming, download to disk first to ensure complete file
|
||||
// Download to disk first to ensure complete file with metadata
|
||||
// This is necessary because:
|
||||
// 1. Clients may seek to arbitrary positions (requires full file)
|
||||
// 2. Metadata embedding requires complete file
|
||||
// 3. Caching for future plays
|
||||
Logger.LogInformation("Downloading song for streaming: {Provider}:{ExternalId}", externalProvider, externalId);
|
||||
|
||||
try
|
||||
{
|
||||
localPath = await DownloadSongInternalAsync(externalProvider, externalId, triggerAlbumDownload: true, cancellationToken);
|
||||
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
|
||||
Logger.LogInformation("Download completed, starting stream ({ElapsedMs}ms total): {Path}", elapsed, localPath);
|
||||
return IOFile.OpenRead(localPath);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
|
||||
Logger.LogWarning("Download cancelled by client after {ElapsedMs}ms for {Provider}:{ExternalId}", elapsed, externalProvider, externalId);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var elapsed = (DateTime.UtcNow - startTime).TotalMilliseconds;
|
||||
Logger.LogError(ex, "Download failed after {ElapsedMs}ms for {Provider}:{ExternalId}", elapsed, externalProvider, externalId);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public DownloadInfo? GetDownloadStatus(string songId)
|
||||
{
|
||||
@@ -219,21 +247,26 @@ public abstract class BaseDownloadService : IDownloadService
|
||||
// Check if download in progress
|
||||
if (ActiveDownloads.TryGetValue(songId, out var activeDownload) && activeDownload.Status == DownloadStatus.InProgress)
|
||||
{
|
||||
Logger.LogInformation("Download already in progress for {SongId}, waiting...", songId);
|
||||
Logger.LogDebug("Download already in progress for {SongId}, waiting for completion...", songId);
|
||||
// Release lock while waiting
|
||||
DownloadLock.Release();
|
||||
|
||||
// Wait for download to complete, checking every 100ms (faster than 500ms)
|
||||
// Also respect cancellation token so client timeouts are handled immediately
|
||||
while (ActiveDownloads.TryGetValue(songId, out activeDownload) && activeDownload.Status == DownloadStatus.InProgress)
|
||||
{
|
||||
await Task.Delay(500, cancellationToken);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await Task.Delay(100, cancellationToken);
|
||||
}
|
||||
|
||||
if (activeDownload?.Status == DownloadStatus.Completed && activeDownload.LocalPath != null)
|
||||
{
|
||||
Logger.LogDebug("Download completed while waiting, returning path: {Path}", activeDownload.LocalPath);
|
||||
return activeDownload.LocalPath;
|
||||
}
|
||||
|
||||
throw new Exception(activeDownload?.ErrorMessage ?? "Download failed");
|
||||
// Download failed or was cancelled
|
||||
throw new Exception(activeDownload?.ErrorMessage ?? "Download failed while waiting");
|
||||
}
|
||||
|
||||
// Get metadata
|
||||
|
||||
Reference in New Issue
Block a user