Commit 022b4762 authored by justcoding121's avatar justcoding121

cache disposal improvement

parent 2b4fd32d
...@@ -55,7 +55,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -55,7 +55,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
$"{isHttps}-"); $"{isHttps}-");
if (applicationProtocols != null) if (applicationProtocols != null)
{ {
foreach (var protocol in applicationProtocols.OrderBy(x=>x)) foreach (var protocol in applicationProtocols.OrderBy(x => x))
{ {
cacheKeyBuilder.Append($"{protocol}-"); cacheKeyBuilder.Append($"{protocol}-");
} }
...@@ -94,22 +94,34 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -94,22 +94,34 @@ namespace Titanium.Web.Proxy.Network.Tcp
if (proxyServer.EnableConnectionPool) if (proxyServer.EnableConnectionPool)
{ {
if (cache.TryGetValue(cacheKey, out var existingConnections)) try
{ {
while (existingConnections.TryDequeue(out var recentConnection)) await @lock.WaitAsync();
{
//+3 seconds for potential delay after getting connection
var cutOff = DateTime.Now.AddSeconds(-1 * proxyServer.ConnectionTimeOutSeconds + 3);
if (recentConnection.LastAccess > cutOff if (cache.TryGetValue(cacheKey, out var existingConnections))
&& isGoodConnection(recentConnection.TcpClient)) {
while (existingConnections.Count > 0)
{ {
return recentConnection; if (existingConnections.TryDequeue(out var recentConnection))
} {
//+3 seconds for potential delay after getting connection
var cutOff = DateTime.Now.AddSeconds(-1 * proxyServer.ConnectionTimeOutSeconds + 3);
disposalBag.Add(recentConnection); if (recentConnection.LastAccess > cutOff
&& isGoodConnection(recentConnection.TcpClient))
{
return recentConnection;
}
disposalBag.Add(recentConnection);
}
}
} }
} }
finally
{
@lock.Release();
}
} }
var connection = await createClient(remoteHostName, remotePort, httpVersion, isHttps, var connection = await createClient(remoteHostName, remotePort, httpVersion, isHttps,
...@@ -166,7 +178,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -166,7 +178,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
break; break;
} }
} }
} }
finally finally
{ {
...@@ -181,18 +193,22 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -181,18 +193,22 @@ namespace Titanium.Web.Proxy.Network.Tcp
foreach (var item in cache) foreach (var item in cache)
{ {
var queue = item.Value; var queue = item.Value;
while (queue.TryDequeue(out var connection))
while (queue.Count > 0)
{ {
var cutOff = DateTime.Now.AddSeconds(-1 * server.ConnectionTimeOutSeconds); if (queue.TryDequeue(out var connection))
if (!server.EnableConnectionPool
|| connection.LastAccess < cutOff)
{ {
disposalBag.Add(connection); var cutOff = DateTime.Now.AddSeconds(-1 * server.ConnectionTimeOutSeconds);
continue; if (!server.EnableConnectionPool
} || connection.LastAccess < cutOff)
{
disposalBag.Add(connection);
continue;
}
queue.Enqueue(connection); queue.Enqueue(connection);
break; break;
}
} }
} }
...@@ -220,8 +236,8 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -220,8 +236,8 @@ namespace Titanium.Web.Proxy.Network.Tcp
} }
} }
//cleanup every ten seconds by default //cleanup every 3 seconds by default
await Task.Delay(1000 * 10); await Task.Delay(1000 * 3);
} }
} }
......
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