Skip to content

Commit

Permalink
Calculate MaxInstance fix (#399)
Browse files Browse the repository at this point in the history
* MaxInstance fist checks environment variable, then the app domain value, then the default value.
  • Loading branch information
coenm authored Sep 23, 2023
1 parent 3de00aa commit fc489ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/mdsource/diff-tool.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ include: diffToolCleanup

By default a maximum of 5 tool instances will be launched. This prevents a change that breaks many tests from causing too much load on a machine.

This value can be changed:
This value can be changed using an environment variable or by explicitly specifying the value by code. When both are used, the environment variable value will be used.


### Using an environment variable
Expand Down
37 changes: 15 additions & 22 deletions src/DiffEngine/MaxInstance.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
static class MaxInstance
static class MaxInstance
{
public static int MaxInstancesToLaunch { get; private set; } = GetMaxInstances();
public static int MaxInstancesToLaunch => capturedMaxInstancesToLaunch ??= GetMaxInstances();
static int? capturedMaxInstancesToLaunch;
static int? appDomainMaxInstancesToLaunch;
static int launchedInstances;
const int defaultMax = 5;

static int GetMaxInstances()
static int GetMaxInstances() => GetEnvironmentValue() ?? appDomainMaxInstancesToLaunch ?? defaultMax;

static int? GetEnvironmentValue()
{
var variable = Environment.GetEnvironmentVariable("DiffEngine_MaxInstances");

if (string.IsNullOrEmpty(variable))
{
return defaultMax;
return null;
}

if (ushort.TryParse(variable, out var result))
Expand All @@ -21,31 +25,20 @@ static int GetMaxInstances()
throw new($"Could not parse the DiffEngine_MaxInstances environment variable: {variable}");
}

static void ResetCapturedValue() => capturedMaxInstancesToLaunch = null;

public static void SetForAppDomain(int value)
{
Guard.AgainstNegativeAndZero(value, nameof(value));
MaxInstancesToLaunch = value;
appDomainMaxInstancesToLaunch = value;
ResetCapturedValue();
}

public static void SetForUser(int value)
{
if (MaxInstancesToLaunch == value)
{
return;
}

MaxInstancesToLaunch = value;
string? envVariable;
if (value == defaultMax)
{
envVariable = null;
}
else
{
envVariable = value.ToString();
}

Environment.SetEnvironmentVariable("DiffEngine_MaxInstances", envVariable, EnvironmentVariableTarget.User);
Guard.AgainstNegativeAndZero(value, nameof(value));
Environment.SetEnvironmentVariable("DiffEngine_MaxInstances", value.ToString(), EnvironmentVariableTarget.User);
ResetCapturedValue();
}

public static bool Reached()
Expand Down

0 comments on commit fc489ef

Please sign in to comment.