Skip to content

Commit

Permalink
Merge pull request #1047 from aksio-insurtech:fix/max-parallelism
Browse files Browse the repository at this point in the history
Fix/max-parallelism
  • Loading branch information
einari committed Dec 12, 2023
2 parents e87e6b3 + bf0b623 commit 802168f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
19 changes: 19 additions & 0 deletions Source/Kernel/Grains/Workers/InvalidMaximumConcurrencyLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Aksio Insurtech. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Aksio.Cratis.Kernel.Grains.Workers;

/// <summary>
/// Exception that gets thrown when the maximum concurrency level is invalid.
/// </summary>
public class InvalidMaximumConcurrencyLevel : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="InvalidMaximumConcurrencyLevel"/> class.
/// </summary>
/// <param name="maxDegreeOfParallelism">The degree of parallelism configured.</param>
public InvalidMaximumConcurrencyLevel(int maxDegreeOfParallelism)
: base($"Invalid maximum concurrency level: {maxDegreeOfParallelism}. Must be at least 1.")
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class LimitedConcurrencyLevelTaskScheduler : TaskScheduler
/// <param name="maxDegreeOfParallelism">The maximum degree of parallelism provided by this scheduler.</param>
public LimitedConcurrencyLevelTaskScheduler(int maxDegreeOfParallelism)
{
if (maxDegreeOfParallelism < 1) throw new ArgumentOutOfRangeException(nameof(maxDegreeOfParallelism));
if (maxDegreeOfParallelism < 1) throw new InvalidMaximumConcurrencyLevel(maxDegreeOfParallelism);
_maxDegreeOfParallelism = maxDegreeOfParallelism;
}

Expand Down
8 changes: 7 additions & 1 deletion Source/Kernel/Server/CpuBoundWorkersExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Aksio.Cratis.Kernel.Grains.Workers;
using Serilog;

#pragma warning disable SA1600
namespace Aksio.Cratis.Kernel.Server;
Expand All @@ -18,11 +19,16 @@ public static class CpuBoundWorkersExtensions
/// <returns><see cref="ISiloBuilder"/> for continuation.</returns>
public static IHostBuilder ConfigureCpuBoundWorkers(this IHostBuilder builder)
{
Log.Logger.Information("Configuring Cpu bound workers");

var maxLevelOfParallelism = Environment.ProcessorCount - 2;
if (maxLevelOfParallelism < 0)
if (maxLevelOfParallelism <= 0)
{
maxLevelOfParallelism = 1;
}

Log.Logger.Information("Max level of parallelism for Cpu bound workers: {MaxLevelOfParallelism} - number of processor count {ProcessorCount}", maxLevelOfParallelism, Environment.ProcessorCount);

builder.ConfigureServices((services) => services.AddSingleton(new LimitedConcurrencyLevelTaskScheduler(maxLevelOfParallelism)));

return builder;
Expand Down

0 comments on commit 802168f

Please sign in to comment.