-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ServiceBusProcessor.cs
1147 lines (1013 loc) · 51.5 KB
/
ServiceBusProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Shared;
using Azure.Messaging.ServiceBus.Core;
using Azure.Messaging.ServiceBus.Diagnostics;
namespace Azure.Messaging.ServiceBus
{
/// <summary>
/// The <see cref="ServiceBusProcessor"/> provides an abstraction around a set of <see cref="ServiceBusReceiver"/>
/// that allows using an event based model for processing received <see cref="ServiceBusReceivedMessage" />.
/// It is constructed by calling <see cref="ServiceBusClient.CreateProcessor(string, ServiceBusProcessorOptions)"/>.
/// The message handler is specified with the <see cref="ProcessMessageAsync"/>
/// property. The error handler is specified with the <see cref="ProcessErrorAsync"/> property.
/// To start processing after the handlers have been specified, call <see cref="StartProcessingAsync"/>.
/// </summary>
/// <remarks>
/// The <see cref="ServiceBusProcessor" /> is safe to cache and use for the lifetime of an application
/// or until the <see cref="ServiceBusClient" /> that it was created by is disposed. Caching the processor
/// is recommended when the application is processing messages regularly. The sender is responsible for
/// ensuring efficient network, CPU, and memory use. Calling <see cref="DisposeAsync" /> on the
/// associated <see cref="ServiceBusClient" /> as the application is shutting down will ensure that
/// network resources and other unmanaged objects used by the processor are properly cleaned up.
/// </remarks>
#pragma warning disable CA1001 // Types that own disposable fields should be disposable
public class ServiceBusProcessor : IAsyncDisposable
#pragma warning restore CA1001 // Types that own disposable fields should be disposable
{
private Func<ProcessMessageEventArgs, Task> _processMessageAsync;
private Func<ProcessSessionMessageEventArgs, Task> _processSessionMessageAsync;
private Func<ProcessErrorEventArgs, Task> _processErrorAsync;
internal Func<ProcessSessionEventArgs, Task> _sessionInitializingAsync;
internal Func<ProcessSessionEventArgs, Task> _sessionClosingAsync;
// we use int.MaxValue as the maxCount since the concurrency can be changed dynamically by the user
private readonly SemaphoreSlim _messageHandlerSemaphore = new SemaphoreSlim(0, int.MaxValue);
private readonly object _optionsLock = new();
/// <summary>
/// The primitive for ensuring that the service is not overloaded with
/// accept session requests.
/// </summary>
private readonly SemaphoreSlim _maxConcurrentAcceptSessionsSemaphore = new(0, int.MaxValue);
/// <summary>The primitive for synchronizing access during start and close operations.</summary>
private readonly SemaphoreSlim _processingStartStopSemaphore = new(1, 1);
private CancellationTokenSource RunningTaskTokenSource { get; set; }
private Task ActiveReceiveTask { get; set; }
/// <summary>
/// Gets the fully qualified Service Bus namespace that the receiver is associated with. This is likely
/// to be similar to <c>{yournamespace}.servicebus.windows.net</c>.
/// </summary>
public virtual string FullyQualifiedNamespace => Connection.FullyQualifiedNamespace;
/// <summary>
/// Gets the path of the Service Bus entity that the processor is connected to, specific to the
/// Service Bus namespace that contains it.
/// </summary>
public virtual string EntityPath { get; }
/// <summary>
/// Gets the ID used to identify this processor. This can be used to correlate logs and exceptions.
/// </summary>
public virtual string Identifier => Options.Identifier;
/// <summary>
/// Gets the <see cref="ReceiveMode"/> used to specify how messages are received. Defaults to PeekLock mode.
/// </summary>
/// <value>
/// The receive mode is specified using <see cref="ServiceBusProcessorOptions.ReceiveMode"/>
/// and has a default mode of <see cref="ServiceBusReceiveMode.PeekLock"/>.
/// </value>
public virtual ServiceBusReceiveMode ReceiveMode => Options.ReceiveMode;
/// <summary>
/// Gets whether the processor is configured to process session entities.
/// </summary>
internal bool IsSessionProcessor { get; }
/// <summary>
/// Gets the number of messages that will be eagerly requested from Queues or Subscriptions
/// during processing. This is intended to help maximize throughput by allowing the
/// processor to receive from a local cache rather than waiting on a service request.
/// </summary>
/// <value>
/// The prefetch count is specified using <see cref="ServiceBusProcessorOptions.PrefetchCount"/>
/// and has a default value of 0.
/// </value>
public virtual int PrefetchCount => Options.PrefetchCount;
/// <summary>
/// Gets whether or not this processor is currently processing messages.
/// </summary>
///
/// <value>
/// <c>true</c> if the processor is processing messages; otherwise, <c>false</c>.
/// </value>
public virtual bool IsProcessing => ActiveReceiveTask != null;
internal ServiceBusProcessorOptions Options { get; }
/// <summary>
/// The active connection to the Azure Service Bus service, enabling client communications for metadata
/// about the associated Service Bus entity and access to transport-aware consumers.
/// </summary>
internal readonly ServiceBusConnection Connection;
/// <summary>Gets the maximum number of concurrent calls to the
/// <see cref="ProcessMessageAsync"/> message handler the processor should initiate.
/// </summary>
/// <value>
/// The number of maximum concurrent calls is specified using <see cref="ServiceBusProcessorOptions.MaxConcurrentCalls"/>
/// and has a default value of 1.
/// </value>
public virtual int MaxConcurrentCalls => Options.MaxConcurrentCalls;
private int _currentConcurrentCalls;
internal int MaxConcurrentSessions => _maxConcurrentSessions;
private volatile int _maxConcurrentSessions;
private int _currentConcurrentSessions;
internal int MaxConcurrentCallsPerSession => _maxConcurrentCallsPerSession;
private volatile int _maxConcurrentCallsPerSession;
private int _currentAcceptSessions;
internal TimeSpan? MaxReceiveWaitTime => Options.MaxReceiveWaitTime;
/// <summary>
/// Gets a value that indicates whether the processor should automatically
/// complete messages after the message handler has completed processing. If the
/// message handler triggers an exception, the message will not be automatically
/// completed.
/// </summary>
/// <remarks>
/// If the message handler triggers an exception and did not settle the message,
/// then the message will be automatically abandoned, irrespective of <see cref= "AutoCompleteMessages" />.
/// </remarks>
/// <value>
/// The option to auto complete messages is specified using <see cref="ServiceBusProcessorOptions.AutoCompleteMessages"/>
/// and has a default value of <c>true</c>.
/// </value>
public virtual bool AutoCompleteMessages { get; }
/// <summary>
/// Gets the maximum duration within which the lock will be renewed automatically. This
/// value should be greater than the longest message lock duration; for example, the LockDuration Property.
/// </summary>
/// <remarks>The message renew can continue for sometime in the background
/// after completion of message and result in a few false MessageLockLostExceptions temporarily.</remarks>
/// <value>
/// The maximum duration for lock renewal is specified using <see cref="ServiceBusProcessorOptions.MaxAutoLockRenewalDuration"/>
/// and has a default value of 5 minutes.
/// </value>
public virtual TimeSpan MaxAutoLockRenewalDuration => Options.MaxAutoLockRenewalDuration;
/// <summary>
/// The instance of <see cref="ServiceBusEventSource" /> which can be mocked for testing.
/// </summary>
internal ServiceBusEventSource Logger { get; set; } = ServiceBusEventSource.Log;
/// <summary>
/// Indicates whether or not this <see cref="ServiceBusProcessor"/> has been closed.
/// </summary>
///
/// <value>
/// <c>true</c> if the processor is closed; otherwise, <c>false</c>.
/// </value>
public virtual bool IsClosed
{
get => _closed;
private set => _closed = value;
}
// If the user has listed named sessions, and they
// have MaxConcurrentSessions greater or equal to the number
// of sessions, we can leave the sessions open at all times
// instead of cycling through them as receive calls time out.
internal bool KeepOpenOnReceiveTimeout => _sessionIds.Length > 0 && _maxConcurrentSessions >= _sessionIds.Length;
/// <summary>Indicates whether or not this instance has been closed.</summary>
private volatile bool _closed;
private readonly string[] _sessionIds;
private readonly MessagingClientDiagnostics _clientDiagnostics;
// deliberate usage of List instead of IList for faster enumeration and less allocations
private readonly List<ReceiverManager> _receiverManagers = new List<ReceiverManager>();
private readonly ServiceBusSessionProcessor _sessionProcessor;
internal List<(Task Task, CancellationTokenSource Cts)> TaskTuples { get; private set; } = new();
private readonly List<ReceiverManager> _orphanedReceiverManagers = new();
private CancellationTokenSource _handlerCts = new();
private readonly int _processorCount = Environment.ProcessorCount;
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusProcessor"/> class.
/// </summary>
///
/// <param name="connection">The <see cref="ServiceBusConnection" /> connection to use for communication with the Service Bus service.</param>
/// <param name="entityPath">The queue name or subscription path to process messages from.</param>
/// <param name="isSessionEntity">Whether or not the processor is associated with a session entity.</param>
/// <param name="options">The set of options to use when configuring the processor.</param>
/// <param name="sessionIds">An optional set of session Ids to limit processing to.
/// Only applies if isSessionEntity is true.</param>
/// <param name="maxConcurrentSessions">The max number of sessions that can be processed concurrently.
/// Only applies if isSessionEntity is true.</param>
/// <param name="maxConcurrentCallsPerSession">The max number of concurrent calls per session.
/// Only applies if isSessionEntity is true.</param>
/// <param name="sessionProcessor">If this is for a session processor, will contain the session processor instance.</param>
internal ServiceBusProcessor(
ServiceBusConnection connection,
string entityPath,
bool isSessionEntity,
ServiceBusProcessorOptions options,
string[] sessionIds = default,
int maxConcurrentSessions = default,
int maxConcurrentCallsPerSession = default,
ServiceBusSessionProcessor sessionProcessor = default)
{
Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath));
Argument.AssertNotNull(connection, nameof(connection));
Argument.AssertNotNull(connection.RetryOptions, nameof(connection.RetryOptions));
connection.ThrowIfClosed();
Options = options?.Clone() ?? new ServiceBusProcessorOptions();
Connection = connection;
EntityPath = EntityNameFormatter.FormatEntityPath(entityPath, Options.SubQueue);
Options.Identifier = string.IsNullOrEmpty(Options.Identifier) ? DiagnosticUtilities.GenerateIdentifier(EntityPath) : Options.Identifier;
_maxConcurrentSessions = maxConcurrentSessions;
_maxConcurrentCallsPerSession = maxConcurrentCallsPerSession;
_sessionIds = sessionIds ?? Array.Empty<string>();
_sessionProcessor = sessionProcessor;
if (isSessionEntity)
{
Options.MaxConcurrentCalls = (_sessionIds.Length > 0
? Math.Min(_sessionIds.Length, _maxConcurrentSessions)
: _maxConcurrentSessions) * _maxConcurrentCallsPerSession;
}
AutoCompleteMessages = Options.AutoCompleteMessages;
IsSessionProcessor = isSessionEntity;
_clientDiagnostics = new MessagingClientDiagnostics(
DiagnosticProperty.DiagnosticNamespace,
DiagnosticProperty.ResourceProviderNamespace,
DiagnosticProperty.ServiceBusServiceContext,
FullyQualifiedNamespace,
EntityPath);
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusProcessor"/> class for mocking.
/// </summary>
protected ServiceBusProcessor()
{
// assign default options since some of the properties reach into the options
Options = new ServiceBusProcessorOptions();
_sessionIds = Array.Empty<string>();
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusProcessor"/> class for use with derived types.
/// </summary>
/// <param name="client">The client instance to use for the processor.</param>
/// <param name="queueName">The name of the queue to processor from.</param>
/// <param name="options">The set of options to use when configuring the processor.</param>
protected ServiceBusProcessor(ServiceBusClient client, string queueName, ServiceBusProcessorOptions options) :
this(client?.Connection, queueName, false, options)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ServiceBusProcessor"/> class for use with derived types.
/// </summary>
/// <param name="client">The client instance to use for the processor.</param>
/// <param name="topicName">The topic to create a processor for.</param>
/// <param name="subscriptionName">The subscription to create a processor for.</param>
/// <param name="options">The set of options to use when configuring the processor.</param>
protected ServiceBusProcessor(ServiceBusClient client, string topicName, string subscriptionName,
ServiceBusProcessorOptions options) :
this(client?.Connection, EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName), false, options)
{
}
/// <summary>
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
/// </summary>
///
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
///
/// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => base.Equals(obj);
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
///
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
///
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => base.GetHashCode();
/// <summary>
/// Converts the instance to string representation.
/// </summary>
///
/// <returns>A <see cref="System.String" /> that represents this instance.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public override string ToString() => base.ToString();
/// <summary>
/// The handler responsible for processing messages received from the Queue
/// or Subscription.
/// Implementation is mandatory.
/// </summary>
/// <remarks>
/// It is not recommended that the state of the processor be managed directly from within this handler; requesting to start or stop the processor may result in
/// a deadlock scenario.
/// </remarks>
[SuppressMessage("Usage", "AZC0003:DO make service methods virtual.",
Justification = "This member follows the standard .NET event pattern; override via the associated On<<EVENT>> method.")]
public event Func<ProcessMessageEventArgs, Task> ProcessMessageAsync
{
add
{
Argument.AssertNotNull(value, nameof(ProcessMessageAsync));
if (_processMessageAsync != default)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned);
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
EnsureNotRunningAndInvoke(() => _processMessageAsync = value);
}
remove
{
Argument.AssertNotNull(value, nameof(ProcessMessageAsync));
if (_processMessageAsync != value)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new ArgumentException(Resources.HandlerHasNotBeenAssigned);
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
EnsureNotRunningAndInvoke(() => _processMessageAsync = default);
}
}
/// <summary>
/// The handler responsible for processing messages received from the Queue
/// or Subscription. Implementation is mandatory.
/// </summary>
[SuppressMessage("Usage", "AZC0003:DO make service methods virtual.",
Justification = "This member follows the standard .NET event pattern; override via the associated On<<EVENT>> method.")]
internal event Func<ProcessSessionMessageEventArgs, Task> ProcessSessionMessageAsync
{
add
{
Argument.AssertNotNull(value, nameof(ProcessSessionMessageAsync));
if (_processSessionMessageAsync != default)
{
throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned);
}
EnsureNotRunningAndInvoke(() => _processSessionMessageAsync = value);
}
remove
{
Argument.AssertNotNull(value, nameof(ProcessSessionMessageAsync));
if (_processSessionMessageAsync != value)
{
throw new ArgumentException(Resources.HandlerHasNotBeenAssigned);
}
EnsureNotRunningAndInvoke(() => _processSessionMessageAsync = default);
}
}
/// <summary>
/// The handler responsible for processing unhandled exceptions thrown while
/// this processor is running.
/// Implementation is mandatory.
/// </summary>
/// <remarks>
/// It is not recommended that the state of the processor be managed directly from within this handler; requesting to start or stop the processor may result in
/// a deadlock scenario.
/// </remarks>
[SuppressMessage("Usage", "AZC0003:DO make service methods virtual.",
Justification = "This member follows the standard .NET event pattern; override via the associated On<<EVENT>> method.")]
public event Func<ProcessErrorEventArgs, Task> ProcessErrorAsync
{
add
{
Argument.AssertNotNull(value, nameof(ProcessErrorAsync));
if (_processErrorAsync != default)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned);
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
EnsureNotRunningAndInvoke(() => _processErrorAsync = value);
}
remove
{
Argument.AssertNotNull(value, nameof(ProcessErrorAsync));
if (_processErrorAsync != value)
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
throw new ArgumentException(Resources.HandlerHasNotBeenAssigned);
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}
EnsureNotRunningAndInvoke(() => _processErrorAsync = default);
}
}
/// <summary>
/// Optional handler that can be set to be notified when a new session is about to be processed.
/// </summary>
[SuppressMessage("Usage", "AZC0003:DO make service methods virtual.",
Justification = "This member follows the standard .NET event pattern; override via the associated On<<EVENT>> method.")]
internal event Func<ProcessSessionEventArgs, Task> SessionInitializingAsync
{
add
{
Argument.AssertNotNull(value, nameof(SessionInitializingAsync));
if (_sessionInitializingAsync != default)
{
throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned);
}
EnsureNotRunningAndInvoke(() => _sessionInitializingAsync = value);
}
remove
{
Argument.AssertNotNull(value, nameof(SessionInitializingAsync));
if (_sessionInitializingAsync != value)
{
throw new ArgumentException(Resources.HandlerHasNotBeenAssigned);
}
EnsureNotRunningAndInvoke(() => _sessionInitializingAsync = default);
}
}
/// <summary>
/// Optional handler that can be set to be notified when a session is about to be closed for processing.
/// This means that the most recent <see cref="ServiceBusReceiver.ReceiveMessageAsync"/> call timed out so there are currently no messages
/// available to be received for the session.
/// </summary>
[SuppressMessage("Usage", "AZC0003:DO make service methods virtual.",
Justification = "This member follows the standard .NET event pattern; override via the associated On<<EVENT>> method.")]
internal event Func<ProcessSessionEventArgs, Task> SessionClosingAsync
{
add
{
Argument.AssertNotNull(value, nameof(SessionClosingAsync));
if (_sessionClosingAsync != default)
{
throw new NotSupportedException(Resources.HandlerHasAlreadyBeenAssigned);
}
EnsureNotRunningAndInvoke(() => _sessionClosingAsync = value);
}
remove
{
Argument.AssertNotNull(value, nameof(SessionClosingAsync));
if (_sessionClosingAsync != value)
{
throw new ArgumentException(Resources.HandlerHasNotBeenAssigned);
}
EnsureNotRunningAndInvoke(() => _sessionClosingAsync = default);
}
}
/// <summary>
/// Invokes the process message event handler after a message has been received.
/// This method can be overridden to raise an event manually for testing purposes.
/// </summary>
/// <param name="args">The event args containing information related to the message.</param>
protected internal virtual async Task OnProcessMessageAsync(ProcessMessageEventArgs args)
{
try
{
await _processMessageAsync(args).ConfigureAwait(false);
}
finally
{
args.EndExecutionScope();
}
}
/// <summary>
/// Invokes the error event handler when an error has occurred during processing.
/// This method can be overridden to raise an event manually for testing purposes.
/// </summary>
/// <param name="args">The event args containing information related to the error.</param>
protected internal virtual async Task OnProcessErrorAsync(ProcessErrorEventArgs args)
{
await _processErrorAsync(args).ConfigureAwait(false);
}
internal async Task OnProcessSessionMessageAsync(ProcessSessionMessageEventArgs args)
{
try
{
await _processSessionMessageAsync(args).ConfigureAwait(false);
}
finally
{
args.EndExecutionScope();
}
}
internal async Task OnSessionInitializingAsync(ProcessSessionEventArgs args)
{
await _sessionInitializingAsync(args).ConfigureAwait(false);
}
internal async Task OnSessionClosingAsync(ProcessSessionEventArgs args)
{
await _sessionClosingAsync(args).ConfigureAwait(false);
}
/// <summary>
/// Signals the processor to begin processing messages. Should this method be
/// called while the processor is already running, an
/// <see cref="InvalidOperationException"/> is thrown.
/// </summary>
///
/// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to
/// signal the request to cancel the start operation. This won't affect the
/// processor once it starts running.
/// </param>
/// <exception cref="InvalidOperationException">
/// This can occur if the processor is already running. This can be checked via the <see cref="IsProcessing"/> property.
/// This can also occur if event handlers have not been specified for the <see cref="ProcessMessageAsync"/> or
/// the <see cref="ProcessErrorAsync"/> events.
/// </exception>
public virtual async Task StartProcessingAsync(
CancellationToken cancellationToken = default)
{
Argument.AssertNotDisposed(IsClosed, nameof(ServiceBusProcessor));
Connection.ThrowIfClosed();
cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
bool releaseGuard = false;
try
{
await _processingStartStopSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
releaseGuard = true;
if (ActiveReceiveTask == null)
{
Logger.StartProcessingStart(Identifier);
try
{
ValidateMessageHandler();
ValidateErrorHandler();
cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
// We expect the token source to be null, but we are playing safe.
RunningTaskTokenSource?.Cancel();
RunningTaskTokenSource?.Dispose();
RunningTaskTokenSource = new CancellationTokenSource();
// Start the main running task.
ActiveReceiveTask = RunReceiveTaskAsync(RunningTaskTokenSource.Token);
}
catch (Exception exception)
{
Logger.StartProcessingException(Identifier, exception.ToString());
throw;
}
Logger.StartProcessingComplete(Identifier);
}
else
{
throw new InvalidOperationException(Resources.RunningMessageProcessorCannotPerformOperation);
}
}
finally
{
if (releaseGuard)
{
_processingStartStopSemaphore.Release();
}
}
}
private void ReconcileReceiverManagers(int maxConcurrentSessions, int prefetchCount)
{
if (_receiverManagers.Count == 0)
{
if (IsSessionProcessor)
{
var numReceivers = _sessionIds.Length > 0 ? _sessionIds.Length : _maxConcurrentSessions;
for (int i = 0; i < numReceivers; i++)
{
var sessionId = _sessionIds.Length > 0 ? _sessionIds[i] : null;
_receiverManagers.Add(
new SessionReceiverManager(
_sessionProcessor,
sessionId,
_maxConcurrentAcceptSessionsSemaphore,
_clientDiagnostics,
KeepOpenOnReceiveTimeout));
}
}
else
{
_receiverManagers.Add(
new ReceiverManager(
this,
_clientDiagnostics,
false));
}
}
else
{
if (IsSessionProcessor && _sessionIds.Length == 0)
{
var diffSessions = maxConcurrentSessions - _currentConcurrentSessions;
if (diffSessions > 0)
{
for (int i = 0; i < diffSessions; i++)
{
_receiverManagers.Add(
new SessionReceiverManager(
_sessionProcessor,
null,
_maxConcurrentAcceptSessionsSemaphore,
_clientDiagnostics,
KeepOpenOnReceiveTimeout));
}
}
else
{
int diffSessionsLimit = Math.Abs(diffSessions);
for (int i = 0; i < diffSessionsLimit; i++)
{
// These should generally be closed as part of the normal bookkeeping in SessionReceiverManager,
// but we will track them so that they can be explicitly closed when stopping, just like we do with
// _receiverManagers.
_orphanedReceiverManagers.Add(_receiverManagers[0]);
// these tasks will be awaited when closing the orphaned receivers as part of CloseAsync
_ = _receiverManagers[0].CancelAsync();
_receiverManagers.RemoveAt(0);
}
}
}
int receiverManagers = _receiverManagers.Count;
for (int i = 0; i < receiverManagers; i++)
{
var receiverManager = _receiverManagers[i];
receiverManager.UpdatePrefetchCount(prefetchCount);
}
}
}
private void ValidateErrorHandler()
{
if (_processErrorAsync == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
Resources.CannotStartMessageProcessorWithoutHandler, nameof(ProcessErrorAsync)));
}
}
private void ValidateMessageHandler()
{
if (IsSessionProcessor)
{
if (_processSessionMessageAsync == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
Resources.CannotStartMessageProcessorWithoutHandler, nameof(ProcessMessageAsync)));
}
}
else if (_processMessageAsync == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
Resources.CannotStartMessageProcessorWithoutHandler, nameof(ProcessMessageAsync)));
}
}
/// <summary>
/// Signals the processor to stop processing messaging. Should this method be
/// called while the processor is not running, no action is taken. This method
/// will not close the underlying receivers, but will cause the receivers to stop
/// receiving. Any in-flight message handlers will be awaited, and this method will not return
/// until all in-flight message handlers have returned. To close the underlying receivers, <see cref="CloseAsync"/>
/// should be called. If <see cref="CloseAsync"/> is called, the processor cannot be restarted.
/// If you wish to resume processing at some point after calling this method, you can call
/// <see cref="StartProcessingAsync"/>.
/// </summary>
///
/// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to
/// signal the request to cancel the stop operation. If the operation is successfully
/// canceled, the processor will keep running.</param>
public virtual async Task StopProcessingAsync(CancellationToken cancellationToken = default)
{
bool releaseGuard = false;
try
{
await _processingStartStopSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
releaseGuard = true;
if (ActiveReceiveTask != null)
{
Logger.StopProcessingStart(Identifier);
cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
// Cancel the current running task. Catch any exception that are triggered due to customer token registrations, and
// log these as warnings as we don't want to forego stopping due to these exceptions.
try
{
RunningTaskTokenSource.Cancel();
}
catch (Exception exception)
{
Logger.ProcessorStoppingCancellationWarning(Identifier, exception.ToString());
}
RunningTaskTokenSource.Dispose();
RunningTaskTokenSource = null;
// Now that a cancellation request has been issued, wait for the running task to finish. In case something
// unexpected happened and it stopped working midway, this is the moment we expect to catch an exception.
try
{
await ActiveReceiveTask.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// Nothing to do here. These exceptions are expected.
}
finally
{
// If an unexpected exception occurred while awaiting the receive task, we still want to dispose and set to null
// as the task is complete and there is no use in awaiting it again if StopProcessingAsync is called again.
ActiveReceiveTask.Dispose();
ActiveReceiveTask = null;
}
}
}
catch (Exception exception)
{
Logger.StopProcessingException(Identifier, exception.ToString());
throw;
}
finally
{
if (releaseGuard)
{
_processingStartStopSemaphore.Release();
}
}
Logger.StopProcessingComplete(Identifier);
}
/// <summary>
/// Runs the Receive task in as many threads as are
/// specified in the <see cref="MaxConcurrentCalls"/> property.
/// </summary>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel the operation.</param>
private async Task RunReceiveTaskAsync(CancellationToken cancellationToken)
{
CancellationTokenSource linkedHandlerTcs = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _handlerCts.Token);
try
{
while (!cancellationToken.IsCancellationRequested && !Connection.IsClosed)
{
await ReconcileConcurrencyAsync().ConfigureAwait(false);
foreach (ReceiverManager receiverManager in _receiverManagers)
{
// reset the linkedHandlerTcs if it was already cancelled due to user updating the concurrency
// do this before the synchronous Wait call as that does not respect the TCS.
if (linkedHandlerTcs.IsCancellationRequested)
{
linkedHandlerTcs.Dispose();
linkedHandlerTcs = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _handlerCts.Token);
break;
}
// Do a quick synchronous check before we resort to async/await with the state-machine overhead.
if (!_messageHandlerSemaphore.Wait(0, CancellationToken.None))
{
try
{
await _messageHandlerSemaphore.WaitAsync(linkedHandlerTcs.Token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
linkedHandlerTcs.Dispose();
// reset the linkedHandlerTcs if it was already cancelled due to user updating the concurrency
linkedHandlerTcs = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _handlerCts.Token);
// allow the loop to wake up when tcs is signaled
break;
}
}
// hold onto all the tasks that we are starting so that when cancellation is requested,
// we can await them to make sure we surface any unexpected exceptions, i.e. exceptions
// other than TaskCanceledExceptions
// Instead of using the array overload which allocates an array, we use the overload that has two parameters
// and pass in CancellationToken.None. This should be safe since CanBeCanceled will return false.
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, CancellationToken.None);
TaskTuples.Add(
(
ReceiveAndProcessMessagesAsync(receiverManager, linkedCts.Token),
linkedCts)
);
if (TaskTuples.Count > Options.MaxConcurrentCalls)
{
List<(Task Task, CancellationTokenSource Cts)> remaining = new();
foreach (var tuple in TaskTuples)
{
if (tuple.Task.IsCompleted)
{
tuple.Cts.Dispose();
}
else
{
remaining.Add(tuple);
}
}
TaskTuples = remaining;
}
}
}
// If the main loop is aborting due to the connection being canceled, then
// force the processor to stop.
if (!cancellationToken.IsCancellationRequested && Connection.IsClosed)
{
Logger.ProcessorClientClosedException(Identifier);
// Because this is a highly unusual situation
// and goes against the goal of resiliency for the processor, surface
// a fatal exception with an explicit message detailing that processing
// will be stopped.
try
{
await OnProcessErrorAsync(
new ProcessErrorEventArgs(
new ObjectDisposedException(nameof(ServiceBusConnection),
Resources.DisposedConnectionMessageProcessorMustStop),
ServiceBusErrorSource.Receive,
FullyQualifiedNamespace,
EntityPath,
Identifier,
cancellationToken))
.ConfigureAwait(false);
}
catch (Exception ex)
{
// Don't bubble up exceptions raised from customer exception handler.
Logger.ProcessorErrorHandlerThrewException(ex.ToString(), Identifier);
}
// This call will deadlock if awaited, as StopProcessingAsync awaits
// completion of this task. The processor is already known to be in a bad
// state and exceptions in StopProcessingAsync are likely and will be logged
// in that call. There is little value in surfacing those expected exceptions
// to the customer error handler as well; allow StopProcessingAsync to run
// in a fire-and-forget manner.
_ = StopProcessingAsync(CancellationToken.None);
}
}
finally
{
try
{
// await all tasks using WhenAll so that we ensure every task finishes even if there are exceptions
// (rather than try/catch each await)
await Task.WhenAll(TaskTuples.Select(t => t.Task)).ConfigureAwait(false);
}
finally
{
foreach (var (_, cts) in TaskTuples)
{
cts.Dispose();
}
TaskTuples.Clear();
linkedHandlerTcs.Dispose();
}
}
}
private async Task ReceiveAndProcessMessagesAsync(
ReceiverManager receiverManager,
CancellationToken cancellationToken)
{
try
{
await receiverManager.ReceiveAndProcessMessagesAsync(cancellationToken).ConfigureAwait(false);
}
finally
{
_messageHandlerSemaphore.Release();
}
}
/// <summary>
/// Invokes a specified action only if this <see cref="ServiceBusProcessor" /> instance is not running.
/// </summary>
/// <param name="action">The action to invoke.</param>
/// <exception cref="InvalidOperationException">Method is invoked while the event processor is running.</exception>
internal void EnsureNotRunningAndInvoke(Action action)
{
if (ActiveReceiveTask == null)
{
try
{
_processingStartStopSemaphore.Wait();
if (ActiveReceiveTask == null)
{
action?.Invoke();
}
else
{
throw new InvalidOperationException(Resources.RunningMessageProcessorCannotPerformOperation);
}
}
finally
{
_processingStartStopSemaphore.Release();
}
}
else
{
throw new InvalidOperationException(Resources.RunningMessageProcessorCannotPerformOperation);
}
}
/// <summary>
/// Performs the task needed to clean up resources used by the <see cref="ServiceBusProcessor" />. Any in-flight message handlers will
/// be awaited. Once all message handling has completed, the underlying links will be closed. After this point, the method will return.
/// This is equivalent to calling <see cref="DisposeAsync"/>.
/// </summary>
/// <param name="cancellationToken"> An optional<see cref="CancellationToken"/> instance to signal the
/// request to cancel the operation.</param>
public virtual async Task CloseAsync(
CancellationToken cancellationToken = default)
{
IsClosed = true;