Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for BasicNack in the RabbitMQBasicConsumer #1605

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/DotNetCore.CAP.RabbitMQ/RabbitMQBasicConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ namespace DotNetCore.CAP.RabbitMQ;

public class RabbitMQBasicConsumer : AsyncDefaultBasicConsumer
{
private readonly SemaphoreSlim _semaphore;
private readonly string _groupName;
private readonly bool _usingTaskRun;
private readonly Func<TransportMessage, object?, Task> _msgCallback;
private readonly Action<LogMessageEventArgs> _logCallback;
private readonly Func<BasicDeliverEventArgs, IServiceProvider, List<KeyValuePair<string, string>>>? _customHeadersBuilder;
private readonly IServiceProvider _serviceProvider;

protected SemaphoreSlim Semaphore { get; }

public RabbitMQBasicConsumer(IModel? model,
byte concurrent, string groupName,
Func<TransportMessage, object?, Task> msgCallback,
Expand All @@ -31,7 +32,7 @@ public RabbitMQBasicConsumer(IModel? model,
IServiceProvider serviceProvider)
: base(model)
{
_semaphore = new SemaphoreSlim(concurrent);
Semaphore = new SemaphoreSlim(concurrent);
_groupName = groupName;
_usingTaskRun = concurrent > 0;
_msgCallback = msgCallback;
Expand All @@ -45,7 +46,7 @@ public override async Task HandleBasicDeliver(string consumerTag, ulong delivery
{
if (_usingTaskRun)
{
await _semaphore.WaitAsync();
await Semaphore.WaitAsync();

_ = Task.Run(Consume).ConfigureAwait(false);
}
Expand Down Expand Up @@ -85,20 +86,28 @@ Task Consume()
}
}

public void BasicAck(ulong deliveryTag)
public virtual void BasicAck(ulong deliveryTag)
{
if (Model.IsOpen)
Model.BasicAck(deliveryTag, false);

_semaphore.Release();
Semaphore.Release();
}

public void BasicReject(ulong deliveryTag)
public virtual void BasicReject(ulong deliveryTag)
{
if (Model.IsOpen)
Model.BasicReject(deliveryTag, true);

_semaphore.Release();
Semaphore.Release();
}

public virtual void BasicNack(ulong deliveryTag)
{
if (Model.IsOpen)
Model.BasicNack(deliveryTag, false, true);

Semaphore.Release();
}

public override async Task OnCancel(params string[] consumerTags)
Expand Down