Commit b3499602 authored by justcoding121's avatar justcoding121

Update logic to reflect time out change in factory

parent 36249467
......@@ -33,9 +33,12 @@ namespace Titanium.Web.Proxy.Network.Tcp
//cache object race operations lock
private readonly SemaphoreSlim @lock = new SemaphoreSlim(1);
internal TcpConnectionFactory(int connectionTimeOutSeconds)
internal ProxyServer server { get; set; }
internal TcpConnectionFactory(ProxyServer server)
{
Task.Run(async () => await ClearOutdatedConnections(connectionTimeOutSeconds));
this.server = server;
Task.Run(async () => await ClearOutdatedConnections());
}
/// <summary>
......@@ -58,8 +61,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
CancellationToken cancellationToken)
{
string cacheKey = null;
if (proxyServer.EnableConnectionPool)
{
var cacheKeyBuilder = new StringBuilder($"{remoteHostName}-{remotePort}" +
$"-{(httpVersion == null ? string.Empty : httpVersion.ToString())}" +
$"-{isHttps}-{isConnect}-");
......@@ -78,6 +80,8 @@ namespace Titanium.Web.Proxy.Network.Tcp
cacheKey = cacheKeyBuilder.ToString();
if (proxyServer.EnableConnectionPool)
{
if (cache.TryGetValue(cacheKey, out var existingConnections))
{
while (existingConnections.TryDequeue(out var recentConnection))
......@@ -278,7 +282,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
@lock.Release();
}
private async Task ClearOutdatedConnections(int connectionTimeOutSeconds)
private async Task ClearOutdatedConnections()
{
while (true)
......@@ -288,7 +292,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
var queue = item.Value;
while (queue.TryDequeue(out var connection))
{
var cutOff = DateTime.Now.AddSeconds(-1 * connectionTimeOutSeconds);
var cutOff = DateTime.Now.AddSeconds(-1 * server.ConnectionTimeOutSeconds);
if (connection.LastAccess < cutOff)
{
disposalBag.Add(connection);
......
......@@ -98,7 +98,7 @@ namespace Titanium.Web.Proxy
ConnectionTimeOutSeconds = 60;
ProxyEndPoints = new List<ProxyEndPoint>();
tcpConnectionFactory = new TcpConnectionFactory(ConnectionTimeOutSeconds);
tcpConnectionFactory = new TcpConnectionFactory(this);
if (!RunTime.IsRunningOnMono && RunTime.IsWindows)
{
systemProxySettingsManager = new SystemProxyManager();
......
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