Skip to content

Commit 53d590b

Browse files
committed
Implement Connect and ConnectAsync in PooledSocket
1 parent 8ddcef8 commit 53d590b

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed

Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ protected internal virtual PooledSocket CreateSocket()
702702
try
703703
{
704704
var ps = new PooledSocket(this.endPoint, this.config.ConnectionTimeout, this.config.ReceiveTimeout, _logger);
705-
//ps.Connect();
705+
ps.Connect();
706706
return ps;
707707
}
708708
catch (Exception ex)
@@ -717,7 +717,7 @@ protected internal virtual async Task<PooledSocket> CreateSocketAsync()
717717
try
718718
{
719719
var ps = new PooledSocket(this.endPoint, this.config.ConnectionTimeout, this.config.ReceiveTimeout, _logger);
720-
//await ps.ConnectAsync();
720+
await ps.ConnectAsync();
721721
return ps;
722722
}
723723
catch (Exception ex)

Enyim.Caching/Memcached/PooledSocket.cs

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ public partial class PooledSocket : IDisposable
1919

2020
private bool _isAlive;
2121
private Socket _socket;
22-
private EndPoint _endpoint;
22+
private readonly EndPoint _endpoint;
23+
private readonly int _connectionTimeout;
2324

2425
private Stream _inputStream;
2526
private AsyncSocketHelper _helper;
2627

2728
public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan receiveTimeout, ILogger logger)
2829
{
2930
_logger = logger;
30-
3131
_isAlive = true;
3232

3333
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
34+
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
3435
socket.NoDelay = true;
3536

36-
var timeout = connectionTimeout == TimeSpan.MaxValue
37+
_connectionTimeout = connectionTimeout == TimeSpan.MaxValue
3738
? Timeout.Infinite
3839
: (int)connectionTimeout.TotalMilliseconds;
3940

@@ -44,45 +45,73 @@ public PooledSocket(EndPoint endpoint, TimeSpan connectionTimeout, TimeSpan rece
4445
socket.ReceiveTimeout = rcv;
4546
socket.SendTimeout = rcv;
4647

47-
if (!ConnectWithTimeout(socket, endpoint, timeout))
48-
{
49-
throw new TimeoutException($"Could not connect to {endpoint}.");
50-
}
51-
5248
_socket = socket;
5349
_endpoint = endpoint;
54-
55-
_inputStream = new NetworkStream(socket);
5650
}
5751

58-
private bool ConnectWithTimeout(Socket socket, EndPoint endpoint, int timeout)
52+
public void Connect()
5953
{
60-
bool connected = false;
61-
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
54+
bool success = false;
6255

6356
//Learn from https://github.com/dotnet/corefx/blob/release/2.2/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNITcpHandle.cs#L180
6457
var cts = new CancellationTokenSource();
65-
cts.CancelAfter(timeout);
58+
cts.CancelAfter(_connectionTimeout);
6659
void Cancel()
6760
{
68-
if (!socket.Connected)
61+
if (!_socket.Connected)
6962
{
70-
socket.Dispose();
63+
_socket.Dispose();
7164
}
7265
}
7366
cts.Token.Register(Cancel);
7467

75-
socket.Connect(endpoint);
76-
if (socket.Connected)
68+
_socket.Connect(_endpoint);
69+
if (_socket.Connected)
70+
{
71+
success = true;
72+
}
73+
else
74+
{
75+
_socket.Dispose();
76+
}
77+
78+
if (success)
79+
{
80+
_inputStream = new NetworkStream(_socket);
81+
}
82+
else
83+
{
84+
throw new TimeoutException($"Could not connect to {_endpoint}.");
85+
}
86+
}
87+
88+
public async Task ConnectAsync()
89+
{
90+
bool success = false;
91+
92+
var connTask = _socket.ConnectAsync(_endpoint);
93+
if (await Task.WhenAny(connTask, Task.Delay(_connectionTimeout)) == connTask)
7794
{
78-
connected = true;
95+
await connTask;
96+
}
97+
98+
if (_socket.Connected)
99+
{
100+
success = true;
79101
}
80102
else
81103
{
82-
socket.Dispose();
104+
_socket.Dispose();
83105
}
84106

85-
return connected;
107+
if (success)
108+
{
109+
_inputStream = new NetworkStream(_socket);
110+
}
111+
else
112+
{
113+
throw new TimeoutException($"Could not connect to {_endpoint}.");
114+
}
86115
}
87116

88117
public Action<PooledSocket> CleanupCallback { get; set; }
@@ -159,16 +188,14 @@ protected void Dispose(bool disposing)
159188
try
160189
{
161190
if (_socket != null)
162-
try
163-
{
164-
_socket.Dispose();
165-
}
166-
catch
167-
{
168-
}
191+
{
192+
try { _socket.Dispose(); } catch { }
193+
}
169194

170195
if (_inputStream != null)
196+
{
171197
_inputStream.Dispose();
198+
}
172199

173200
_inputStream = null;
174201
_socket = null;
@@ -325,7 +352,7 @@ public void Write(IList<ArraySegment<byte>> buffers)
325352
if (_socket.Send(buffers, SocketFlags.None, out status) != total)
326353
System.Diagnostics.Debugger.Break();
327354
#else
328-
this.socket.Send(buffers, SocketFlags.None, out status);
355+
_socket.Send(buffers, SocketFlags.None, out status);
329356
#endif
330357

331358
if (status != SocketError.Success)

0 commit comments

Comments
 (0)