Skip to content

Commit 1e10433

Browse files
ping tarantool, done
1 parent 20cac5c commit 1e10433

File tree

2 files changed

+18
-47
lines changed

2 files changed

+18
-47
lines changed

src/tarantool.client/LogicalConnectionManager.cs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class LogicalConnectionManager : ILogicalConnection
2727

2828
private const int connectionTimeout = 1000;
2929

30-
private const int connectionPingInterval = 1000;
30+
private const int pingInterval = 100;
3131

3232
public LogicalConnectionManager(ClientOptions options)
3333
{
@@ -42,17 +42,7 @@ public void Dispose()
4242
}
4343

4444
Interlocked.Exchange(ref _droppableLogicalConnection, null)?.Dispose();
45-
46-
var savedTimer = Interlocked.Exchange(ref _timer, null);
47-
if (savedTimer != null)
48-
{
49-
using (var timerDisposedEvent = new ManualResetEvent(false))
50-
{
51-
savedTimer.Dispose(timerDisposedEvent);
52-
// this will guarantee that all callbacks finished
53-
timerDisposedEvent.WaitOne();
54-
}
55-
}
45+
Interlocked.Exchange(ref _timer, null)?.Dispose();
5646
}
5747

5848
public async Task Connect()
@@ -69,37 +59,36 @@ public async Task Connect()
6959

7060
_clientOptions.LogWriter?.WriteLine($"{nameof(LogicalConnectionManager)}: Connected...");
7161

72-
_timer = new Timer(x => CheckPing(), null, connectionPingInterval, Timeout.Infinite);
62+
_timer = new Timer(x => CheckPing(), null, pingInterval, Timeout.Infinite);
7363
}
7464

75-
private static readonly PingRequest pingRequest = new PingRequest();
65+
private static readonly PingRequest _pingRequest = new PingRequest();
7666

7767
private void CheckPing()
7868
{
7969
LogicalConnection savedConnection = _droppableLogicalConnection;
8070

81-
if (savedConnection == null)
82-
{
83-
return;
84-
}
85-
8671
try
8772
{
88-
Task task = savedConnection.SendRequestWithEmptyResponse(pingRequest);
89-
if (!task.Wait(connectionTimeout) || task.Status != TaskStatus.RanToCompletion)
73+
if (savedConnection == null || !savedConnection.IsConnected())
9074
{
91-
savedConnection.Dispose();
75+
return;
76+
}
77+
78+
using (Task task = savedConnection.SendRequestWithEmptyResponse(_pingRequest))
79+
{
80+
if (Task.WaitAny(new[] { task }, connectionTimeout) != 0 || task.Status != TaskStatus.RanToCompletion)
81+
{
82+
_clientOptions.LogWriter?.WriteLine($"{nameof(LogicalConnectionManager)}: Ping failed, dropping logical conection...");
83+
savedConnection.Dispose();
84+
}
9285
}
93-
}
94-
catch (AggregateException ae)
95-
{
96-
savedConnection.Dispose();
9786
}
9887
finally
9988
{
10089
if (_disposing == 0)
10190
{
102-
_timer?.Change(connectionPingInterval, Timeout.Infinite);
91+
_timer?.Change(pingInterval, Timeout.Infinite);
10392
}
10493
}
10594
}

src/tarantool.client/NetworkStreamPhysicalConnection.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,11 @@ public async Task Connect(ClientOptions options)
4242

4343
_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
4444
await ConnectAsync(_socket, singleNode.Uri.Host, singleNode.Uri.Port);
45-
// unfortunately does not worl under linux
46-
// SetKeepAlive(true, 1000, 100);
45+
4746
_stream = new NetworkStream(_socket, true);
4847
options.LogWriter?.WriteLine("Socket connection established.");
4948
}
5049

51-
private void SetKeepAlive(bool on, uint keepAliveTime, uint keepAliveInterval)
52-
{
53-
int size = Marshal.SizeOf(new uint());
54-
55-
var inOptionValues = new byte[size * 3];
56-
57-
BitConverter.GetBytes((uint)(on ? 1 : 0)).CopyTo(inOptionValues, 0);
58-
BitConverter.GetBytes(keepAliveTime).CopyTo(inOptionValues, size);
59-
BitConverter.GetBytes(keepAliveInterval).CopyTo(inOptionValues, size * 2);
60-
61-
_socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
62-
}
63-
6450
public void Write(byte[] buffer, int offset, int count)
6551
{
6652
CheckConnectionStatus();
@@ -125,11 +111,7 @@ public bool IsConnected()
125111
return false;
126112
}
127113

128-
try
129-
{
130-
return !(_socket.Poll(1, SelectMode.SelectRead) && _socket.Available == 0);
131-
}
132-
catch (SocketException) { return false; }
114+
return true;
133115
}
134116

135117
private void CheckConnectionStatus()

0 commit comments

Comments
 (0)