diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-22 18:36:31 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-22 18:36:31 -0600 |
| commit | 47490e366b5bc4b2a1679fba50655697afbe7137 (patch) | |
| tree | ae98bb4fb66d20e5fecd69267bcbb17d80c4986b /src/slskd/Transfers/API/Controllers | |
| parent | 2e073014a003fc4e972a9a2677fccd3d30074cd2 (diff) | |
| parent | 6ba7311d48b95ca75f49b3b28c1a8db810f87fe1 (diff) | |
| download | slskd-47490e366b5bc4b2a1679fba50655697afbe7137.tar.gz | |
Merge branch 'master' into dependabot/npm_and_yarn/src/web/axios-0.30.2
Diffstat (limited to 'src/slskd/Transfers/API/Controllers')
| -rw-r--r-- | src/slskd/Transfers/API/Controllers/TransfersController.cs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index fdbca1dc..6be11b2d 100644 --- a/src/slskd/Transfers/API/Controllers/TransfersController.cs +++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs @@ -23,10 +23,12 @@ namespace slskd.Transfers.API using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; + using System.Threading; using System.Threading.Tasks; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using Serilog; /// <summary> /// Transfers. @@ -51,8 +53,10 @@ namespace slskd.Transfers.API OptionsSnapshot = optionsSnapshot; } + private static SemaphoreSlim DownloadRequestLimiter { get; } = new SemaphoreSlim(2, 2); private ITransferService Transfers { get; } private IOptionsSnapshot<Options> OptionsSnapshot { get; } + private ILogger Log { get; set; } = Serilog.Log.ForContext<TransfersController>(); /// <summary> /// Cancels the specified download. @@ -204,22 +208,37 @@ namespace slskd.Transfers.API [ProducesResponseType(201)] [ProducesResponseType(typeof(string), 403)] [ProducesResponseType(typeof(string), 500)] - public async Task<IActionResult> EnqueueAsync([FromRoute, Required] string username, [FromBody] IEnumerable<QueueDownloadRequest> requests) +#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do) +#pragma warning disable SA1611 // Element parameters should be documented + public async Task<IActionResult> EnqueueAsync([FromRoute, Required] string username, [FromBody] IEnumerable<QueueDownloadRequest> requests, CancellationToken cancellationToken = default) +#pragma warning restore SA1611 // Element parameters should be documented +#pragma warning restore CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do) { if (Program.IsRelayAgent) { return Forbid(); } + if (!DownloadRequestLimiter.Wait(0)) + { + return StatusCode(429, "Only one concurrent operation is permitted. Wait until the previous request completes"); + } + try { - await Transfers.Downloads.EnqueueAsync(username, requests.Select(r => (r.Filename, r.Size))); - return StatusCode(201); + var (enqueued, failed) = await Transfers.Downloads.EnqueueAsync(username, requests.Select(r => (r.Filename, r.Size)), cancellationToken); + + return StatusCode(201, new { Enqueued = enqueued, Failed = failed }); } catch (Exception ex) { + Log.Error(ex, "Failed to enqueue {Count} files for {Username}: {Message}", requests.Count(), username, ex.Message); return StatusCode(500, ex.Message); } + finally + { + DownloadRequestLimiter.Release(); + } } /// <summary> |
