Unverified Commit 5815b7b8 authored by Jehonathan Thomas's avatar Jehonathan Thomas Committed by GitHub

Merge pull request #574 from StephaneGraziano/master

Add ThreadPool tuning for better performances when heavy load
parents 0f19fea8 1e2c1c92
...@@ -57,6 +57,11 @@ namespace Titanium.Web.Proxy ...@@ -57,6 +57,11 @@ 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.
...@@ -173,6 +178,12 @@ namespace Titanium.Web.Proxy ...@@ -173,6 +178,12 @@ 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.
...@@ -721,13 +732,38 @@ namespace Titanium.Web.Proxy ...@@ -721,13 +732,38 @@ namespace Titanium.Web.Proxy
if (tcpClient != null) if (tcpClient != null)
{ {
Task.Run(async () => { await handleClient(tcpClient, endPoint); }); SetThreadPoolMinThread(1); // increase the ThreadPool
Task.Run(async () =>
{
await handleClient(tcpClient, endPoint);
SetThreadPoolMinThread(-1); //decrease the Threadpool
});
} }
// Get the listener that handles the client request. // Get the listener that handles the client request.
endPoint.Listener.BeginAcceptTcpClient(onAcceptConnection, endPoint); endPoint.Listener.BeginAcceptTcpClient(onAcceptConnection, endPoint);
} }
/// <summary>
/// Change the ThreadPool.WorkerThread minThread
/// </summary>
/// <param name="piNbWorkerThreadsToAdd">Number of threads to add</param>
void SetThreadPoolMinThread(int piNbWorkerThreadsToAdd)
{
if (EnableThreadPoolOptimizing)
{
lock (lockThreadPoolTuning)
{
int iWorkerThreads, iCompletionPortThreads;
ThreadPool.GetMinThreads(out iWorkerThreads, out iCompletionPortThreads);
iWorkerThreads = Math.Max(iWorkerThreads + piNbWorkerThreadsToAdd, Environment.ProcessorCount);
ThreadPool.SetMinThreads(iWorkerThreads, iCompletionPortThreads);
}
}
}
/// <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