Set minthreads on webhost start
MODIFY
src/WebJobs.Script.WebHost/Global.asax.cs
src/WebJobs.Script.WebHost/Global.asax.cs
using System;
using System.Linq;
using System.Net;
+using System.Threading;
using System.Web.Http;
using Microsoft.Azure.WebJobs.Script.Config;
using Microsoft.Azure.WebJobs.Script.Diagnostics;
@@ -23,6 +24,7 @@ protected void Application_Start()
ServicePointManager.DefaultConnectionLimit = ScriptConstants.DynamicSkuConnectionLimit;
}
+ ConfigureMinimumThreads(settingsManager.IsDynamicSku);
VerifyAndEnableShadowCopy(webHostSettings);
using (var metricsLogger = new WebHostMetricsLogger())
@@ -45,6 +47,25 @@ private static void VerifyAndEnableShadowCopy(WebHostSettings webHostSettings)
}
}
+ private static void ConfigureMinimumThreads(bool isDynamicSku)
+ {
+ // For information on MinThreads, see:
+ // https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setminthreads?view=netframework-4.7
+ // https://docs.microsoft.com/en-us/azure/redis-cache/cache-faq#important-details-about-threadpool-growth
+ // 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/
+ //
+ // This behavior can be overridden by using the "ComPlus_ThreadPool_ForceMinWorkerThreads" environment variable (honored by the .NET threadpool).
+
+ // 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.
+ var effectiveCores = isDynamicSku ? 1 : Environment.ProcessorCount;
+
+ // 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.
+ const int minThreadsPerLogicalProcessor = 6;
+
+ int minThreadCount = effectiveCores * minThreadsPerLogicalProcessor;
+ ThreadPool.SetMinThreads(minThreadCount, minThreadCount);
+ }
+
internal static string GetShadowCopyPath(string currentShadowCopyDirectories, string scriptPath)
{
return string.Join(";", new[] { currentShadowCopyDirectories, scriptPath }.Where(s => !string.IsNullOrEmpty(s)));

Set minthreads on webhost start