Beispiel #1
0
        private void Worker()
        {
            bool ShouldDoWork() => !_isDisposed && IsConnected && _cancellation?.IsCancellationRequested == false;

            var lastOpReceivedAt = DateTime.UtcNow;
            var ping             = false;

            while (ShouldDoWork())
            {
                try
                {
                    if (ping)
                    {
                        ping = false;
                        _logger.Debug("Pinging due to silent server.");
                        Ping();
                    }

                    foreach (var op in _connection.ReadOp())
                    {
                        lastOpReceivedAt = DateTime.UtcNow;

                        _opMediator.Emit(op);

                        switch (op)
                        {
                        case PingOp _ when _connectionInfo.AutoRespondToPing && ShouldDoWork():
                            Pong();
                            break;

                        case ErrOp errOp:
                            throw NatsException.ClientReceivedErrOp(errOp);
                        }
                    }
                }
                catch (NatsException nex) when(nex.ExceptionCode == NatsExceptionCodes.OpParserError)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (!ShouldDoWork())
                    {
                        break;
                    }

                    _logger.Error("Worker got Exception.", ex);

                    if (ex.InnerException is SocketException socketEx)
                    {
                        // ReSharper disable once HeapView.BoxingAllocation
                        _logger.Error($"Worker task got SocketException with SocketErrorCode='{socketEx.SocketErrorCode}'");

                        if (socketEx.SocketErrorCode == SocketError.Interrupted)
                        {
                            break;
                        }

                        if (socketEx.SocketErrorCode != SocketError.TimedOut)
                        {
                            throw;
                        }
                    }

                    var silenceDeltaMs = DateTime.UtcNow.Subtract(lastOpReceivedAt).TotalMilliseconds;
                    if (silenceDeltaMs >= ConsumerMaxMsSilenceFromServer)
                    {
                        throw NatsException.ConnectionFoundIdling(_connection.ServerInfo.Host, _connection.ServerInfo.Port);
                    }

                    if (silenceDeltaMs >= ConsumerPingAfterMsSilenceFromServer)
                    {
                        ping = true;
                    }
                }
            }
        }
Beispiel #2
0
        private void Worker()
        {
            bool ShouldDoWork() => !_isDisposed && IsConnected && _cancellation?.IsCancellationRequested == false;

            while (ShouldDoWork())
            {
                var lastOpReceivedAt = DateTime.UtcNow;

                try
                {
                    foreach (var op in _connection.ReadOp())
                    {
                        if (!ShouldDoWork())
                        {
                            break;
                        }

                        lastOpReceivedAt = DateTime.UtcNow;

                        _opMediator.Emit(op);

                        if (op is PingOp && _connectionInfo.AutoRespondToPing)
                        {
                            Pong();
                        }

                        if (op is ErrOp errOp)
                        {
                            throw NatsException.ClientReceivedErrOp(errOp);
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (!ShouldDoWork())
                    {
                        break;
                    }

                    _logger.Error("Worker got Exception.", ex);

                    if (ex.InnerException is SocketException socketEx)
                    {
                        _logger.Error($"Worker task got SocketException with SocketErrorCode='{socketEx.SocketErrorCode}'");

                        if (socketEx.SocketErrorCode == SocketError.Interrupted)
                        {
                            break;
                        }

                        if (socketEx.SocketErrorCode != SocketError.TimedOut)
                        {
                            throw;
                        }
                    }

                    var silenceDeltaMs = DateTime.UtcNow.Subtract(lastOpReceivedAt).TotalMilliseconds;
                    if (silenceDeltaMs >= ConsumerMaxMsSilenceFromServer)
                    {
                        throw NatsException.ConnectionFoundIdling(_connection.ServerInfo.Host, _connection.ServerInfo.Port);
                    }

                    if (silenceDeltaMs >= ConsumerPingAfterMsSilenceFromServer)
                    {
                        Ping();
                    }
                }
            }
        }