Unverified Commit ff5f92fe authored by Jehonathan Thomas's avatar Jehonathan Thomas Committed by GitHub

Merge pull request #578 from StephaneGraziano/master

Remove ThreadPool auto optimizations, add a manual ThreadPoolWorkerThread
parents 25b05f20 a39bf493
...@@ -57,11 +57,7 @@ namespace Titanium.Web.Proxy ...@@ -57,11 +57,7 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
private WinHttpWebProxyFinder systemProxyResolver; private WinHttpWebProxyFinder systemProxyResolver;
/// <summary>
/// lock for Thread Pool tuning
/// </summary>
private object lockThreadPoolTuning = new object();
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
/// Initializes a new instance of ProxyServer class with provided parameters. /// Initializes a new instance of ProxyServer class with provided parameters.
...@@ -178,12 +174,6 @@ namespace Titanium.Web.Proxy ...@@ -178,12 +174,6 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public bool EnableTcpServerConnectionPrefetch { get; set; } = true; public bool EnableTcpServerConnectionPrefetch { get; set; } = true;
/// <summary>
/// Gets or sets a Boolean value that specifies whether ThreadPool grows new connections and decrease when connections close
/// Defaults to true.
/// </summary>
public bool EnableThreadPoolOptimizing { get; set; } = true;
/// <summary> /// <summary>
/// Gets or sets a Boolean value that specifies whether server and client stream Sockets are using the Nagle algorithm. /// Gets or sets a Boolean value that specifies whether server and client stream Sockets are using the Nagle algorithm.
/// Defaults to true, no nagle algorithm is used. /// Defaults to true, no nagle algorithm is used.
...@@ -363,6 +353,11 @@ namespace Titanium.Web.Proxy ...@@ -363,6 +353,11 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public event AsyncEventHandler<TcpClient> OnServerConnectionCreate; public event AsyncEventHandler<TcpClient> OnServerConnectionCreate;
/// <summary>
/// Customize the minimum ThreadPool size (increase it on a server)
/// </summary>
public int ThreadPoolWorkerThread { get; set; } = Environment.ProcessorCount;
/// <summary> /// <summary>
/// Add a proxy end point. /// Add a proxy end point.
/// </summary> /// </summary>
...@@ -555,6 +550,8 @@ namespace Titanium.Web.Proxy ...@@ -555,6 +550,8 @@ namespace Titanium.Web.Proxy
throw new Exception("Proxy is already running."); throw new Exception("Proxy is already running.");
} }
setThreadPoolMinThread(ThreadPoolWorkerThread);
if (ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.GenericCertificate == null)) if (ProxyEndPoints.OfType<ExplicitProxyEndPoint>().Any(x => x.GenericCertificate == null))
{ {
CertificateManager.EnsureRootCertificate(); CertificateManager.EnsureRootCertificate();
...@@ -732,19 +729,9 @@ namespace Titanium.Web.Proxy ...@@ -732,19 +729,9 @@ namespace Titanium.Web.Proxy
if (tcpClient != null) if (tcpClient != null)
{ {
setThreadPoolMinThread(1); // increase the ThreadPool
Task.Run(async () => Task.Run(async () =>
{ {
try await handleClient(tcpClient, endPoint);
{
await handleClient(tcpClient, endPoint);
}
finally
{
setThreadPoolMinThread(-1); //decrease the Threadpool
}
}); });
} }
...@@ -752,36 +739,24 @@ namespace Titanium.Web.Proxy ...@@ -752,36 +739,24 @@ namespace Titanium.Web.Proxy
endPoint.Listener.BeginAcceptTcpClient(onAcceptConnection, endPoint); endPoint.Listener.BeginAcceptTcpClient(onAcceptConnection, endPoint);
} }
private Lazy<int> maxWorkerThreads = new Lazy<int>(() =>
{
int maxWorkerThreads;
ThreadPool.GetMaxThreads(out maxWorkerThreads, out var _);
return maxWorkerThreads;
});
/// <summary> /// <summary>
/// Change the ThreadPool.WorkerThread minThread /// Change the ThreadPool.WorkerThread minThread
/// </summary> /// </summary>
/// <param name="workerThreadsToAdd">Number of threads to add</param> /// <param name="workerThreadsToAdd">minimum Threads allocated in the ThreadPool</param>
private void setThreadPoolMinThread(int workerThreadsToAdd) private void setThreadPoolMinThread(int workerThreads)
{ {
if (EnableThreadPoolOptimizing) int minWorkerThreads, minCompletionPortThreads, maxWorkerThreads;
{
lock (lockThreadPoolTuning)
{
int minWorkerThreads, minCompletionPortThreads;
ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads); ThreadPool.GetMinThreads(out minWorkerThreads, out minCompletionPortThreads);
minWorkerThreads = Math.Max(minWorkerThreads + workerThreadsToAdd, Environment.ProcessorCount); ThreadPool.GetMaxThreads(out maxWorkerThreads, out _);
if (minWorkerThreads <= maxWorkerThreads.Value) minWorkerThreads = Math.Min(maxWorkerThreads, Math.Max(workerThreads, Environment.ProcessorCount));
{
ThreadPool.SetMinThreads(minWorkerThreads, minCompletionPortThreads); ThreadPool.SetMinThreads(minWorkerThreads, minCompletionPortThreads);
}
}
}
} }
/// <summary> /// <summary>
/// Handle the client. /// Handle the client.
/// </summary> /// </summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment