Commit 33d50f32 authored by justcoding121's avatar justcoding121

Fix -ive server connection count

parent e662451f
...@@ -37,6 +37,7 @@ namespace Titanium.Web.Proxy ...@@ -37,6 +37,7 @@ namespace Titanium.Web.Proxy
Task<TcpServerConnection> prefetchConnectionTask = null; Task<TcpServerConnection> prefetchConnectionTask = null;
bool closeServerConnection = false; bool closeServerConnection = false;
bool calledRequestHandler = false;
try try
{ {
...@@ -289,7 +290,7 @@ namespace Titanium.Web.Proxy ...@@ -289,7 +290,7 @@ namespace Titanium.Web.Proxy
await tcpConnectionFactory.Release(connection, true); await tcpConnectionFactory.Release(connection, true);
} }
} }
calledRequestHandler = true;
// Now create the request // Now create the request
await handleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter, await handleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter,
cancellationTokenSource, connectHostname, connectArgs?.WebSession.ConnectRequest, prefetchConnectionTask); cancellationTokenSource, connectHostname, connectArgs?.WebSession.ConnectRequest, prefetchConnectionTask);
...@@ -316,7 +317,8 @@ namespace Titanium.Web.Proxy ...@@ -316,7 +317,8 @@ namespace Titanium.Web.Proxy
} }
finally finally
{ {
if (prefetchConnectionTask != null) if (!calledRequestHandler
&& prefetchConnectionTask != null)
{ {
var connection = await prefetchConnectionTask; var connection = await prefetchConnectionTask;
await tcpConnectionFactory.Release(connection, closeServerConnection); await tcpConnectionFactory.Release(connection, closeServerConnection);
......
...@@ -42,6 +42,29 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -42,6 +42,29 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal ProxyServer server { get; set; } internal ProxyServer server { get; set; }
internal string GetConnectionCacheKey(string remoteHostName, int remotePort,
Version httpVersion, bool isHttps, List<SslApplicationProtocol> applicationProtocols, bool isConnect,
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy)
{
var cacheKeyBuilder = new StringBuilder($"{remoteHostName}-{remotePort}" +
$"-{(httpVersion == null ? string.Empty : httpVersion.ToString())}" +
$"-{isHttps}-{isConnect}-");
if (applicationProtocols != null)
{
foreach (var protocol in applicationProtocols)
{
cacheKeyBuilder.Append($"{protocol}-");
}
}
cacheKeyBuilder.Append(upStreamEndPoint != null
? $"{upStreamEndPoint.Address}-{upStreamEndPoint.Port}-"
: string.Empty);
cacheKeyBuilder.Append(externalProxy != null ? $"{externalProxy.GetCacheKey()}-" : string.Empty);
return cacheKeyBuilder.ToString();
}
/// <summary> /// <summary>
/// Gets a TCP connection to server from connection pool. /// Gets a TCP connection to server from connection pool.
/// </summary> /// </summary>
...@@ -61,23 +84,9 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -61,23 +84,9 @@ namespace Titanium.Web.Proxy.Network.Tcp
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy, ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var cacheKeyBuilder = new StringBuilder($"{remoteHostName}-{remotePort}" + var cacheKey = GetConnectionCacheKey(remoteHostName, remotePort,
$"-{(httpVersion == null ? string.Empty : httpVersion.ToString())}" + httpVersion, isHttps, applicationProtocols, isConnect,
$"-{isHttps}-{isConnect}-"); proxyServer, upStreamEndPoint, externalProxy);
if (applicationProtocols != null)
{
foreach (var protocol in applicationProtocols)
{
cacheKeyBuilder.Append($"{protocol}-");
}
}
cacheKeyBuilder.Append(upStreamEndPoint != null
? $"{upStreamEndPoint.Address}-{upStreamEndPoint.Port}-"
: string.Empty);
cacheKeyBuilder.Append(externalProxy != null ? $"{externalProxy.GetCacheKey()}-" : string.Empty);
string cacheKey = cacheKeyBuilder.ToString();
if (proxyServer.EnableConnectionPool) if (proxyServer.EnableConnectionPool)
{ {
......
This diff is collapsed.
...@@ -35,6 +35,7 @@ namespace Titanium.Web.Proxy ...@@ -35,6 +35,7 @@ namespace Titanium.Web.Proxy
Task<TcpServerConnection> prefetchConnectionTask = null; Task<TcpServerConnection> prefetchConnectionTask = null;
bool closeServerConnection = false; bool closeServerConnection = false;
bool calledRequestHandler = false;
try try
{ {
...@@ -127,7 +128,7 @@ namespace Titanium.Web.Proxy ...@@ -127,7 +128,7 @@ namespace Titanium.Web.Proxy
} }
} }
calledRequestHandler = true;
// HTTPS server created - we can now decrypt the client's traffic // HTTPS server created - we can now decrypt the client's traffic
// Now create the request // Now create the request
await handleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter, await handleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter,
...@@ -155,7 +156,8 @@ namespace Titanium.Web.Proxy ...@@ -155,7 +156,8 @@ namespace Titanium.Web.Proxy
} }
finally finally
{ {
if (prefetchConnectionTask != null) if (!calledRequestHandler
&& prefetchConnectionTask != null)
{ {
var connection = await prefetchConnectionTask; var connection = await prefetchConnectionTask;
await tcpConnectionFactory.Release(connection, closeServerConnection); await tcpConnectionFactory.Release(connection, closeServerConnection);
......
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