Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 2d10f59

Browse files
committed
add support for ConnectTimeOutSeconds
if establishing a connection takes longer than ConnectTimeOutSeconds it will try the next address of fail the connect instead of waiting for the default timeout which is usually 20-30 seconds. Most users will want to change this to something reasonable like 6 - 10 seconds.
1 parent 6672cb6 commit 2d10f59

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,38 @@ private async Task<TcpServerConnection> createServerConnection(string remoteHost
348348
tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
349349
}
350350

351-
await tcpClient.ConnectAsync(ipAddress, port);
351+
var connectTask = tcpClient.ConnectAsync(ipAddress, port);
352+
await Task.WhenAny(connectTask, Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000));
353+
if (!connectTask.IsCompleted || !tcpClient.Connected)
354+
{
355+
// here we can just do some cleanup and let the loop continue since
356+
// we will either get a connection or wind up with a null tcpClient
357+
// which will throw
358+
try
359+
{
360+
connectTask.Dispose();
361+
362+
}
363+
catch
364+
{
365+
// ignore
366+
}
367+
try
368+
{
369+
#if NET45
370+
tcpClient?.Close();
371+
#else
372+
tcpClient?.Dispose();
373+
#endif
374+
tcpClient = null;
375+
}
376+
catch
377+
{
378+
// ignore
379+
}
380+
381+
continue;
382+
}
352383
break;
353384
}
354385
catch (Exception e)

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam
196196
/// </summary>
197197
public int ConnectionTimeOutSeconds { get; set; } = 60;
198198

199+
/// <summary>
200+
/// Seconds server connection are to wait for connection to be established.
201+
/// Default value is 20 seconds.
202+
/// </summary>
203+
public int ConnectTimeOutSeconds { get; set; } = 20;
204+
199205
/// <summary>
200206
/// Maximum number of concurrent connections per remote host in cache.
201207
/// Only valid when connection pooling is enabled.

0 commit comments

Comments
 (0)