Skip to content

Commit 55a3d1d

Browse files
authored
Optional configurable thread settings (#7870)
* threading settings. * Proper source name
1 parent 9c88a6d commit 55a3d1d

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

src/AccountDeleter/Configuration/GalleryConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,9 @@ public string SiteRoot
108108
public bool SelfServiceAccountDeleteEnabled { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
109109
public string DeploymentLabel { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
110110
public string UsabillaFeedbackButtonId { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
111+
public int? MinWorkerThreads { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
112+
public int? MaxWorkerThreads { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
113+
public int? MinIoThreads { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
114+
public int? MaxIoThreads { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
111115
}
112116
}

src/NuGetGallery.Services/Configuration/AppConfiguration.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,5 +404,14 @@ public string ExternalBrandingMessage
404404
public string DeploymentLabel { get; set; }
405405

406406
public string UsabillaFeedbackButtonId { get; set; }
407+
408+
[DefaultValue(null)]
409+
public int? MinWorkerThreads { get; set; }
410+
[DefaultValue(null)]
411+
public int? MaxWorkerThreads { get; set; }
412+
[DefaultValue(null)]
413+
public int? MinIoThreads { get; set; }
414+
[DefaultValue(null)]
415+
public int? MaxIoThreads { get; set; }
407416
}
408417
}

src/NuGetGallery.Services/Configuration/IAppConfiguration.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,5 +455,41 @@ public interface IAppConfiguration : IMessageServiceConfiguration
455455
/// in your button's JavaScript code. Look for "//w.usabilla.com/{button ID}.js".
456456
/// </summary>
457457
string UsabillaFeedbackButtonId { get; set; }
458+
459+
/// <summary>
460+
/// Allows to override the default (or externally configured) minWorkerThreads setting. Must be specified with <see cref="MinIoThreads"/>.
461+
/// This is a global (not per CPU core) limit.
462+
/// See more:
463+
/// https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setminthreads?view=netframework-4.7.2
464+
/// https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/7w2sway1(v=vs.100)?redirectedfrom=MSDN
465+
/// </summary>
466+
int? MinWorkerThreads { get; set; }
467+
468+
/// <summary>
469+
/// Allows to override the default (or externally configured) maxWorkerThreads setting. Must be specified with <see cref="MaxIoThreads"/>
470+
/// This is a global (not per CPU core) limit.
471+
/// See more:
472+
/// https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setmaxthreads?view=netframework-4.7.2
473+
/// https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/7w2sway1(v=vs.100)?redirectedfrom=MSDN
474+
/// </summary>
475+
int? MaxWorkerThreads { get; set; }
476+
477+
/// <summary>
478+
/// Allows to override the default (or externally configured) minIoThreads setting. Must be specified with <see cref="MinWorkerThreads"/>.
479+
/// This is a global (not per CPU core) limit.
480+
/// See more:
481+
/// https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setminthreads?view=netframework-4.7.2
482+
/// https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/7w2sway1(v=vs.100)?redirectedfrom=MSDN
483+
/// </summary>
484+
int? MinIoThreads { get; set; }
485+
486+
/// <summary>
487+
/// Allows to override the default (or externally configured) maxIoThreads setting. Must be specified with <see cref="MaxWorkerThreads"/>.
488+
/// This is a global (not per CPU core) limit.
489+
/// See more:
490+
/// https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setmaxthreads?view=netframework-4.7.2
491+
/// https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/7w2sway1(v=vs.100)?redirectedfrom=MSDN
492+
/// </summary>
493+
int? MaxIoThreads { get; set; }
458494
}
459495
}

src/NuGetGallery/App_Start/OwinStartup.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using System.Net;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using System.Web;
1011
using System.Web.Hosting;
1112
using System.Web.Http;
1213
using System.Web.Mvc;
1314
using Elmah;
15+
using Microsoft.Extensions.Logging;
1416
using Microsoft.Owin;
1517
using Microsoft.Owin.Logging;
1618
using Microsoft.Owin.Security;
@@ -20,9 +22,12 @@
2022
using NuGetGallery.Authentication.Providers;
2123
using NuGetGallery.Authentication.Providers.Cookie;
2224
using NuGetGallery.Configuration;
25+
using NuGetGallery.Diagnostics;
2326
using NuGetGallery.Infrastructure;
2427
using Owin;
2528

29+
using ILoggerFactory = Microsoft.Extensions.Logging.ILoggerFactory;
30+
2631
[assembly: OwinStartup(typeof(NuGetGallery.OwinStartup))]
2732

2833
namespace NuGetGallery
@@ -91,6 +96,24 @@ public static void Configuration(IAppBuilder app)
9196
}
9297
}
9398

99+
var tds = new TraceDiagnosticsSource(nameof(OwinStartup), dependencyResolver.GetService<ITelemetryClient>());
100+
if (config.Current.MaxWorkerThreads.HasValue && config.Current.MaxIoThreads.HasValue)
101+
{
102+
int defaultMaxWorkerThreads, defaultMaxIoThreads;
103+
ThreadPool.GetMaxThreads(out defaultMaxWorkerThreads, out defaultMaxIoThreads);
104+
tds.Information($"Default maxWorkerThreads: {defaultMaxWorkerThreads}, maxIoThreads: {defaultMaxIoThreads}");
105+
var success = ThreadPool.SetMaxThreads(config.Current.MaxWorkerThreads.Value, config.Current.MaxIoThreads.Value);
106+
tds.Information($"Attempt to update max threads to {config.Current.MaxWorkerThreads.Value}, {config.Current.MaxIoThreads.Value}, success: {success}");
107+
}
108+
if (config.Current.MinWorkerThreads.HasValue && config.Current.MinIoThreads.HasValue)
109+
{
110+
int defaultMinWorkerThreads, defaultMinIoThreads;
111+
ThreadPool.GetMinThreads(out defaultMinWorkerThreads, out defaultMinIoThreads);
112+
tds.Information($"Default minWorkerThreads: {defaultMinWorkerThreads}, minIoThreads: {defaultMinIoThreads}");
113+
var success = ThreadPool.SetMinThreads(config.Current.MinWorkerThreads.Value, config.Current.MinIoThreads.Value);
114+
tds.Information($"Attempt to update min threads to {config.Current.MinWorkerThreads.Value}, {config.Current.MinIoThreads.Value}, success: {success}");
115+
}
116+
94117
// Get the local user auth provider, if present and attach it first
95118
Authenticator localUserAuthenticator;
96119
if (auth.Authenticators.TryGetValue(Authenticator.GetName(typeof(LocalUserAuthenticator)), out localUserAuthenticator))

0 commit comments

Comments
 (0)