Unverified Commit 45b13d34 authored by justcoding121's avatar justcoding121 Committed by GitHub

Merge pull request #443 from justcoding121/master

Cache improvements
parents e1f03c43 022b4762
...@@ -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,9 +94,15 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -94,9 +94,15 @@ namespace Titanium.Web.Proxy.Network.Tcp
if (proxyServer.EnableConnectionPool) if (proxyServer.EnableConnectionPool)
{ {
try
{
await @lock.WaitAsync();
if (cache.TryGetValue(cacheKey, out var existingConnections)) if (cache.TryGetValue(cacheKey, out var existingConnections))
{ {
while (existingConnections.TryDequeue(out var recentConnection)) while (existingConnections.Count > 0)
{
if (existingConnections.TryDequeue(out var recentConnection))
{ {
//+3 seconds for potential delay after getting connection //+3 seconds for potential delay after getting connection
var cutOff = DateTime.Now.AddSeconds(-1 * proxyServer.ConnectionTimeOutSeconds + 3); var cutOff = DateTime.Now.AddSeconds(-1 * proxyServer.ConnectionTimeOutSeconds + 3);
...@@ -111,6 +117,12 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -111,6 +117,12 @@ namespace Titanium.Web.Proxy.Network.Tcp
} }
} }
} }
}
finally
{
@lock.Release();
}
}
var connection = await createClient(remoteHostName, remotePort, httpVersion, isHttps, var connection = await createClient(remoteHostName, remotePort, httpVersion, isHttps,
applicationProtocols, isConnect, proxyServer, upStreamEndPoint, externalProxy, cancellationToken); applicationProtocols, isConnect, proxyServer, upStreamEndPoint, externalProxy, cancellationToken);
...@@ -181,7 +193,10 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -181,7 +193,10 @@ 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)
{
if (queue.TryDequeue(out var connection))
{ {
var cutOff = DateTime.Now.AddSeconds(-1 * server.ConnectionTimeOutSeconds); var cutOff = DateTime.Now.AddSeconds(-1 * server.ConnectionTimeOutSeconds);
if (!server.EnableConnectionPool if (!server.EnableConnectionPool
...@@ -195,6 +210,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -195,6 +210,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
break; break;
} }
} }
}
try try
{ {
...@@ -212,13 +228,16 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -212,13 +228,16 @@ namespace Titanium.Web.Proxy.Network.Tcp
@lock.Release(); @lock.Release();
} }
while (disposalBag.TryTake(out var connection)) while (!disposalBag.IsEmpty)
{
if (disposalBag.TryTake(out var connection))
{ {
connection?.Dispose(); connection?.Dispose();
} }
}
//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