Skip to content

Commit ccab30c

Browse files
authored
Update minThreads for dynamic sku (#4736)
* Update minThreads for dynamic sku * update comment
1 parent 02f83de commit ccab30c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/WebJobs.Script.WebHost/Program.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Linq;
6+
using System.Threading;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.Azure.WebJobs.Script.WebHost.Configuration;
89
using Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection;
@@ -93,6 +94,27 @@ private static void InitializeProcess()
9394
{
9495
SystemEnvironment.Instance.SetEnvironmentVariable(DataProtectionCostants.AzureWebsiteEnvironmentMachineKey, authEncryptionKey);
9596
}
97+
98+
ConfigureMinimumThreads(SystemEnvironment.Instance.IsDynamic());
99+
}
100+
101+
private static void ConfigureMinimumThreads(bool isDynamicSku)
102+
{
103+
// For information on MinThreads, see:
104+
// https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setminthreads?view=netcore-2.2
105+
// https://docs.microsoft.com/en-us/azure/redis-cache/cache-faq#important-details-about-threadpool-growth
106+
// https://blogs.msdn.microsoft.com/perfworld/2010/01/13/how-can-i-improve-the-performance-of-asp-net-by-adjusting-the-clr-thread-throttling-properties/
107+
//
108+
// This behavior can be overridden by using the "ComPlus_ThreadPool_ForceMinWorkerThreads" environment variable (honored by the .NET threadpool).
109+
110+
// The dynamic plan has some limits that mean that a given instance is using effectively a single core, so we should not use Environment.Processor count in this case.
111+
var effectiveCores = isDynamicSku ? 1 : Environment.ProcessorCount;
112+
113+
// This value was derived by looking at the thread count for several function apps running load on a multicore machine and dividing by the number of cores.
114+
const int minThreadsPerLogicalProcessor = 6;
115+
116+
int minThreadCount = effectiveCores * minThreadsPerLogicalProcessor;
117+
ThreadPool.SetMinThreads(minThreadCount, minThreadCount);
96118
}
97119
}
98120
}

0 commit comments

Comments
 (0)