Commit dc486d7d authored by justcoding121's avatar justcoding121

refactor

parent ff27020e
...@@ -150,8 +150,11 @@ namespace Titanium.Web.Proxy ...@@ -150,8 +150,11 @@ namespace Titanium.Web.Proxy
} }
SslStream sslStream = null; SslStream sslStream = null;
//don't pass cancellation token here
//it could cause floating server connections when client exits
prefetchConnectionTask = getServerConnection(connectArgs, true, prefetchConnectionTask = getServerConnection(connectArgs, true,
null, false, cancellationToken); null, false, CancellationToken.None);
try try
{ {
sslStream = new SslStream(clientStream); sslStream = new SslStream(clientStream);
...@@ -333,17 +336,17 @@ namespace Titanium.Web.Proxy ...@@ -333,17 +336,17 @@ namespace Titanium.Web.Proxy
{ {
clientStream.Dispose(); clientStream.Dispose();
if (!cancellationTokenSource.IsCancellationRequested)
{
cancellationTokenSource.Cancel();
}
if (!calledRequestHandler if (!calledRequestHandler
&& prefetchConnectionTask != null) && prefetchConnectionTask != null)
{ {
var connection = await prefetchConnectionTask; var connection = await prefetchConnectionTask;
await tcpConnectionFactory.Release(connection, closeServerConnection); await tcpConnectionFactory.Release(connection, closeServerConnection);
} }
if (!cancellationTokenSource.IsCancellationRequested)
{
cancellationTokenSource.Cancel();
}
} }
} }
} }
......
...@@ -815,12 +815,12 @@ namespace Titanium.Web.Proxy ...@@ -815,12 +815,12 @@ namespace Titanium.Web.Proxy
.RetryAsync(retries, .RetryAsync(retries,
onRetryAsync: async (ex, i, context) => onRetryAsync: async (ex, i, context) =>
{ {
if (context.ContainsKey("connection")) if (context["connection"] != null)
{ {
//close connection on error //close connection on error
var connection = (TcpServerConnection)context["connection"]; var connection = (TcpServerConnection)context["connection"];
await tcpConnectionFactory.Release(connection, true); await tcpConnectionFactory.Release(connection, true);
context.Remove("connection"); context["connection"] = null;
} }
}); });
......
...@@ -204,21 +204,13 @@ namespace Titanium.Web.Proxy ...@@ -204,21 +204,13 @@ namespace Titanium.Web.Proxy
connection = null; connection = null;
} }
var contextData = new Dictionary<string, object>();
if (connection != null)
{
contextData.Add("connection", connection);
}
//for connection pool retry fails until cache is exhausted //for connection pool retry fails until cache is exhausted
await retryPolicy<ServerConnectionException>().ExecuteAsync(async (context) => await retryPolicy<ServerConnectionException>().ExecuteAsync(async (context) =>
{ {
connection = context["connection"] as TcpServerConnection ??
connection = context.ContainsKey("connection")? await getServerConnection(args, false,
(TcpServerConnection)context["connection"] clientConnection.NegotiatedApplicationProtocol,
: await getServerConnection(args, false, false, cancellationToken);
clientConnection.NegotiatedApplicationProtocol,
false, cancellationToken);
context["connection"] = connection; context["connection"] = connection;
...@@ -235,8 +227,7 @@ namespace Titanium.Web.Proxy ...@@ -235,8 +227,7 @@ namespace Titanium.Web.Proxy
// construct the web request that we are going to issue on behalf of the client. // construct the web request that we are going to issue on behalf of the client.
await handleHttpSessionRequestInternal(connection, args); await handleHttpSessionRequestInternal(connection, args);
}, contextData); }, new Dictionary<string, object> { { "connection", connection } });
//user requested //user requested
if (args.WebSession.CloseServerConnection) if (args.WebSession.CloseServerConnection)
......
...@@ -63,9 +63,11 @@ namespace Titanium.Web.Proxy ...@@ -63,9 +63,11 @@ namespace Titanium.Web.Proxy
if (endPoint.DecryptSsl && args.DecryptSsl) if (endPoint.DecryptSsl && args.DecryptSsl)
{ {
//don't pass cancellation token here
//it could cause floating server connections when client exits
prefetchConnectionTask = tcpConnectionFactory.GetClient(httpsHostName, endPoint.Port, prefetchConnectionTask = tcpConnectionFactory.GetClient(httpsHostName, endPoint.Port,
null, true, null, false, this, null, true, null, false, this,
UpStreamEndPoint, UpStreamHttpsProxy, false, cancellationToken); UpStreamEndPoint, UpStreamHttpsProxy, false, CancellationToken.None);
SslStream sslStream = null; SslStream sslStream = null;
......
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