Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0f44a7

Browse files
yurii-pelekhYurii Pelekhgalvesribeiro
authored
ISSUE#416 Fixed Socket.ConnectAsync timeout issue (#466)
Co-authored-by: Yurii Pelekh <[email protected]> Co-authored-by: Gutemberg Ribeiro <[email protected]>
1 parent 5bc8c3e commit c0f44a7

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Docker.DotNet/Microsoft.Net.Http.Client/ManagedHandler.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,19 @@ private static async Task<Socket> TCPSocketOpenerAsync(string host, int port, Ca
332332
var s = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
333333
try
334334
{
335-
await s.ConnectAsync(address, port).ConfigureAwait(false);
335+
#if (NETSTANDARD1_3 || NETSTANDARD1_6 || NETSTANDARD2_0)
336+
// TODO: This method can be replaced by native ConnectAsync when this functionality is available:
337+
// https://github.com/dotnet/runtime/pull/40750
338+
await s.ConnectAsync(address, port, cancellationToken).ConfigureAwait(false);
339+
#else
340+
await Task.Factory.FromAsync(
341+
s.BeginConnect,
342+
s.EndConnect,
343+
new IPEndPoint(address, port),
344+
null
345+
).ConfigureAwait(false);
346+
#endif
347+
336348
connectedSocket = s;
337349
break;
338350
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Net;
2+
using System.Net.Sockets;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.Net.Http.Client
7+
{
8+
public static class SocketExtensionMethods
9+
{
10+
public static async Task ConnectAsync(this Socket socket, IPAddress address, int port, CancellationToken cancellationToken)
11+
{
12+
Task socketConnectTask = socket.ConnectAsync(address, port);
13+
Task delayTask = Task.Delay(int.MaxValue, cancellationToken);
14+
15+
await await Task.WhenAny(socketConnectTask, delayTask);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)