diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-10-17 17:51:37 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-17 17:51:37 -0500 |
| commit | c2dca40e1369cc4d999a05d365c2d19151c90f15 (patch) | |
| tree | 111bd3f7df7ba5c5cd7ee272d81755545adb8ccd /src/slskd/Transfers/API/Controllers | |
| parent | 4d9c12858f64e59c470aa29db3e708781c7b0873 (diff) | |
| parent | 41e59c5aeea36def2832847e3aabd3023d2cc8f6 (diff) | |
| download | slskd-c2dca40e1369cc4d999a05d365c2d19151c90f15.tar.gz | |
Merge pull request #1483 from slskd/download-semaphore
Add rate limiting to search and download requests
Diffstat (limited to 'src/slskd/Transfers/API/Controllers')
| -rw-r--r-- | src/slskd/Transfers/API/Controllers/TransfersController.cs | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index fdbca1dc..0afb79ca 100644 --- a/src/slskd/Transfers/API/Controllers/TransfersController.cs +++ b/src/slskd/Transfers/API/Controllers/TransfersController.cs @@ -23,6 +23,7 @@ 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; @@ -51,6 +52,7 @@ namespace slskd.Transfers.API OptionsSnapshot = optionsSnapshot; } + private static SemaphoreSlim DownloadRequestLimiter { get; } = new SemaphoreSlim(1, 1); private ITransferService Transfers { get; } private IOptionsSnapshot<Options> OptionsSnapshot { get; } @@ -204,22 +206,35 @@ 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) { return StatusCode(500, ex.Message); } + finally + { + DownloadRequestLimiter.Release(); + } } /// <summary> |
