Commit 8d6d8ac0 authored by justcoding121's avatar justcoding121

add max cached connections property

parent bbcdb318
...@@ -129,7 +129,7 @@ namespace Titanium.Web.Proxy ...@@ -129,7 +129,7 @@ namespace Titanium.Web.Proxy
{ {
connectRequest.RequestUri = new Uri("https://" + httpUrl); connectRequest.RequestUri = new Uri("https://" + httpUrl);
bool http2Supproted = false; bool http2Supported = false;
var alpn = clientHelloInfo.GetAlpn(); var alpn = clientHelloInfo.GetAlpn();
if (alpn != null && alpn.Contains(SslApplicationProtocol.Http2)) if (alpn != null && alpn.Contains(SslApplicationProtocol.Http2))
...@@ -139,8 +139,12 @@ namespace Titanium.Web.Proxy ...@@ -139,8 +139,12 @@ namespace Titanium.Web.Proxy
var connection = await getServerConnection(connectArgs, true, var connection = await getServerConnection(connectArgs, true,
SslExtensions.Http2ProtocolAsList, cancellationToken); SslExtensions.Http2ProtocolAsList, cancellationToken);
http2Supproted = connection.NegotiatedApplicationProtocol == SslApplicationProtocol.Http2; http2Supported = connection.NegotiatedApplicationProtocol == SslApplicationProtocol.Http2;
tcpConnectionFactory.Release(connection, true);
//release connection back to pool intead of closing when connection pool is enabled
//In future we can connect to server preemptively here for all HTTPS connections using connect hostname
//and then release it back to pool so that we can save time when reading client headers
tcpConnectionFactory.Release(connection, !EnableConnectionPool);
} }
SslStream sslStream = null; SslStream sslStream = null;
...@@ -156,7 +160,7 @@ namespace Titanium.Web.Proxy ...@@ -156,7 +160,7 @@ namespace Titanium.Web.Proxy
// Successfully managed to authenticate the client using the fake certificate // Successfully managed to authenticate the client using the fake certificate
var options = new SslServerAuthenticationOptions(); var options = new SslServerAuthenticationOptions();
if (http2Supproted) if (http2Supported)
{ {
options.ApplicationProtocols = clientHelloInfo.GetAlpn(); options.ApplicationProtocols = clientHelloInfo.GetAlpn();
if (options.ApplicationProtocols == null || options.ApplicationProtocols.Count == 0) if (options.ApplicationProtocols == null || options.ApplicationProtocols.Count == 0)
...@@ -202,7 +206,7 @@ namespace Titanium.Web.Proxy ...@@ -202,7 +206,7 @@ namespace Titanium.Web.Proxy
{ {
// create new connection // create new connection
var connection = await getServerConnection(connectArgs, true, var connection = await getServerConnection(connectArgs, true,
clientConnection.NegotiatedApplicationProtocol, cancellationToken); null, cancellationToken);
if (isClientHello) if (isClientHello)
{ {
......
...@@ -127,6 +127,13 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -127,6 +127,13 @@ namespace Titanium.Web.Proxy.Network.Tcp
{ {
if (cache.TryGetValue(connection.CacheKey, out var existingConnections)) if (cache.TryGetValue(connection.CacheKey, out var existingConnections))
{ {
while (existingConnections.Count >= server.MaxCachedConnections)
{
if (existingConnections.TryDequeue(out var staleConnection))
{
disposalBag.Add(staleConnection);
}
}
existingConnections.Enqueue(connection); existingConnections.Enqueue(connection);
break; break;
} }
......
...@@ -165,6 +165,14 @@ namespace Titanium.Web.Proxy ...@@ -165,6 +165,14 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
public int ConnectionTimeOutSeconds { get; set; } public int ConnectionTimeOutSeconds { get; set; }
/// <summary>
/// Maximum number of concurrent connections per remote host in cache.
/// Only valid when connection pooling is enabled.
/// Default value is 3.
/// </summary>
public int MaxCachedConnections { get; set; } = 3;
/// <summary> /// <summary>
/// Total number of active client connections. /// Total number of active client connections.
/// </summary> /// </summary>
......
...@@ -196,13 +196,16 @@ namespace Titanium.Web.Proxy ...@@ -196,13 +196,16 @@ namespace Titanium.Web.Proxy
connectionChanged = true; connectionChanged = true;
} }
//for connection pool retry attempt until we get a good connection //for connection pool retry fails until cache is exhausted
int attemt = 0; //In future once connection pool becomes stable
while (attemt < 3) //we can get connection for each HTTP session instead of per client connection
//That will be more efficient especially when client is holding connection but not using it
int attempt = 0;
while (attempt < MaxCachedConnections + 1)
{ {
try try
{ {
// if upgrading to websocket then relay the requet without reading the contents // if upgrading to websocket then relay the request without reading the contents
if (request.UpgradeToWebSocket) if (request.UpgradeToWebSocket)
{ {
await handleWebSocketUpgrade(httpCmd, args, request, await handleWebSocketUpgrade(httpCmd, args, request,
...@@ -217,7 +220,7 @@ namespace Titanium.Web.Proxy ...@@ -217,7 +220,7 @@ namespace Titanium.Web.Proxy
//connection pool retry //connection pool retry
catch (ServerConnectionException) catch (ServerConnectionException)
{ {
if (!connectionChanged || !EnableConnectionPool || attemt == 3) if (!connectionChanged || !EnableConnectionPool || attempt == MaxCachedConnections + 1)
{ {
throw; throw;
} }
...@@ -226,7 +229,7 @@ namespace Titanium.Web.Proxy ...@@ -226,7 +229,7 @@ namespace Titanium.Web.Proxy
tcpConnectionFactory.Release(serverConnection, true); tcpConnectionFactory.Release(serverConnection, true);
serverConnection = await getServerConnection(args, false, serverConnection = await getServerConnection(args, false,
clientConnection.NegotiatedApplicationProtocol, cancellationToken); clientConnection.NegotiatedApplicationProtocol, cancellationToken);
attemt++; attempt++;
continue; continue;
} }
......
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