Commit 4633f88f authored by titanium007's avatar titanium007

close connection each request

parent dfc356cd
...@@ -20,7 +20,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -20,7 +20,7 @@ namespace Titanium.Web.Proxy.Http
internal class TcpConnectionManager internal class TcpConnectionManager
{ {
static ConcurrentDictionary<string, ConcurrentStack<TcpConnection>> ConnectionCache = new ConcurrentDictionary<string, ConcurrentStack<TcpConnection>>(); static Dictionary<string, Stack<TcpConnection>> ConnectionCache = new Dictionary<string, Stack<TcpConnection>>();
public static TcpConnection GetClient(string Hostname, int port, bool IsSecure) public static TcpConnection GetClient(string Hostname, int port, bool IsSecure)
{ {
...@@ -28,17 +28,18 @@ namespace Titanium.Web.Proxy.Http ...@@ -28,17 +28,18 @@ namespace Titanium.Web.Proxy.Http
TcpConnection client; TcpConnection client;
lock (ConnectionCache) lock (ConnectionCache)
{ {
ConcurrentStack<TcpConnection> connections; Stack<TcpConnection> connections;
if (!ConnectionCache.TryGetValue(key, out connections)) if (!ConnectionCache.TryGetValue(key, out connections))
{ {
return CreateClient(Hostname, port, IsSecure); return CreateClient(Hostname, port, IsSecure);
} }
if (connections.Count > 0)
if (!connections.TryPop(out client))
{ {
return CreateClient(Hostname, port, IsSecure); client = connections.Pop();
} }
else
return CreateClient(Hostname, port, IsSecure);
} }
return client; return client;
} }
...@@ -73,13 +74,13 @@ namespace Titanium.Web.Proxy.Http ...@@ -73,13 +74,13 @@ namespace Titanium.Web.Proxy.Http
var key = string.Concat(Hostname, ":", port, ":", IsSecure); var key = string.Concat(Hostname, ":", port, ":", IsSecure);
lock (ConnectionCache) lock (ConnectionCache)
{ {
ConcurrentStack<TcpConnection> connections; Stack<TcpConnection> connections;
if (!ConnectionCache.TryGetValue(key, out connections)) if (!ConnectionCache.TryGetValue(key, out connections))
{ {
connections = new ConcurrentStack<TcpConnection>(); connections = new Stack<TcpConnection>();
connections.Push(Client); connections.Push(Client);
ConnectionCache.TryAdd(key, connections); ConnectionCache.Add(key, connections);
} }
connections.Push(Client); connections.Push(Client);
......
...@@ -247,9 +247,10 @@ namespace Titanium.Web.Proxy ...@@ -247,9 +247,10 @@ namespace Titanium.Web.Proxy
Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args); Dispose(client, clientStream, clientStreamReader, clientStreamWriter, args);
return; return;
} }
args.ProxySession.ProxyClient.Client.Close();
//if (args.ProxySession.ProxyClient.Client.Connected) // if (args.ProxySession.ProxyClient.Client.Connected)
// TcpConnectionManager.AddClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps, args.ProxySession.ProxyClient); // TcpConnectionManager.AddClient(args.ProxySession.Request.RequestUri.Host, args.ProxySession.Request.RequestUri.Port, args.IsHttps, args.ProxySession.ProxyClient);
// // read the next request // // read the next request
httpCmd = clientStreamReader.ReadLine(); httpCmd = clientStreamReader.ReadLine();
......
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