From 587b49aaba36e8d3c524ef27788896b85b42ad22 Mon Sep 17 00:00:00 2001 From: Leonardo Galli Date: Tue, 7 Aug 2018 23:20:58 +0200 Subject: [PATCH] Added: The ability to set the number of threads (NOT SUPPORTED!) to use for tasks using THREAD_LIMIT environment variable. --- .../Messaging/Commands/CommandExecutor.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs index ecb96d9be..bd02b00c9 100644 --- a/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs +++ b/src/NzbDrone.Core/Messaging/Commands/CommandExecutor.cs @@ -17,7 +17,9 @@ namespace NzbDrone.Core.Messaging.Commands private readonly IEventAggregator _eventAggregator; private static CancellationTokenSource _cancellationTokenSource; - private const int THREAD_LIMIT = 3; + private const int THREAD_UPPER_BOUND = 10; + private const int THREAD_LOWER_BOUND = 2; + private const int THREAD_LIMIT = 2; public CommandExecutor(IServiceFactory serviceFactory, IManageCommandQueue commandQueueManager, @@ -117,7 +119,16 @@ namespace NzbDrone.Core.Messaging.Commands { _cancellationTokenSource = new CancellationTokenSource(); - for (int i = 0; i < THREAD_LIMIT; i++) + var envLimit = Environment.GetEnvironmentVariable("THREAD_LIMIT") ?? $"{THREAD_LIMIT}"; + int threadLimit = THREAD_LIMIT; + if (int.TryParse(envLimit, out int parsedLimit)) threadLimit = parsedLimit; + + threadLimit = Math.Max(THREAD_LOWER_BOUND, threadLimit); + threadLimit = Math.Min(THREAD_UPPER_BOUND, threadLimit); + + _logger.Info("Starting {} threads for tasks.", threadLimit); + + for (int i = 0; i < threadLimit + 1; i++) { var thread = new Thread(ExecuteCommands); thread.Start();