Skip to content

Commit ddd59f4

Browse files
always reconnect with events
1 parent 2159793 commit ddd59f4

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

src/tarantool.client/LogicalConnectionManager.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ public class LogicalConnectionManager : ILogicalConnection
1717

1818
private LogicalConnection _droppableLogicalConnection;
1919

20-
private readonly ReaderWriterLockSlim _connectionLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
20+
private readonly ManualResetEvent _connected = new ManualResetEvent(false);
21+
22+
private readonly AutoResetEvent _reconnectAvailable = new AutoResetEvent(true);
23+
24+
private bool _onceConnected;
2125

2226
private Timer _timer;
2327

@@ -54,43 +58,45 @@ public void Dispose()
5458

5559
public async Task Connect()
5660
{
57-
if (!_connectionLock.TryEnterUpgradeableReadLock(_connectionTimeout))
61+
if (IsConnectedInternal())
62+
{
63+
return;
64+
}
65+
66+
if (!_reconnectAvailable.WaitOne(_connectionTimeout))
5867
{
5968
throw ExceptionHelper.NotConnected();
6069
}
6170

6271
try
6372
{
64-
if (this.IsConnected())
73+
if (IsConnectedInternal())
6574
{
6675
return;
6776
}
6877

78+
_connected.Reset();
79+
80+
_onceConnected = true;
81+
6982
_clientOptions.LogWriter?.WriteLine($"{nameof(LogicalConnectionManager)}: Connecting...");
7083

71-
_connectionLock.EnterWriteLock();
84+
var _newConnection = new LogicalConnection(_clientOptions, _requestIdCounter);
85+
await _newConnection.Connect();
86+
Interlocked.Exchange(ref _droppableLogicalConnection, _newConnection)?.Dispose();
7287

73-
try
74-
{
75-
var _newConnection = new LogicalConnection(_clientOptions, _requestIdCounter);
76-
await _newConnection.Connect();
77-
Interlocked.Exchange(ref _droppableLogicalConnection, _newConnection)?.Dispose();
88+
_connected.Set();
7889

79-
_clientOptions.LogWriter?.WriteLine($"{nameof(LogicalConnectionManager)}: Connected...");
90+
_clientOptions.LogWriter?.WriteLine($"{nameof(LogicalConnectionManager)}: Connected...");
8091

81-
if (_pingCheckInterval > 0 && _timer == null)
82-
{
83-
//_timer = new Timer(x => CheckPing(), null, _pingTimerInterval, Timeout.Infinite);
84-
}
85-
}
86-
finally
92+
if (_pingCheckInterval > 0 && _timer == null)
8793
{
88-
_connectionLock.ExitWriteLock();
94+
_timer = new Timer(x => CheckPing(), null, _pingTimerInterval, Timeout.Infinite);
8995
}
9096
}
9197
finally
9298
{
93-
_connectionLock.ExitUpgradeableReadLock();
99+
_reconnectAvailable.Set();
94100
}
95101
}
96102

@@ -118,19 +124,17 @@ private void CheckPing()
118124

119125
public bool IsConnected()
120126
{
121-
if (!_connectionLock.TryEnterReadLock(_connectionTimeout))
127+
if (!_onceConnected || !_connected.WaitOne(_connectionTimeout))
122128
{
123129
return false;
124130
}
125131

126-
try
127-
{
128-
return _droppableLogicalConnection?.IsConnected() ?? false;
129-
}
130-
finally
131-
{
132-
_connectionLock.ExitReadLock();
133-
}
132+
return IsConnectedInternal();
133+
}
134+
135+
private bool IsConnectedInternal()
136+
{
137+
return _droppableLogicalConnection?.IsConnected() ?? false;
134138
}
135139

136140
private void ScheduleNextPing()

0 commit comments

Comments
 (0)