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 ...@@ -33,9 +33,12 @@ namespace Titanium.Web.Proxy.Network.Tcp
//cache object race operations lock //cache object race operations lock
private readonly SemaphoreSlim @lock = new SemaphoreSlim(1); 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> /// <summary>
...@@ -58,26 +61,27 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -58,26 +61,27 @@ namespace Titanium.Web.Proxy.Network.Tcp
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
string cacheKey = null; string cacheKey = null;
if (proxyServer.EnableConnectionPool)
var cacheKeyBuilder = new StringBuilder($"{remoteHostName}-{remotePort}" +
$"-{(httpVersion == null ? string.Empty : httpVersion.ToString())}" +
$"-{isHttps}-{isConnect}-");
if (applicationProtocols != null)
{ {
var cacheKeyBuilder = new StringBuilder($"{remoteHostName}-{remotePort}" + foreach (var protocol in applicationProtocols)
$"-{(httpVersion == null ? string.Empty : httpVersion.ToString())}" +
$"-{isHttps}-{isConnect}-");
if (applicationProtocols != null)
{ {
foreach (var protocol in applicationProtocols) cacheKeyBuilder.Append($"{protocol}-");
{
cacheKeyBuilder.Append($"{protocol}-");
}
} }
}
cacheKeyBuilder.Append(upStreamEndPoint != null cacheKeyBuilder.Append(upStreamEndPoint != null
? $"{upStreamEndPoint.Address}-{upStreamEndPoint.Port}-" ? $"{upStreamEndPoint.Address}-{upStreamEndPoint.Port}-"
: string.Empty); : string.Empty);
cacheKeyBuilder.Append(externalProxy != null ? $"{externalProxy.GetCacheKey()}-" : string.Empty); cacheKeyBuilder.Append(externalProxy != null ? $"{externalProxy.GetCacheKey()}-" : string.Empty);
cacheKey = cacheKeyBuilder.ToString(); cacheKey = cacheKeyBuilder.ToString();
if (proxyServer.EnableConnectionPool)
{
if (cache.TryGetValue(cacheKey, out var existingConnections)) if (cache.TryGetValue(cacheKey, out var existingConnections))
{ {
while (existingConnections.TryDequeue(out var recentConnection)) while (existingConnections.TryDequeue(out var recentConnection))
...@@ -278,7 +282,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -278,7 +282,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
@lock.Release(); @lock.Release();
} }
private async Task ClearOutdatedConnections(int connectionTimeOutSeconds) private async Task ClearOutdatedConnections()
{ {
while (true) while (true)
...@@ -288,7 +292,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -288,7 +292,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
var queue = item.Value; var queue = item.Value;
while (queue.TryDequeue(out var connection)) 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) if (connection.LastAccess < cutOff)
{ {
disposalBag.Add(connection); disposalBag.Add(connection);
......
...@@ -98,7 +98,7 @@ namespace Titanium.Web.Proxy ...@@ -98,7 +98,7 @@ namespace Titanium.Web.Proxy
ConnectionTimeOutSeconds = 60; ConnectionTimeOutSeconds = 60;
ProxyEndPoints = new List<ProxyEndPoint>(); ProxyEndPoints = new List<ProxyEndPoint>();
tcpConnectionFactory = new TcpConnectionFactory(ConnectionTimeOutSeconds); tcpConnectionFactory = new TcpConnectionFactory(this);
if (!RunTime.IsRunningOnMono && RunTime.IsWindows) if (!RunTime.IsRunningOnMono && RunTime.IsWindows)
{ {
systemProxySettingsManager = new SystemProxyManager(); 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