| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using RabbitMQ.Client; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | internal class RabbitMqReceiveContext : ReceiveContext |
| | | 12 | | { |
| | | 13 | | private readonly ulong _deliveryTag; |
| | | 14 | | private readonly IModel _sourceChannel; |
| | | 15 | | private readonly bool _isAutoAck; |
| | | 16 | | private readonly ILogger _logger; |
| | | 17 | | |
| | 3 | 18 | | public RabbitMqReceiveContext( |
| | 3 | 19 | | ulong deliveryTag, |
| | 3 | 20 | | IModel sourceChannel, |
| | 3 | 21 | | bool isAutoAck, |
| | 3 | 22 | | ILogger logger) |
| | | 23 | | { |
| | 3 | 24 | | _deliveryTag = deliveryTag; |
| | 3 | 25 | | _sourceChannel = sourceChannel; |
| | 3 | 26 | | _isAutoAck = isAutoAck; |
| | 3 | 27 | | _logger = logger; |
| | 3 | 28 | | } |
| | | 29 | | |
| | | 30 | | protected override async Task OnAbandonAsync(CancellationToken token) |
| | | 31 | | { |
| | 0 | 32 | | await Task.Run(() => |
| | 0 | 33 | | _logger.LogError("Dispatch failed for message with delivery tag: {deliveryTag}", _deliveryTag), |
| | 0 | 34 | | token); |
| | | 35 | | |
| | 0 | 36 | | if (!_isAutoAck) |
| | | 37 | | { |
| | | 38 | | // Note: The default behavior is to requeue the message. This works well with Quorum queues |
| | | 39 | | // as they have a delivery limit, but it is a known issue that Classic queues do not have a |
| | | 40 | | // delivery limit, exposing them to risk of a DOS. |
| | | 41 | | // GitHub Issue: https://github.com/rabbitmq/rabbitmq-server/issues/2013 |
| | 0 | 42 | | _sourceChannel.BasicNack(_deliveryTag, false, true); |
| | | 43 | | } |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | protected override async Task OnCompleteAsync(CancellationToken token) |
| | | 47 | | { |
| | 3 | 48 | | if (!_isAutoAck) |
| | | 49 | | { |
| | 6 | 50 | | await Task.Run(() => _sourceChannel.BasicAck(_deliveryTag, false), token); |
| | | 51 | | } |
| | 2 | 52 | | } |
| | | 53 | | } |
| | | 54 | | } |