Skip to content

Commit c623241

Browse files
committed
More CancellationToken todos
* Add cancellation token to methods in `IChannelExtensions` * Add cancellation token to Exchange recovery * Add cancellation token to Queue recovery * Add cancellation token to binding recovery
1 parent a9f331e commit c623241

10 files changed

+84
-77
lines changed

projects/RabbitMQ.Client/PublicAPI.Unshipped.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,13 @@ virtual RabbitMQ.Client.TcpClientAdapter.ReceiveTimeout.set -> void
937937
~RabbitMQ.Client.TopologyRecoveryExceptionHandler.ExchangeRecoveryExceptionHandlerAsync.set -> void
938938
~RabbitMQ.Client.TopologyRecoveryExceptionHandler.QueueRecoveryExceptionHandlerAsync.get -> System.Func<RabbitMQ.Client.IRecordedQueue, System.Exception, RabbitMQ.Client.IConnection, System.Threading.Tasks.Task>
939939
~RabbitMQ.Client.TopologyRecoveryExceptionHandler.QueueRecoveryExceptionHandlerAsync.set -> void
940-
~static RabbitMQ.Client.IChannelExtensions.AbortAsync(this RabbitMQ.Client.IChannel channel) -> System.Threading.Tasks.Task
940+
~static RabbitMQ.Client.IChannelExtensions.AbortAsync(this RabbitMQ.Client.IChannel channel, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task
941941
~static RabbitMQ.Client.IChannelExtensions.BasicConsumeAsync(this RabbitMQ.Client.IChannel channel, RabbitMQ.Client.IBasicConsumer consumer, string queue, bool autoAck = false, string consumerTag = "", bool noLocal = false, bool exclusive = false, System.Collections.Generic.IDictionary<string, object> arguments = null) -> System.Threading.Tasks.Task<string>
942942
~static RabbitMQ.Client.IChannelExtensions.BasicConsumeAsync(this RabbitMQ.Client.IChannel channel, string queue, bool autoAck, RabbitMQ.Client.IBasicConsumer consumer) -> System.Threading.Tasks.Task<string>
943943
~static RabbitMQ.Client.IChannelExtensions.BasicConsumeAsync(this RabbitMQ.Client.IChannel channel, string queue, bool autoAck, string consumerTag, RabbitMQ.Client.IBasicConsumer consumer) -> System.Threading.Tasks.Task<string>
944944
~static RabbitMQ.Client.IChannelExtensions.BasicConsumeAsync(this RabbitMQ.Client.IChannel channel, string queue, bool autoAck, string consumerTag, System.Collections.Generic.IDictionary<string, object> arguments, RabbitMQ.Client.IBasicConsumer consumer) -> System.Threading.Tasks.Task<string>
945-
~static RabbitMQ.Client.IChannelExtensions.CloseAsync(this RabbitMQ.Client.IChannel channel) -> System.Threading.Tasks.Task
946-
~static RabbitMQ.Client.IChannelExtensions.CloseAsync(this RabbitMQ.Client.IChannel channel, ushort replyCode, string replyText) -> System.Threading.Tasks.Task
945+
~static RabbitMQ.Client.IChannelExtensions.CloseAsync(this RabbitMQ.Client.IChannel channel, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task
946+
~static RabbitMQ.Client.IChannelExtensions.CloseAsync(this RabbitMQ.Client.IChannel channel, ushort replyCode, string replyText, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task
947947
~static RabbitMQ.Client.IChannelExtensions.ExchangeDeclareAsync(this RabbitMQ.Client.IChannel channel, string exchange, string type, bool durable = false, bool autoDelete = false, System.Collections.Generic.IDictionary<string, object> arguments = null, bool noWait = false) -> System.Threading.Tasks.Task
948948
~static RabbitMQ.Client.IChannelExtensions.QueueDeclareAsync(this RabbitMQ.Client.IChannel channel, string queue = "", bool durable = false, bool exclusive = true, bool autoDelete = true, System.Collections.Generic.IDictionary<string, object> arguments = null, bool noWait = false) -> System.Threading.Tasks.Task<RabbitMQ.Client.QueueDeclareOk>
949949
~static RabbitMQ.Client.IChannelExtensions.QueueDeleteAsync(this RabbitMQ.Client.IChannel channel, string queue, bool ifUnused = false, bool ifEmpty = false) -> System.Threading.Tasks.Task<uint>

projects/RabbitMQ.Client/client/api/IChannelExtensions.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
using System;
3333
using System.Collections.Generic;
34+
using System.Threading;
3435
using System.Threading.Tasks;
3536
using RabbitMQ.Client.client.impl;
3637

@@ -145,12 +146,13 @@ public static Task QueueUnbindAsync(this IChannel channel, string queue, string
145146
/// method does nothing but wait for the in-progress close
146147
/// operation to complete. This method will not return to the
147148
/// caller until the shutdown is complete.
148-
/// In comparison to normal <see cref="CloseAsync(IChannel)"/> method, <see cref="AbortAsync(IChannel)"/> will not throw
149+
/// In comparison to normal <see cref="CloseAsync(IChannel, CancellationToken)"/> method, <see cref="AbortAsync(IChannel, CancellationToken)"/> will not throw
149150
/// <see cref="Exceptions.AlreadyClosedException"/> or <see cref="System.IO.IOException"/> or any other <see cref="Exception"/> during closing channel.
150151
/// </remarks>
151-
public static Task AbortAsync(this IChannel channel)
152+
public static Task AbortAsync(this IChannel channel, CancellationToken cancellationToken = default)
152153
{
153-
return channel.CloseAsync(Constants.ReplySuccess, "Goodbye", true);
154+
return channel.CloseAsync(Constants.ReplySuccess, "Goodbye", true,
155+
cancellationToken);
154156
}
155157

156158
/// <summary>Asynchronously close this session.</summary>
@@ -160,9 +162,10 @@ public static Task AbortAsync(this IChannel channel)
160162
/// operation to complete. This method will not return to the
161163
/// caller until the shutdown is complete.
162164
/// </remarks>
163-
public static Task CloseAsync(this IChannel channel)
165+
public static Task CloseAsync(this IChannel channel, CancellationToken cancellationToken = default)
164166
{
165-
return channel.CloseAsync(Constants.ReplySuccess, "Goodbye", false);
167+
return channel.CloseAsync(Constants.ReplySuccess, "Goodbye", false,
168+
cancellationToken);
166169
}
167170

168171
/// <summary>
@@ -171,6 +174,7 @@ public static Task CloseAsync(this IChannel channel)
171174
/// <param name="channel">The channel.</param>
172175
/// <param name="replyCode">The reply code.</param>
173176
/// <param name="replyText">The reply text.</param>
177+
/// <param name="cancellationToken">The cancellation token.</param>
174178
/// <remarks>
175179
/// The method behaves in the same way as Close(), with the only
176180
/// difference that the channel is closed with the given channel
@@ -181,9 +185,10 @@ public static Task CloseAsync(this IChannel channel)
181185
/// A message indicating the reason for closing the channel
182186
/// </para>
183187
/// </remarks>
184-
public static Task CloseAsync(this IChannel channel, ushort replyCode, string replyText)
188+
public static Task CloseAsync(this IChannel channel, ushort replyCode, string replyText,
189+
CancellationToken cancellationToken = default)
185190
{
186-
return channel.CloseAsync(replyCode, replyText, false);
191+
return channel.CloseAsync(replyCode, replyText, false, cancellationToken);
187192
}
188193
}
189194
}

projects/RabbitMQ.Client/client/impl/AutorecoveringChannel.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public async Task ExchangeDeleteAsync(string exchange, bool ifUnused, bool noWai
351351
{
352352
await InnerChannel.ExchangeDeleteAsync(exchange, ifUnused, noWait, cancellationToken)
353353
.ConfigureAwait(false);
354-
await _connection.DeleteRecordedExchangeAsync(exchange, recordedEntitiesSemaphoreHeld: false)
354+
await _connection.DeleteRecordedExchangeAsync(exchange, recordedEntitiesSemaphoreHeld: false, cancellationToken)
355355
.ConfigureAwait(false);
356356
}
357357

@@ -361,11 +361,11 @@ public async Task ExchangeUnbindAsync(string destination, string source, string
361361
{
362362
ThrowIfDisposed();
363363
var recordedBinding = new RecordedBinding(false, destination, source, routingKey, arguments);
364-
await _connection.DeleteRecordedBindingAsync(recordedBinding, recordedEntitiesSemaphoreHeld: false)
364+
await _connection.DeleteRecordedBindingAsync(recordedBinding, recordedEntitiesSemaphoreHeld: false, cancellationToken)
365365
.ConfigureAwait(false);
366366
await InnerChannel.ExchangeUnbindAsync(destination, source, routingKey, arguments, noWait, cancellationToken)
367367
.ConfigureAwait(false);
368-
await _connection.DeleteAutoDeleteExchangeAsync(source, recordedEntitiesSemaphoreHeld: false)
368+
await _connection.DeleteAutoDeleteExchangeAsync(source, recordedEntitiesSemaphoreHeld: false, cancellationToken)
369369
.ConfigureAwait(false);
370370
}
371371

@@ -396,7 +396,7 @@ public async Task<QueueDeclareOk> QueueDeclareAsync(string queue, bool durable,
396396
if (false == passive)
397397
{
398398
var recordedQueue = new RecordedQueue(result.QueueName, queue.Length == 0, durable, exclusive, autoDelete, arguments);
399-
await _connection.RecordQueueAsync(recordedQueue, recordedEntitiesSemaphoreHeld: false)
399+
await _connection.RecordQueueAsync(recordedQueue, recordedEntitiesSemaphoreHeld: false, cancellationToken)
400400
.ConfigureAwait(false);
401401
}
402402
return result;
@@ -415,7 +415,7 @@ public async Task<uint> QueueDeleteAsync(string queue, bool ifUnused, bool ifEmp
415415
{
416416
uint result = await InnerChannel.QueueDeleteAsync(queue, ifUnused, ifEmpty, noWait, cancellationToken)
417417
.ConfigureAwait(false);
418-
await _connection.DeleteRecordedQueueAsync(queue, recordedEntitiesSemaphoreHeld: false)
418+
await _connection.DeleteRecordedQueueAsync(queue, recordedEntitiesSemaphoreHeld: false, cancellationToken)
419419
.ConfigureAwait(false);
420420
return result;
421421
}
@@ -429,11 +429,11 @@ public async Task QueueUnbindAsync(string queue, string exchange, string routing
429429
{
430430
ThrowIfDisposed();
431431
var recordedBinding = new RecordedBinding(true, queue, exchange, routingKey, arguments);
432-
await _connection.DeleteRecordedBindingAsync(recordedBinding, recordedEntitiesSemaphoreHeld: false)
432+
await _connection.DeleteRecordedBindingAsync(recordedBinding, recordedEntitiesSemaphoreHeld: false, cancellationToken)
433433
.ConfigureAwait(false);
434434
await _innerChannel.QueueUnbindAsync(queue, exchange, routingKey, arguments, cancellationToken)
435435
.ConfigureAwait(false);
436-
await _connection.DeleteAutoDeleteExchangeAsync(exchange, recordedEntitiesSemaphoreHeld: false)
436+
await _connection.DeleteAutoDeleteExchangeAsync(exchange, recordedEntitiesSemaphoreHeld: false, cancellationToken)
437437
.ConfigureAwait(false);
438438
}
439439

projects/RabbitMQ.Client/client/impl/AutorecoveringConnection.Recording.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private void DoRecordExchange(in RecordedExchange exchange)
8383
}
8484

8585
internal async ValueTask DeleteRecordedExchangeAsync(string exchangeName,
86-
bool recordedEntitiesSemaphoreHeld)
86+
bool recordedEntitiesSemaphoreHeld, CancellationToken cancellationToken)
8787
{
8888
if (_disposed)
8989
{
@@ -92,16 +92,16 @@ internal async ValueTask DeleteRecordedExchangeAsync(string exchangeName,
9292

9393
if (recordedEntitiesSemaphoreHeld)
9494
{
95-
await DoDeleteRecordedExchangeAsync(exchangeName)
95+
await DoDeleteRecordedExchangeAsync(exchangeName, cancellationToken)
9696
.ConfigureAwait(false);
9797
}
9898
else
9999
{
100-
await _recordedEntitiesSemaphore.WaitAsync()
100+
await _recordedEntitiesSemaphore.WaitAsync(cancellationToken)
101101
.ConfigureAwait(false);
102102
try
103103
{
104-
await DoDeleteRecordedExchangeAsync(exchangeName)
104+
await DoDeleteRecordedExchangeAsync(exchangeName, cancellationToken)
105105
.ConfigureAwait(false);
106106
}
107107
finally
@@ -110,7 +110,7 @@ await DoDeleteRecordedExchangeAsync(exchangeName)
110110
}
111111
}
112112

113-
async Task DoDeleteRecordedExchangeAsync(string exchangeName)
113+
async Task DoDeleteRecordedExchangeAsync(string exchangeName, CancellationToken cancellationToken)
114114
{
115115
_recordedExchanges.Remove(exchangeName);
116116

@@ -120,18 +120,18 @@ async Task DoDeleteRecordedExchangeAsync(string exchangeName)
120120
if (binding.Destination == exchangeName)
121121
{
122122
await DeleteRecordedBindingAsync(binding,
123-
recordedEntitiesSemaphoreHeld: true)
123+
recordedEntitiesSemaphoreHeld: true, cancellationToken)
124124
.ConfigureAwait(false);
125125
await DeleteAutoDeleteExchangeAsync(binding.Source,
126-
recordedEntitiesSemaphoreHeld: true)
126+
recordedEntitiesSemaphoreHeld: true, cancellationToken)
127127
.ConfigureAwait(false);
128128
}
129129
}
130130
}
131131
}
132132

133133
internal async ValueTask DeleteAutoDeleteExchangeAsync(string exchangeName,
134-
bool recordedEntitiesSemaphoreHeld)
134+
bool recordedEntitiesSemaphoreHeld, CancellationToken cancellationToken)
135135
{
136136
if (_disposed)
137137
{
@@ -144,7 +144,7 @@ internal async ValueTask DeleteAutoDeleteExchangeAsync(string exchangeName,
144144
}
145145
else
146146
{
147-
await _recordedEntitiesSemaphore.WaitAsync()
147+
await _recordedEntitiesSemaphore.WaitAsync(cancellationToken)
148148
.ConfigureAwait(false);
149149
try
150150
{
@@ -185,7 +185,7 @@ bool AnyBindingsOnExchange(string exchange)
185185
internal int RecordedQueuesCount => _recordedQueues.Count;
186186

187187
internal async ValueTask RecordQueueAsync(RecordedQueue queue,
188-
bool recordedEntitiesSemaphoreHeld)
188+
bool recordedEntitiesSemaphoreHeld, CancellationToken cancellationToken)
189189
{
190190
if (_disposed)
191191
{
@@ -198,7 +198,7 @@ internal async ValueTask RecordQueueAsync(RecordedQueue queue,
198198
}
199199
else
200200
{
201-
await _recordedEntitiesSemaphore.WaitAsync()
201+
await _recordedEntitiesSemaphore.WaitAsync(cancellationToken)
202202
.ConfigureAwait(false);
203203
try
204204
{
@@ -217,7 +217,7 @@ private void DoRecordQueue(RecordedQueue queue)
217217
}
218218

219219
internal async ValueTask DeleteRecordedQueueAsync(string queueName,
220-
bool recordedEntitiesSemaphoreHeld)
220+
bool recordedEntitiesSemaphoreHeld, CancellationToken cancellationToken)
221221
{
222222
if (_disposed)
223223
{
@@ -226,16 +226,16 @@ internal async ValueTask DeleteRecordedQueueAsync(string queueName,
226226

227227
if (recordedEntitiesSemaphoreHeld)
228228
{
229-
await DoDeleteRecordedQueueAsync(queueName)
229+
await DoDeleteRecordedQueueAsync(queueName, cancellationToken)
230230
.ConfigureAwait(false);
231231
}
232232
else
233233
{
234-
await _recordedEntitiesSemaphore.WaitAsync()
234+
await _recordedEntitiesSemaphore.WaitAsync(cancellationToken)
235235
.ConfigureAwait(false);
236236
try
237237
{
238-
await DoDeleteRecordedQueueAsync(queueName)
238+
await DoDeleteRecordedQueueAsync(queueName, cancellationToken)
239239
.ConfigureAwait(false);
240240
}
241241
finally
@@ -244,7 +244,7 @@ await DoDeleteRecordedQueueAsync(queueName)
244244
}
245245
}
246246

247-
async ValueTask DoDeleteRecordedQueueAsync(string queueName)
247+
async ValueTask DoDeleteRecordedQueueAsync(string queueName, CancellationToken cancellationToken)
248248
{
249249
_recordedQueues.Remove(queueName);
250250

@@ -254,10 +254,10 @@ async ValueTask DoDeleteRecordedQueueAsync(string queueName)
254254
if (binding.Destination == queueName)
255255
{
256256
await DeleteRecordedBindingAsync(binding,
257-
recordedEntitiesSemaphoreHeld: true)
257+
recordedEntitiesSemaphoreHeld: true, cancellationToken)
258258
.ConfigureAwait(false);
259259
await DeleteAutoDeleteExchangeAsync(binding.Source,
260-
recordedEntitiesSemaphoreHeld: true)
260+
recordedEntitiesSemaphoreHeld: true, cancellationToken)
261261
.ConfigureAwait(false);
262262
}
263263
}
@@ -298,7 +298,7 @@ private void DoRecordBinding(in RecordedBinding binding)
298298
}
299299

300300
internal async ValueTask DeleteRecordedBindingAsync(RecordedBinding rb,
301-
bool recordedEntitiesSemaphoreHeld)
301+
bool recordedEntitiesSemaphoreHeld, CancellationToken cancellationToken)
302302
{
303303
if (_disposed)
304304
{
@@ -311,7 +311,7 @@ internal async ValueTask DeleteRecordedBindingAsync(RecordedBinding rb,
311311
}
312312
else
313313
{
314-
await _recordedEntitiesSemaphore.WaitAsync()
314+
await _recordedEntitiesSemaphore.WaitAsync(cancellationToken)
315315
.ConfigureAwait(false);
316316
try
317317
{

0 commit comments

Comments
 (0)