Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh: refactor WebSocket connections to support concurrency #100

Open
wants to merge 12 commits into
base: 3.0
Choose a base branch
from
62 changes: 36 additions & 26 deletions src/Driver/Client/Websocket/WSClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class WSClient : ITDengineClient
private Connection _connection;
private readonly TimeZoneInfo _tz;
private readonly ConnectionStringBuilder _builder;
private readonly object _reconnectLock = new object();


public WSClient(ConnectionStringBuilder builder)
{
Expand Down Expand Up @@ -66,42 +68,50 @@ private void Reconnect()
{
if (!_builder.AutoReconnect)
return;

Connection connection = null;
for (int i = 0; i < _builder.ReconnectRetryCount; i++)
lock (_reconnectLock)
{
try
if (_connection != null)
{
// sleep
System.Threading.Thread.Sleep(_builder.ReconnectIntervalMs);
connection = new Connection(GetUrl(_builder), _builder.Username, _builder.Password,
_builder.Database, _builder.ConnTimeout, _builder.ReadTimeout, _builder.WriteTimeout,
_builder.EnableCompression);
connection.Connect();
break;
// connection is available, no need to reconnect
if (_connection.IsAvailable()) return;
}
catch (Exception)

Connection connection = null;
for (int i = 0; i < _builder.ReconnectRetryCount; i++)
{
if (connection != null)
try
{
// sleep
System.Threading.Thread.Sleep(_builder.ReconnectIntervalMs);
connection = new Connection(GetUrl(_builder), _builder.Username, _builder.Password,
_builder.Database, _builder.ConnTimeout, _builder.ReadTimeout, _builder.WriteTimeout,
_builder.EnableCompression);
connection.Connect();
break;
}
catch (Exception)
{
connection.Close();
connection = null;
if (connection != null)
{
connection.Close();
connection = null;
}
}
}
}

if (connection == null)
{
throw new TDengineError((int)TDengineError.InternalErrorCode.WS_RECONNECT_FAILED,
"websocket connection reconnect failed");
}
if (connection == null)
{
throw new TDengineError((int)TDengineError.InternalErrorCode.WS_RECONNECT_FAILED,
"websocket connection reconnect failed");
}

if (_connection != null)
{
_connection.Close();
}
if (_connection != null)
{
_connection.Close();
}

_connection = connection;
_connection = connection;
}
}

public IStmt StmtInit()
Expand Down
Loading