Beispiel #1
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);
            }
        }
Beispiel #2
0
        public async Task Handle(IOwinContext owinContext, Func <Task> next)
        {
            if (_stopping)
            {
                owinContext.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
                owinContext.Response.Write("Stopping");

                await next().ConfigureAwait(false);

                return;
            }

            using (_tracker.BeginDelivery())
            {
                var responseProxy = new HttpResponseSendEndpointProvider(_receiveSettings, owinContext, _inputAddress, _sendPipe);

                var context = new HttpReceiveContext(owinContext, false, _receiveObserver, responseProxy, _receiveSettings.PublishEndpointProvider);

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

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

                    await context.CompleteTask.ConfigureAwait(false);

                    //TODO: Push into Pipe! -- cant' be on the receive pipe because it doesn't have the content
                    if (!owinContext.Response.ContentLength.HasValue)
                    {
                        owinContext.Response.StatusCode = (int)HttpStatusCode.Accepted;
                        owinContext.Response.Write("");
                    }

                    await next().ConfigureAwait(false);

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

                    //TODO: Push into pipe?
                    owinContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }
                finally
                {
                    context.Dispose();
                }
            }
        }
Beispiel #3
0
        public async Task Handle(HttpContext httpContext, Func <Task> next)
        {
            if (IsStopping)
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
                await httpContext.Response.WriteAsync("Stopping").ConfigureAwait(false);

                await next().ConfigureAwait(false);

                return;
            }

            using (_tracker.BeginDelivery())
            {
                var responseEndpointTopology = _context.CreateResponseEndpointContext(httpContext);

                var context = new HttpReceiveContext(httpContext, false, _receiveObserver, responseEndpointTopology);

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

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

                    await context.CompleteTask.ConfigureAwait(false);

                    //TODO: Push into Pipe! -- can't be on the receive pipe because it doesn't have the content
                    if (!httpContext.Response.ContentLength.HasValue)
                    {
                        httpContext.Response.StatusCode = (int)HttpStatusCode.Accepted;
                    }

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

                    //TODO: ensure the Fault is written to the response pipe
                    httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                }
                finally
                {
                    context.Dispose();
                }
            }
        }
Beispiel #4
0
 Task IReceiveObserver.PostReceive(ReceiveContext context)
 {
     return(_receiveObserver.PostReceive(context));
 }
Beispiel #5
0
        async Task OnMessage(BrokeredMessage message)
        {
            int current = Interlocked.Increment(ref _currentPendingDeliveryCount);

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

            long deliveryCount = Interlocked.Increment(ref _deliveryCount);

            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Receiving {0}:{1} - {2}", deliveryCount, message.MessageId, _receiveSettings.QueueDescription.Path);
            }

            Exception exception = null;
            var       context   = new ServiceBusReceiveContext(_inputAddress, message, _receiveObserver);

            try
            {
                if (_shuttingDown)
                {
                    await _completeTask.Task;

                    throw new TransportException(_inputAddress, "Transport shutdown in progress, abandoning message");
                }

                await _receiveObserver.PreReceive(context);

                await _receivePipe.Send(context);

                await context.CompleteTask;

                await message.CompleteAsync();

                await _receiveObserver.PostReceive(context);

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Receive completed: {0}", message.MessageId);
                }
            }
            catch (Exception ex)
            {
                exception = ex;
                if (_log.IsErrorEnabled)
                {
                    _log.Error($"Received faulted: {message.MessageId}", ex);
                }
            }

            try
            {
                if (exception != null)
                {
                    await message.AbandonAsync();

                    await _receiveObserver.ReceiveFault(context, exception);
                }
            }
            finally
            {
                int pendingCount = Interlocked.Decrement(ref _currentPendingDeliveryCount);
                if (pendingCount == 0 && _shuttingDown)
                {
                    _completeTask.TrySetResult(this);
                }
            }
        }
Beispiel #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;
            }

            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);
                }
            }
        }
Beispiel #7
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();
                }
            }
        }
Beispiel #8
0
        async Task OnMessage(BrokeredMessage message)
        {
            if (_shuttingDown)
            {
                await WaitAndAbandonMessage(message).ConfigureAwait(false);

                return;
            }

            var current = Interlocked.Increment(ref _currentPendingDeliveryCount);

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

            var deliveryCount = Interlocked.Increment(ref _deliveryCount);

            if (_log.IsDebugEnabled)
            {
                _log.DebugFormat("Receiving {0}:{1} - {2}", deliveryCount, message.MessageId, _receiveSettings.QueueDescription.Path);
            }

            var context = new ServiceBusReceiveContext(_inputAddress, message, _receiveObserver);

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

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

                await context.CompleteTask.ConfigureAwait(false);

                await message.CompleteAsync().ConfigureAwait(false);

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

                if (_log.IsDebugEnabled)
                {
                    _log.DebugFormat("Receive completed: {0}", message.MessageId);
                }
            }
            catch (Exception ex)
            {
                if (_log.IsErrorEnabled)
                {
                    _log.Error($"Received faulted: {message.MessageId}", ex);
                }

                await message.AbandonAsync().ConfigureAwait(false);

                await _receiveObserver.ReceiveFault(context, ex).ConfigureAwait(false);
            }
            finally
            {
                var pendingCount = Interlocked.Decrement(ref _currentPendingDeliveryCount);
                if (pendingCount == 0 && _shuttingDown)
                {
                    if (_log.IsDebugEnabled)
                    {
                        _log.DebugFormat("Receiver shutdown completed: {0}", _inputAddress);
                    }

                    _participant.SetComplete();
                }
            }
        }
        async void HandleMessage(IMessage message)
        {
            if (IsStopping)
            {
                await WaitAndAbandonMessage(message).ConfigureAwait(false);

                return;
            }

            using (var delivery = _tracker.BeginDelivery())
            {
                var context = new ActiveMqReceiveContext(_inputAddress, message, _receiveObserver, _context);

                context.GetOrAddPayload(() => _errorTransport);
                context.GetOrAddPayload(() => _deadLetterTransport);

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

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

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

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

                    await context.CompleteTask.ConfigureAwait(false);

                    message.Acknowledge();

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

                    try
                    {
//                        _session.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}", message.NMSMessageId, ackEx.ToString());
                        }
                    }
                }
                finally
                {
                    _pending.TryRemove(message.NMSMessageId, out _);

                    context.Dispose();
                }
            }
        }
        public async Task HandleMessage(Message message)
        {
            if (IsStopping)
            {
                await WaitAndAbandonMessage(message).ConfigureAwait(false);

                return;
            }

            using (var delivery = _tracker.BeginDelivery())
            {
                var redelivered = message.Attributes.ContainsKey("ApproximateReceiveCount") && (int.TryParse(message.Attributes["ApproximateReceiveCount"], out var approximateReceiveCount) && approximateReceiveCount > 1);
                var context     = new AmazonSqsReceiveContext(_inputAddress, message, redelivered, _receiveObserver, _context);

                context.GetOrAddPayload(() => _errorTransport);
                context.GetOrAddPayload(() => _deadLetterTransport);

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

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

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

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

                    await context.CompleteTask.ConfigureAwait(false);

                    // Acknowledge
                    await _model.DeleteMessage(_queueUrl, message.ReceiptHandle);

                    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}", message.MessageId, ackEx.ToString());
                        }
                    }
                }
                finally
                {
                    _pending.TryRemove(message.MessageId, out _);

                    context.Dispose();
                }
            }
        }