From b5b4f6d7cbc1bdfaa0c4b659030872e83facafdb Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 10 Oct 2025 09:31:58 -0500 Subject: prime the peer connection cache before starting downloads --- src/slskd/Transfers/Downloads/DownloadService.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Transfers/Downloads/DownloadService.cs b/src/slskd/Transfers/Downloads/DownloadService.cs index 6a149ff8..c8f82167 100644 --- a/src/slskd/Transfers/Downloads/DownloadService.cs +++ b/src/slskd/Transfers/Downloads/DownloadService.cs @@ -25,6 +25,8 @@ namespace slskd.Transfers.Downloads using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; + using System.Net; + using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; @@ -477,7 +479,7 @@ namespace slskd.Transfers.Downloads /// Thrown when the username is null or an empty string. /// Thrown when no files are requested. /// Thrown when at least one of the requested files throws. - public Task<(List Enqueued, List Failed)> EnqueueAsync(string username, IEnumerable<(string Filename, long Size)> files, CancellationToken cancellationToken = default) + public async Task<(List Enqueued, List Failed)> EnqueueAsync(string username, IEnumerable<(string Filename, long Size)> files, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(username)) { @@ -613,6 +615,17 @@ namespace slskd.Transfers.Downloads return (enqueued, failed); } + // prime the cache for this user, to 1) make sure we can connect, and 2) avoid needing to race for it in the loop + try + { + await Client.ConnectToUserAsync(username, invalidateCache: false, cancellationToken); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to connect to {Username}: {Message}", username, ex.Message); + throw; + } + /* the files are all now in the database in the Queued | Locally state. @@ -794,7 +807,7 @@ namespace slskd.Transfers.Downloads Log.Error(task.Exception, "Task for enqueue of {Count} files from {Username} did not complete successfully: {Error}", files.Count(), username, task.Exception.Flatten().Message); }, cancellationToken: CancellationToken.None); - return Task.FromResult((enqueued, failed)); + return (enqueued, failed); } finally { -- cgit v1.2.3