Skip to content

Commit 2df6fa1

Browse files
ping tarantool instead of socket ping, tbd
1 parent 5c398f7 commit 2df6fa1

File tree

5 files changed

+98
-7
lines changed

5 files changed

+98
-7
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
using ProGaudi.MsgPack.Light;
4+
5+
using ProGaudi.Tarantool.Client.Model.Requests;
6+
7+
namespace ProGaudi.Tarantool.Client.Converters
8+
{
9+
internal class PingPacketConverter : IMsgPackConverter<PingRequest>
10+
{
11+
public void Initialize(MsgPackContext context)
12+
{
13+
}
14+
15+
public void Write(PingRequest value, IMsgPackWriter writer)
16+
{
17+
}
18+
19+
public PingRequest Read(IMsgPackReader reader)
20+
{
21+
throw new NotImplementedException();
22+
}
23+
}
24+
}

src/tarantool.client/LogicalConnectionManager.cs

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
5+
using ProGaudi.Tarantool.Client.Model;
26
using ProGaudi.Tarantool.Client.Model.Requests;
37
using ProGaudi.Tarantool.Client.Model.Responses;
8+
using ProGaudi.Tarantool.Client.Utils;
49

510
namespace ProGaudi.Tarantool.Client
611
{
7-
using System.Threading;
8-
9-
using ProGaudi.Tarantool.Client.Model;
10-
using ProGaudi.Tarantool.Client.Utils;
11-
1212
public class LogicalConnectionManager : ILogicalConnection
1313
{
1414
private readonly ClientOptions _clientOptions;
@@ -21,16 +21,38 @@ public class LogicalConnectionManager : ILogicalConnection
2121

2222
private readonly AutoResetEvent _reconnectAvailable = new AutoResetEvent(true);
2323

24+
private Timer _timer;
25+
26+
private int _disposing;
27+
2428
private const int connectionTimeout = 1000;
2529

30+
private const int connectionPingInterval = 1000;
31+
2632
public LogicalConnectionManager(ClientOptions options)
2733
{
2834
_clientOptions = options;
2935
}
3036

3137
public void Dispose()
3238
{
39+
if (Interlocked.Exchange(ref _disposing, 1) > 0)
40+
{
41+
return;
42+
}
43+
3344
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+
}
3456
}
3557

3658
public async Task Connect()
@@ -46,6 +68,40 @@ public async Task Connect()
4668
_connected.Set();
4769

4870
_clientOptions.LogWriter?.WriteLine($"{nameof(LogicalConnectionManager)}: Connected...");
71+
72+
_timer = new Timer(x => CheckPing(), null, connectionPingInterval, Timeout.Infinite);
73+
}
74+
75+
private static readonly PingRequest pingRequest = new PingRequest();
76+
77+
private void CheckPing()
78+
{
79+
LogicalConnection savedConnection = _droppableLogicalConnection;
80+
81+
if (savedConnection == null)
82+
{
83+
return;
84+
}
85+
86+
try
87+
{
88+
Task task = savedConnection.SendRequestWithEmptyResponse(pingRequest);
89+
if (!task.Wait(connectionTimeout) || task.Status != TaskStatus.RanToCompletion)
90+
{
91+
savedConnection.Dispose();
92+
}
93+
}
94+
catch (AggregateException ae)
95+
{
96+
savedConnection.Dispose();
97+
}
98+
finally
99+
{
100+
if (_disposing == 0)
101+
{
102+
_timer?.Change(connectionPingInterval, Timeout.Infinite);
103+
}
104+
}
49105
}
50106

51107
public bool IsConnected()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using ProGaudi.Tarantool.Client.Model.Enums;
2+
3+
namespace ProGaudi.Tarantool.Client.Model.Requests
4+
{
5+
public class PingRequest : IRequest
6+
{
7+
public CommandCode Code => CommandCode.Ping;
8+
}
9+
}

src/tarantool.client/NetworkStreamPhysicalConnection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ 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-
SetKeepAlive(true, 1000, 100);
45+
// unfortunately does not worl under linux
46+
// SetKeepAlive(true, 1000, 100);
4647
_stream = new NetworkStream(_socket, true);
4748
options.LogWriter?.WriteLine("Socket connection established.");
4849
}

src/tarantool.client/TarantoolConvertersRegistrator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static void Register(MsgPackContext context)
4545
context.RegisterGenericConverter(typeof(InsertReplacePacketConverter<>));
4646
context.RegisterGenericConverter(typeof(SelectPacketConverter<>));
4747
context.RegisterGenericConverter(typeof(UpsertPacketConverter<>));
48+
context.RegisterConverter(new PingPacketConverter());
4849

4950
context.RegisterGenericConverter(typeof(TupleConverter<>));
5051
context.RegisterGenericConverter(typeof(TupleConverter<,>));

0 commit comments

Comments
 (0)