diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-10-17 08:55:08 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-10-17 08:55:08 -0500 |
| commit | 0a511c76d86109d9e974929752b59b94899d700b (patch) | |
| tree | e29ff0d565bbdaea2dd9c9c7151c1d3ff229027c /src/slskd/Transfers/API/Controllers/TransfersController.cs | |
| parent | 4d9c12858f64e59c470aa29db3e708781c7b0873 (diff) | |
| download | slskd-0a511c76d86109d9e974929752b59b94899d700b.tar.gz | |
limit concurrent download enqueue requests to 1
Diffstat (limited to 'src/slskd/Transfers/API/Controllers/TransfersController.cs')
| -rw-r--r-- | src/slskd/Transfers/API/Controllers/TransfersController.cs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/slskd/Transfers/API/Controllers/TransfersController.cs b/src/slskd/Transfers/API/Controllers/TransfersController.cs index fdbca1dc..a45e4bd1 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,13 +206,18 @@ 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) + public async Task<IActionResult> EnqueueAsync([FromRoute, Required] string username, [FromBody] IEnumerable<QueueDownloadRequest> requests, CancellationToken cancellationToken) { 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))); @@ -220,6 +227,10 @@ namespace slskd.Transfers.API { return StatusCode(500, ex.Message); } + finally + { + DownloadRequestLimiter.Release(); + } } /// <summary> |
