Unverified Commit 8fa9ebd3 authored by honfika's avatar honfika Committed by GitHub

Merge pull request #677 from justcoding121/master

beta
parents a994cb31 0366b348
......@@ -348,7 +348,38 @@ retry:
tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
}
await tcpClient.ConnectAsync(ipAddress, port);
var connectTask = tcpClient.ConnectAsync(ipAddress, port);
await Task.WhenAny(connectTask, Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000));
if (!connectTask.IsCompleted || !tcpClient.Connected)
{
// here we can just do some cleanup and let the loop continue since
// we will either get a connection or wind up with a null tcpClient
// which will throw
try
{
connectTask.Dispose();
}
catch
{
// ignore
}
try
{
#if NET45
tcpClient?.Close();
#else
tcpClient?.Dispose();
#endif
tcpClient = null;
}
catch
{
// ignore
}
continue;
}
break;
}
catch (Exception e)
......
......@@ -196,6 +196,12 @@ namespace Titanium.Web.Proxy
/// </summary>
public int ConnectionTimeOutSeconds { get; set; } = 60;
/// <summary>
/// Seconds server connection are to wait for connection to be established.
/// Default value is 20 seconds.
/// </summary>
public int ConnectTimeOutSeconds { get; set; } = 20;
/// <summary>
/// Maximum number of concurrent connections per remote host in cache.
/// Only valid when connection pooling is enabled.
......
......@@ -257,6 +257,8 @@ namespace Titanium.Web.Proxy
TcpServerConnection? serverConnection, SslApplicationProtocol sslApplicationProtocol,
CancellationToken cancellationToken, CancellationTokenSource cancellationTokenSource)
{
args.HttpClient.Request.Locked = true;
// a connection generator task with captured parameters via closure.
Func<Task<TcpServerConnection>> generator = () =>
tcpConnectionFactory.GetServerConnection(this, args, isConnect: false,
......@@ -266,6 +268,9 @@ namespace Titanium.Web.Proxy
// for connection pool, retry fails until cache is exhausted.
return await retryPolicy<ServerConnectionException>().ExecuteAsync(async (connection) =>
{
// set the connection and send request headers
args.HttpClient.SetConnection(connection);
args.TimeLine["Connection Ready"] = DateTime.Now;
if (args.HttpClient.Request.UpgradeToWebSocket)
......@@ -290,12 +295,9 @@ namespace Titanium.Web.Proxy
{
var cancellationToken = args.CancellationTokenSource.Token;
var request = args.HttpClient.Request;
request.Locked = true;
var body = request.CompressBodyAndUpdateContentLength();
// set the connection and send request headers
args.HttpClient.SetConnection(connection);
await args.HttpClient.SendRequest(Enable100ContinueBehaviour, args.IsTransparent,
cancellationToken);
......
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