Commit 1e7b53db authored by Stéphane Graziano's avatar Stéphane Graziano

EnableThreadPoolOptimizing option (increase ThreadPool for new connection and...

EnableThreadPoolOptimizing option (increase ThreadPool for new connection and decrease when connection close)
parent 0f19fea8
...@@ -57,6 +57,12 @@ namespace Titanium.Web.Proxy ...@@ -57,6 +57,12 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
private WinHttpWebProxyFinder systemProxyResolver; private WinHttpWebProxyFinder systemProxyResolver;
/// <summary>
///
/// </summary>
private object lockThreadPoolSettings = 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 +179,12 @@ namespace Titanium.Web.Proxy ...@@ -173,6 +179,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 +733,38 @@ namespace Titanium.Web.Proxy ...@@ -721,13 +733,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 (lockThreadPoolSettings)
{
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