private async Task RetryConnect(Exception ex) { if (!_connectLock.Wait(0)) { // someone is already connecting return; } try { if (ex != null) { ConnectStats.AddException(ex); Interlocked.Increment(ref ConnectStats.ErrorCount); } var reconnectNow = DateTime.UtcNow; while (true) { try { // delay a random value < 3s await Task.Delay(new Random((int)Stopwatch.GetTimestamp()).Next(MinReconnectWaitMilliseconds, MaxReconnectWaitMilliseconds)); await ConnectCore(); RecoverStats.SetElapsed(reconnectNow); Interlocked.Increment(ref RecoverStats.SuccessCount); break; } catch (Exception e) { RecoverStats.AddException(e); Interlocked.Increment(ref RecoverStats.ErrorCount); Interlocked.Increment(ref ConnectStats.ErrorCount); } } } finally { _connectLock.Release(); } }
private async Task RetryConnect(Exception ex) { ConnectStats.AddException(ex); Interlocked.Increment(ref ConnectStats.ErrorCount); var reconnectNow = DateTime.Now; while (true) { try { await Task.Delay(new Random((int)Stopwatch.GetTimestamp()).Next(300, 20000)); if (DateTime.Now > _lastAuthTime.AddMinutes(50)) { // Reauth await Connect(); } else { // direct connect await ConnectCore(); } RecoverStats.SetElapsed(reconnectNow); Interlocked.Increment(ref RecoverStats.SuccessCount); break; } catch (Exception e) { RecoverStats.AddException(e); Interlocked.Increment(ref RecoverStats.ErrorCount); Interlocked.Increment(ref ConnectStats.NotSentCount); // another round after 300 } } }