Commit 8d6d8ac0 authored by justcoding121's avatar justcoding121

add max cached connections property

parent bbcdb318
......@@ -129,7 +129,7 @@ namespace Titanium.Web.Proxy
{
connectRequest.RequestUri = new Uri("https://" + httpUrl);
bool http2Supproted = false;
bool http2Supported = false;
var alpn = clientHelloInfo.GetAlpn();
if (alpn != null && alpn.Contains(SslApplicationProtocol.Http2))
......@@ -139,8 +139,12 @@ namespace Titanium.Web.Proxy
var connection = await getServerConnection(connectArgs, true,
SslExtensions.Http2ProtocolAsList, cancellationToken);
http2Supproted = connection.NegotiatedApplicationProtocol == SslApplicationProtocol.Http2;
tcpConnectionFactory.Release(connection, true);
http2Supported = connection.NegotiatedApplicationProtocol == SslApplicationProtocol.Http2;
//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;
......@@ -156,7 +160,7 @@ namespace Titanium.Web.Proxy
// Successfully managed to authenticate the client using the fake certificate
var options = new SslServerAuthenticationOptions();
if (http2Supproted)
if (http2Supported)
{
options.ApplicationProtocols = clientHelloInfo.GetAlpn();
if (options.ApplicationProtocols == null || options.ApplicationProtocols.Count == 0)
......@@ -202,7 +206,7 @@ namespace Titanium.Web.Proxy
{
// create new connection
var connection = await getServerConnection(connectArgs, true,
clientConnection.NegotiatedApplicationProtocol, cancellationToken);
null, cancellationToken);
if (isClientHello)
{
......
......@@ -127,6 +127,13 @@ namespace Titanium.Web.Proxy.Network.Tcp
{
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);
break;
}
......
......@@ -165,6 +165,14 @@ namespace Titanium.Web.Proxy
/// </summary>
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>
/// Total number of active client connections.
/// </summary>
......
......@@ -196,13 +196,16 @@ namespace Titanium.Web.Proxy
connectionChanged = true;
}
//for connection pool retry attempt until we get a good connection
int attemt = 0;
while (attemt < 3)
//for connection pool retry fails until cache is exhausted
//In future once connection pool becomes stable
//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
{
// 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)
{
await handleWebSocketUpgrade(httpCmd, args, request,
......@@ -217,7 +220,7 @@ namespace Titanium.Web.Proxy
//connection pool retry
catch (ServerConnectionException)
{
if (!connectionChanged || !EnableConnectionPool || attemt == 3)
if (!connectionChanged || !EnableConnectionPool || attempt == MaxCachedConnections + 1)
{
throw;
}
......@@ -226,7 +229,7 @@ namespace Titanium.Web.Proxy
tcpConnectionFactory.Release(serverConnection, true);
serverConnection = await getServerConnection(args, false,
clientConnection.NegotiatedApplicationProtocol, cancellationToken);
attemt++;
attempt++;
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