diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-10-20 09:08:56 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-10-20 09:08:56 -0500 |
| commit | 6bcc23c1856f9878509c940cc37b5d4fdb90e2d4 (patch) | |
| tree | 0574d0032b475f1e0204c724ecdc5f46fac11a55 /src/slskd/Transfers/API/Controllers | |
| parent | 75ad65dbed0333fd6bd0441a5599527c539366e0 (diff) | |
| parent | 0a15a71028c2a12795023550f28d416dd050763d (diff) | |
| download | slskd-6bcc23c1856f9878509c940cc37b5d4fdb90e2d4.tar.gz | |
Merge remote-tracking branch 'origin' into download-records
Diffstat (limited to 'src/slskd/Transfers/API/Controllers')
| -rw-r--r-- | src/slskd/Transfers/API/Controllers/TransfersController.cs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index fdbca1dc..f38c4b87 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(1, 1); 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,36 @@ 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))); + await Transfers.Downloads.EnqueueAsync(username, requests.Select(r => (r.Filename, r.Size)), cancellationToken); return StatusCode(201); } 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> |
