public void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
                                       IBasicProperties properties, ReadOnlyMemory <byte> body)
        {
            byte[] bodyBytes = body.ToArray();

            Task.Run(async() =>
            {
                LogContext.Current = _context.LogContext;

                var context = new RabbitMqReceiveContext(exchange, routingKey, _consumerTag, deliveryTag, bodyBytes, redelivered, properties,
                                                         _context, _receiveSettings, _model, _model.ConnectionContext);

                if (!_pending.TryAdd(deliveryTag, context))
                {
                    LogContext.Warning?.Log("Duplicate BasicDeliver: {DeliveryTag}", deliveryTag);
                }

                var receiveLock = _receiveSettings.NoAck ? default : new RabbitMqReceiveLockContext(_model, deliveryTag);

                                  try
                                  {
                                      await _dispatcher.Dispatch(context, receiveLock).ConfigureAwait(false);
                                  }
                                  catch (Exception exception)
                                  {
                                      context.LogTransportFaulted(exception);
                                  }
                                  finally
                                  {
                                      _pending.TryRemove(deliveryTag, out _);
                                      context.Dispose();
                                  }
            });
        }
示例#2
0
        async void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange,
                                                     string routingKey,
                                                     IBasicProperties properties, byte[] body)
        {
            Interlocked.Increment(ref _deliveryCount);

            var current = Interlocked.Increment(ref _currentPendingDeliveryCount);

            while (current > _maxPendingDeliveryCount)
            {
                Interlocked.CompareExchange(ref _maxPendingDeliveryCount, current, _maxPendingDeliveryCount);
            }

            await Task.Yield();

            var context = new RabbitMqReceiveContext(_inputAddress, exchange, routingKey, _consumerTag, deliveryTag, body, redelivered, properties,
                                                     _receiveObserver);

            context.GetOrAddPayload(() => _receiveSettings);
            context.GetOrAddPayload(() => _model);

            try
            {
                if (!_pending.TryAdd(deliveryTag, context))
                {
                    if (_log.IsErrorEnabled)
                    {
                        _log.ErrorFormat("Duplicate BasicDeliver: {0}", deliveryTag);
                    }
                }

                await _receiveObserver.PreReceive(context).ConfigureAwait(false);

                await _receivePipe.Send(context).ConfigureAwait(false);

                await context.CompleteTask.ConfigureAwait(false);

                _model.BasicAck(deliveryTag, false);

                await _receiveObserver.PostReceive(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);

                _model.BasicNack(deliveryTag, false, true);
            }
            finally
            {
                Interlocked.Decrement(ref _currentPendingDeliveryCount);

                RabbitMqReceiveContext ignored;
                _pending.TryRemove(deliveryTag, out ignored);
            }
        }
示例#3
0
        public void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
                                       IBasicProperties properties, ReadOnlyMemory <byte> body)
        {
            var bodyBytes = body.ToArray();

            Task.Run(async() =>
            {
                LogContext.Current = _context.LogContext;

                var context = new RabbitMqReceiveContext(exchange, routingKey, _consumerTag, deliveryTag, bodyBytes, redelivered, properties,
                                                         _context, _receiveSettings, _model, _model.ConnectionContext);

                var added = _pending.TryAdd(deliveryTag, context);
                if (!added && deliveryTag != 1) // DIRECT REPLY-TO fixed value
                {
                    LogContext.Warning?.Log("Duplicate BasicDeliver: {DeliveryTag}", deliveryTag);
                }

                var receiveLock = _receiveSettings.NoAck ? default : new RabbitMqReceiveLockContext(_model, deliveryTag);

                                  if (_limit != null)
                                  {
                                      await _limit.WaitAsync(context.CancellationToken).ConfigureAwait(false);
                                  }

                                  try
                                  {
                                      await _dispatcher.Dispatch(context, receiveLock).ConfigureAwait(false);
                                  }
                                  catch (Exception exception)
                                  {
                                      context.LogTransportFaulted(exception);
                                  }
                                  finally
                                  {
                                      _limit?.Release();

                                      if (added)
                                      {
                                          _pending.TryRemove(deliveryTag, out _);
                                      }

                                      context.Dispose();
                                  }
            });
        }
        void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
                                               IBasicProperties properties, byte[] body)
        {
            Task.Run(async() =>
            {
                LogContext.Current = _context.LogContext;

                if (IsStopping && _receiveSettings.NoAck == false)
                {
                    await WaitAndAbandonMessage(deliveryTag).ConfigureAwait(false);
                    return;
                }

                var context = new RabbitMqReceiveContext(exchange, routingKey, _consumerTag, deliveryTag, body, redelivered, properties, _context,
                                                         _receiveSettings,
                                                         _model, _model.ConnectionContext);

                if (!_pending.TryAdd(deliveryTag, context))
                {
                    LogContext.Warning?.Log("Duplicate BasicDeliver: {DeliveryTag}", deliveryTag);
                }

                var receiveLock = _receiveSettings.NoAck ? default : new RabbitMqReceiveLockContext(_model, deliveryTag);

                                  try
                                  {
                                      await _dispatcher.Dispatch(context, receiveLock).ConfigureAwait(false);
                                  }
                                  catch (Exception exception)
                                  {
                                      context.LogTransportFaulted(exception);
                                  }
                                  finally
                                  {
                                      _pending.TryRemove(deliveryTag, out _);
                                      context.Dispose();
                                  }
            });
        }
示例#5
0
        async void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange,
                                                     string routingKey,
                                                     IBasicProperties properties, byte[] body)
        {
            if (_shuttingDown)
            {
                await WaitAndAbandonMessage(deliveryTag).ConfigureAwait(false);

                return;
            }

            Interlocked.Increment(ref _deliveryCount);

            int current = Interlocked.Increment(ref _currentPendingDeliveryCount);

            while (current > _maxPendingDeliveryCount)
            {
                Interlocked.CompareExchange(ref _maxPendingDeliveryCount, current, _maxPendingDeliveryCount);
            }

            var context = new RabbitMqReceiveContext(_inputAddress, exchange, routingKey, _consumerTag, deliveryTag, body, redelivered, properties,
                                                     _receiveObserver);

            context.GetOrAddPayload(() => _receiveSettings);
            context.GetOrAddPayload(() => _model);
            context.GetOrAddPayload(() => _model.ConnectionContext);

            try
            {
                if (!_pending.TryAdd(deliveryTag, context))
                {
                    if (_log.IsErrorEnabled)
                    {
                        _log.ErrorFormat("Duplicate BasicDeliver: {0}", deliveryTag);
                    }
                }

                await _receiveObserver.PreReceive(context).ConfigureAwait(false);

                await _receivePipe.Send(context).ConfigureAwait(false);

                await context.CompleteTask.ConfigureAwait(false);

                _model.BasicAck(deliveryTag, false);

                await _receiveObserver.PostReceive(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);

                try
                {
                    _model.BasicNack(deliveryTag, false, true);
                }
                catch (Exception ackEx)
                {
                    if (_log.IsErrorEnabled)
                    {
                        _log.ErrorFormat("An error occurred trying to NACK a message with delivery tag {0}: {1}", deliveryTag, ackEx.ToString());
                    }
                }
            }
            finally
            {
                RabbitMqReceiveContext ignored;
                _pending.TryRemove(deliveryTag, out ignored);

                int pendingCount = Interlocked.Decrement(ref _currentPendingDeliveryCount);
                if (pendingCount == 0 && _shuttingDown)
                {
                    if (_log.IsDebugEnabled)
                    {
                        _log.DebugFormat("Consumer shutdown completed: {0}", _inputAddress);
                    }

                    _deliveryComplete.TrySetResult(true);
                }
            }
        }
示例#6
0
        async void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange,
                                                     string routingKey,
                                                     IBasicProperties properties, byte[] body)
        {
            if (_shuttingDown)
            {
                await WaitAndAbandonMessage(deliveryTag).ConfigureAwait(false);

                return;
            }

            using (var delivery = _tracker.BeginDelivery())
            {
                var context = new RabbitMqReceiveContext(_inputAddress, exchange, routingKey, _consumerTag, deliveryTag, body, redelivered, properties,
                                                         _receiveObserver, _topology);

                context.GetOrAddPayload(() => _receiveSettings);
                context.GetOrAddPayload(() => _model);
                context.GetOrAddPayload(() => _model.ConnectionContext);

                try
                {
                    if (!_pending.TryAdd(deliveryTag, context))
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.ErrorFormat("Duplicate BasicDeliver: {0}", deliveryTag);
                        }
                    }

                    await _receiveObserver.PreReceive(context).ConfigureAwait(false);

                    await _receivePipe.Send(context).ConfigureAwait(false);

                    await context.CompleteTask.ConfigureAwait(false);

                    _model.BasicAck(deliveryTag, false);

                    await _receiveObserver.PostReceive(context).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);

                    try
                    {
                        _model.BasicNack(deliveryTag, false, true);
                    }
                    catch (Exception ackEx)
                    {
                        if (_log.IsErrorEnabled)
                        {
                            _log.ErrorFormat("An error occurred trying to NACK a message with delivery tag {0}: {1}", deliveryTag, ackEx.ToString());
                        }
                    }
                }
                finally
                {
                    RabbitMqReceiveContext ignored;
                    _pending.TryRemove(deliveryTag, out ignored);

                    context.Dispose();
                }
            }
        }
        async void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange,
            string routingKey,
            IBasicProperties properties, byte[] body)
        {
            Interlocked.Increment(ref _deliveryCount);

            int current = Interlocked.Increment(ref _currentPendingDeliveryCount);
            while (current > _maxPendingDeliveryCount)
                Interlocked.CompareExchange(ref _maxPendingDeliveryCount, current, _maxPendingDeliveryCount);

            await Task.Yield();

            var context = new RabbitMqReceiveContext(_inputAddress, exchange, routingKey, _consumerTag, deliveryTag, body, redelivered, properties,
                _receiveObserver);

            context.GetOrAddPayload(() => _receiveSettings);
            context.GetOrAddPayload(() => _model);

            try
            {
                if (!_pending.TryAdd(deliveryTag, context))
                {
                    if (_log.IsErrorEnabled)
                        _log.ErrorFormat("Duplicate BasicDeliver: {0}", deliveryTag);
                }

                await _receiveObserver.PreReceive(context);

                await _receivePipe.Send(context);

                await context.CompleteTask;

                _model.BasicAck(deliveryTag, false);

                await _receiveObserver.PostReceive(context);
            }
            catch (Exception ex)
            {
                await _receiveObserver.ReceiveFault(context, ex);

                _model.BasicNack(deliveryTag, false, true);
            }
            finally
            {
                Interlocked.Decrement(ref _currentPendingDeliveryCount);

                RabbitMqReceiveContext ignored;
                _pending.TryRemove(deliveryTag, out ignored);
            }
        }
        async void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange,
            string routingKey,
            IBasicProperties properties, byte[] body)
        {
            if (_shuttingDown)
            {
                await WaitAndAbandonMessage(deliveryTag).ConfigureAwait(false);
                return;
            }

            Interlocked.Increment(ref _deliveryCount);

            int current = Interlocked.Increment(ref _currentPendingDeliveryCount);
            while (current > _maxPendingDeliveryCount)
                Interlocked.CompareExchange(ref _maxPendingDeliveryCount, current, _maxPendingDeliveryCount);

            var context = new RabbitMqReceiveContext(_inputAddress, exchange, routingKey, _consumerTag, deliveryTag, body, redelivered, properties,
                _receiveObserver);

            context.GetOrAddPayload(() => _receiveSettings);
            context.GetOrAddPayload(() => _model);
            context.GetOrAddPayload(() => _model.ConnectionContext);

            try
            {
                if (!_pending.TryAdd(deliveryTag, context))
                {
                    if (_log.IsErrorEnabled)
                        _log.ErrorFormat("Duplicate BasicDeliver: {0}", deliveryTag);
                }

                await _receiveObserver.PreReceive(context).ConfigureAwait(false);

                await _receivePipe.Send(context).ConfigureAwait(false);

                await context.CompleteTask.ConfigureAwait(false);

                _model.BasicAck(deliveryTag, false);

                await _receiveObserver.PostReceive(context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);
                try
                {
                    _model.BasicNack(deliveryTag, false, true);
                }
                catch (Exception ackEx)
                {
                    if (_log.IsErrorEnabled)
                        _log.ErrorFormat("An error occurred trying to NACK a message with delivery tag {0}: {1}", deliveryTag, ackEx.ToString());
                }
            }
            finally
            {
                RabbitMqReceiveContext ignored;
                _pending.TryRemove(deliveryTag, out ignored);

                int pendingCount = Interlocked.Decrement(ref _currentPendingDeliveryCount);
                if (pendingCount == 0 && _shuttingDown)
                {
                    if (_log.IsDebugEnabled)
                        _log.DebugFormat("Consumer shutdown completed: {0}", _inputAddress);

                    _deliveryComplete.TrySetResult(true);
                }
            }
        }
示例#9
0
        async void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange,
                                                     string routingKey,
                                                     IBasicProperties properties, byte[] body)
        {
            LogContext.Current = _context.LogContext;

            if (IsStopping && _receiveSettings.NoAck == false)
            {
                await WaitAndAbandonMessage(deliveryTag).ConfigureAwait(false);

                return;
            }

            var delivery = _tracker.BeginDelivery();

            var context = new RabbitMqReceiveContext(exchange, routingKey, _consumerTag, deliveryTag, body, redelivered, properties, _context, _receiveSettings,
                                                     _model, _model.ConnectionContext);

            var activity = LogContext.IfEnabled(OperationName.Transport.Receive)?.StartReceiveActivity(context);

            try
            {
                if (!_pending.TryAdd(deliveryTag, context))
                {
                    LogContext.Warning?.Log("Duplicate BasicDeliver: {DeliveryTag}", deliveryTag);
                }

                if (_context.ReceiveObservers.Count > 0)
                {
                    await _context.ReceiveObservers.PreReceive(context).ConfigureAwait(false);
                }

                await _context.ReceivePipe.Send(context).ConfigureAwait(false);

                await context.ReceiveCompleted.ConfigureAwait(false);

                if (_receiveSettings.NoAck == false)
                {
                    _model.BasicAck(deliveryTag, false);
                }

                if (_context.ReceiveObservers.Count > 0)
                {
                    await _context.ReceiveObservers.PostReceive(context).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                if (_context.ReceiveObservers.Count > 0)
                {
                    await _context.ReceiveObservers.ReceiveFault(context, ex).ConfigureAwait(false);
                }

                try
                {
                    if (_receiveSettings.NoAck == false)
                    {
                        _model.BasicNack(deliveryTag, false, true);
                    }
                }
                catch (Exception ackEx)
                {
                    LogContext.Error?.Log(ackEx, "Message NACK failed: {DeliveryTag}", deliveryTag);
                }
            }
            finally
            {
                activity?.Stop();

                delivery.Dispose();

                _pending.TryRemove(deliveryTag, out _);
                context.Dispose();
            }
        }
        async void IBasicConsumer.HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange,
            string routingKey,
            IBasicProperties properties, byte[] body)
        {
            if (_shuttingDown)
            {
                await WaitAndAbandonMessage(deliveryTag).ConfigureAwait(false);
                return;
            }

            using (var delivery = _tracker.BeginDelivery())
            {
                var context = new RabbitMqReceiveContext(_inputAddress, exchange, routingKey, _consumerTag, deliveryTag, body, redelivered, properties,
                    _receiveObserver, _sendEndpointProvider, _publishEndpointProvider);

                context.GetOrAddPayload(() => _receiveSettings);
                context.GetOrAddPayload(() => _model);
                context.GetOrAddPayload(() => _model.ConnectionContext);

                try
                {
                    if (!_pending.TryAdd(deliveryTag, context))
                    {
                        if (_log.IsErrorEnabled)
                            _log.ErrorFormat("Duplicate BasicDeliver: {0}", deliveryTag);
                    }

                    await _receiveObserver.PreReceive(context).ConfigureAwait(false);

                    await _receivePipe.Send(context).ConfigureAwait(false);

                    await context.CompleteTask.ConfigureAwait(false);

                    _model.BasicAck(deliveryTag, false);

                    await _receiveObserver.PostReceive(context).ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);
                    try
                    {
                        _model.BasicNack(deliveryTag, false, true);
                    }
                    catch (Exception ackEx)
                    {
                        if (_log.IsErrorEnabled)
                            _log.ErrorFormat("An error occurred trying to NACK a message with delivery tag {0}: {1}", deliveryTag, ackEx.ToString());
                    }
                }
                finally
                {
                    RabbitMqReceiveContext ignored;
                    _pending.TryRemove(deliveryTag, out ignored);

                    context.Dispose();
                }
            }
        }