-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventMessagePushWithTopics.cs
510 lines (429 loc) · 19.9 KB
/
EventMessagePushWithTopics.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using DataCore.Adapter.Common;
using IntelligentPlant.BackgroundTasks;
using Microsoft.Extensions.Logging;
namespace DataCore.Adapter.Events {
/// <summary>
/// Implements <see cref="IEventMessagePushWithTopics"/>.
/// </summary>
public class EventMessagePushWithTopics : SubscriptionManager<EventMessagePushWithTopicsOptions, string, EventMessage, EventSubscriptionChannel>, IEventMessagePushWithTopics {
/// <summary>
/// Flags if the object has been disposed.
/// </summary>
private bool _isDisposed;
/// <summary>
/// Channel that is used to publish changes to subscribed topics.
/// </summary>
private readonly Channel<(List<string> Topics, bool Added, TaskCompletionSource<bool> Processed)> _topicSubscriptionChangesChannel = Channel.CreateUnbounded<(List<string>, bool, TaskCompletionSource<bool>)>(new UnboundedChannelOptions() {
AllowSynchronousContinuations = false,
SingleReader = true,
SingleWriter = true
});
/// <summary>
/// Maps from topic name to the subscriber count for that topic.
/// </summary>
private readonly Dictionary<string, int> _subscriberCount = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Lock for performing subscription modifications.
/// </summary>
private readonly Nito.AsyncEx.AsyncReaderWriterLock _subscriptionLock = new Nito.AsyncEx.AsyncReaderWriterLock();
/// <summary>
/// Indicates if the subscription manager holds any active subscriptions. If your adapter uses
/// a forward-only cursor that you do not want to advance when only passive listeners are
/// attached to the adapter, you can use this property to identify if any active listeners are
/// attached.
/// </summary>
protected bool HasActiveSubscriptions { get; private set; }
/// <summary>
/// Creates a new <see cref="EventMessagePush"/> object.
/// </summary>
/// <param name="options">
/// The feature options.
/// </param>
/// <param name="backgroundTaskService">
/// The <see cref="IBackgroundTaskService"/> to use when running background tasks.
/// </param>
/// <param name="logger">
/// The logger to use.
/// </param>
public EventMessagePushWithTopics(EventMessagePushWithTopicsOptions? options, IBackgroundTaskService? backgroundTaskService, ILogger? logger)
: base(options, backgroundTaskService, logger) {
BackgroundTaskService.QueueBackgroundWorkItem(
ProcessTopicSubscriptionChangesChannel,
DisposedToken
);
}
/// <inheritdoc/>
public async IAsyncEnumerable<EventMessage> Subscribe(
IAdapterCallContext context,
CreateEventMessageTopicSubscriptionRequest request,
IAsyncEnumerable<EventMessageSubscriptionUpdate> channel,
[EnumeratorCancellation]
CancellationToken cancellationToken
) {
if (_isDisposed) {
throw new ObjectDisposedException(GetType().FullName);
}
if (context == null) {
throw new ArgumentNullException(nameof(context));
}
if (request == null) {
throw new ArgumentNullException(nameof(request));
}
ValidationExtensions.ValidateObject(request);
if (channel == null) {
throw new ArgumentNullException(nameof(channel));
}
using (var ctSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, DisposedToken)) {
var subscription = await CreateSubscriptionAsync<IEventMessagePushWithTopics>(context, nameof(Subscribe), request, ctSource.Token).ConfigureAwait(false);
if (request.Topics != null && request.Topics.Any()) {
await OnTopicsAddedToSubscriptionInternalAsync(subscription, request.Topics, ctSource.Token).ConfigureAwait(false);
}
BackgroundTaskService.QueueBackgroundWorkItem(
ct => RunSubscriptionChangesListener(subscription, channel, ct),
null,
true,
subscription.CancellationToken,
ctSource.Token
);
await foreach (var item in subscription.ReadAllAsync(ctSource.Token).ConfigureAwait(false)) {
yield return item;
}
}
}
/// <inheritdoc/>
protected override EventSubscriptionChannel CreateSubscriptionChannel(
IAdapterCallContext context,
int id,
int channelCapacity,
CancellationToken[] cancellationTokens,
Func<ValueTask> cleanup,
object? state
) {
var request = (CreateEventMessageTopicSubscriptionRequest) state!;
return new EventSubscriptionChannel(
id,
context,
BackgroundTaskService,
request?.SubscriptionType ?? EventMessageSubscriptionType.Active,
TimeSpan.Zero,
cancellationTokens,
cleanup,
channelCapacity
);
}
/// <inheritdoc/>
protected override async ValueTask OnSubscriptionAddedAsync(EventSubscriptionChannel subscription, CancellationToken cancellationToken) {
await base.OnSubscriptionAddedAsync(subscription, cancellationToken).ConfigureAwait(false);
HasActiveSubscriptions = HasSubscriptions && GetSubscriptions().Any(x => x.SubscriptionType == EventMessageSubscriptionType.Active);
}
/// <inheritdoc/>
protected override async ValueTask OnSubscriptionCancelledAsync(EventSubscriptionChannel subscription, CancellationToken cancellationToken) {
await base.OnSubscriptionCancelledAsync(subscription, cancellationToken).ConfigureAwait(false);
HasActiveSubscriptions = HasSubscriptions && GetSubscriptions().Any(x => x.SubscriptionType == EventMessageSubscriptionType.Active);
if (subscription != null) {
await OnTopicsRemovedFromSubscriptionInternalAsync(subscription, subscription.Topics, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Gets the composite set of topics that are currently being subscribed to by all
/// subscribers.
/// </summary>
/// <returns>
/// The subscribed topics.
/// </returns>
public IEnumerable<string> GetSubscribedTopics() {
using (_subscriptionLock.ReaderLock()) {
return _subscriberCount.Keys.ToArray();
}
}
/// <inheritdoc/>
protected override async ValueTask<bool> IsTopicMatch(EventMessage value, IEnumerable<string> topics, CancellationToken cancellationToken) {
if (value?.Topic == null) {
return false;
}
// If a custom delegate has been specified, defer to that.
if (Options.IsTopicMatch != null) {
foreach (var topic in topics) {
if (await Options.IsTopicMatch(topic, value.Topic, cancellationToken).ConfigureAwait(false)) {
return true;
}
}
return false;
}
if (topics.Any(x => string.Equals(value.Topic, x, StringComparison.Ordinal))) {
return true;
}
return false;
}
/// <inheritdoc/>
protected override IDictionary<string, string> GetHealthCheckProperties(IAdapterCallContext context) {
var result = base.GetHealthCheckProperties(context);
var subscriptions = GetSubscriptions();
result[Resources.HealthChecks_Data_ActiveSubscriberCount] = subscriptions.Count(x => x.SubscriptionType == EventMessageSubscriptionType.Active).ToString(context?.CultureInfo);
result[Resources.HealthChecks_Data_PassiveSubscriberCount] = subscriptions.Count(x => x.SubscriptionType == EventMessageSubscriptionType.Passive).ToString(context?.CultureInfo);
return result;
}
/// <summary>
/// Called when the number of subscribers for a topic changes from zero to one.
/// </summary>
/// <param name="topics">
/// The topics that were added.
/// </param>
/// <param name="cancellationToken">
/// The cancellation token for the operation.
/// </param>
/// <returns>
/// A task that will process the change.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="topics"/> is <see langword="null"/>.
/// </exception>
protected virtual Task OnTopicsAdded(IEnumerable<string> topics, CancellationToken cancellationToken) {
if (topics == null) {
throw new ArgumentNullException(nameof(topics));
}
return Options.OnTopicSubscriptionsAdded == null
? Task.CompletedTask
: Options.OnTopicSubscriptionsAdded.Invoke(this, topics, cancellationToken);
}
/// <summary>
/// Called when the number of subscribers for a topic changes from one to zero.
/// </summary>
/// <param name="topics">
/// The topics that were removed.
/// </param>
/// <param name="cancellationToken">
/// The cancellation token for the operation.
/// </param>
/// <returns>
/// A task that will process the change.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="topics"/> is <see langword="null"/>.
/// </exception>
protected virtual Task OnTopicsRemoved(IEnumerable<string> topics, CancellationToken cancellationToken) {
if (topics == null) {
throw new ArgumentNullException(nameof(topics));
}
return Options.OnTopicSubscriptionsRemoved == null
? Task.CompletedTask
: Options.OnTopicSubscriptionsRemoved.Invoke(this, topics, cancellationToken);
}
/// <summary>
/// Runs a long-running task that will process changes on a subscription.
/// </summary>
/// <param name="subscription">
/// The subscription.
/// </param>
/// <param name="channel">
/// The subscription changes channel.
/// </param>
/// <param name="cancellationToken">
/// The cancellation token for the operation.
/// </param>
/// <returns>
/// The long-running task.
/// </returns>
private async Task RunSubscriptionChangesListener(EventSubscriptionChannel subscription, IAsyncEnumerable<EventMessageSubscriptionUpdate> channel, CancellationToken cancellationToken) {
await foreach (var item in channel.WithCancellation(cancellationToken).ConfigureAwait(false)) {
cancellationToken.ThrowIfCancellationRequested();
if (item?.Topics == null) {
continue;
}
var topics = item.Topics.Where(x => x != null).ToArray();
if (topics.Length == 0) {
continue;
}
if (item.Action == SubscriptionUpdateAction.Subscribe) {
await OnTopicsAddedToSubscriptionInternalAsync(subscription, topics, cancellationToken).ConfigureAwait(false);
}
else {
await OnTopicsRemovedFromSubscriptionInternalAsync(subscription, topics, cancellationToken).ConfigureAwait(false);
}
}
}
/// <summary>
/// Called when topics are added to a subscription.
/// </summary>
/// <param name="subscription">
/// The subscription.
/// </param>
/// <param name="topics">
/// The subscription topics
/// </param>
/// <param name="cancellationToken">
/// The cancellation token for the operation.
/// </param>
/// <returns>
/// A task that will process the operation.
/// </returns>
private async Task OnTopicsAddedToSubscriptionInternalAsync(EventSubscriptionChannel subscription, IEnumerable<string> topics, CancellationToken cancellationToken) {
TaskCompletionSource<bool> processed = null!;
subscription.AddTopics(topics);
using (await _subscriptionLock.WriterLockAsync(cancellationToken).ConfigureAwait(false)) {
var newSubscriptions = new List<string>();
foreach (var topic in topics) {
if (cancellationToken.IsCancellationRequested) {
break;
}
if (topic == null) {
continue;
}
if (!_subscriberCount.TryGetValue(topic, out var subscriberCount)) {
subscriberCount = 0;
newSubscriptions.Add(topic);
}
_subscriberCount[topic] = ++subscriberCount;
}
if (newSubscriptions.Count > 0) {
processed = new TaskCompletionSource<bool>();
_topicSubscriptionChangesChannel.Writer.TryWrite((newSubscriptions, true, processed));
}
}
if (processed == null) {
return;
}
// Wait for last change to be processed.
await processed.Task.WithCancellation(cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Called when topics are removed from a subscription.
/// </summary>
/// <param name="subscription">
/// The subscription.
/// </param>
/// <param name="topics">.
/// The subscription topics
/// </param>
/// <param name="cancellationToken">
/// The cancellation token for the operation.
/// </param>
/// <returns>
/// A task that will process the operation.
/// </returns>
private async Task OnTopicsRemovedFromSubscriptionInternalAsync(EventSubscriptionChannel subscription, IEnumerable<string> topics, CancellationToken cancellationToken) {
using (await _subscriptionLock.WriterLockAsync(cancellationToken).ConfigureAwait(false)) {
var removedSubscriptions = new List<string>();
foreach (var topic in topics) {
if (topic == null || !subscription.RemoveTopic(topic)) {
continue;
}
if (!_subscriberCount.TryGetValue(topic, out var subscriberCount)) {
continue;
}
--subscriberCount;
if (subscriberCount == 0) {
_subscriberCount.Remove(topic);
removedSubscriptions.Add(topic);
}
else {
_subscriberCount[topic] = subscriberCount;
}
}
if (removedSubscriptions.Count > 0) {
_topicSubscriptionChangesChannel.Writer.TryWrite((removedSubscriptions, false, null!));
}
}
}
/// <summary>
/// Starts a long-running that that will read and process subscription changes published
/// to <see cref="_topicSubscriptionChangesChannel"/>.
/// </summary>
/// <param name="cancellationToken">
/// A cancellation token that will fire when the task should exit.
/// </param>
/// <returns>
/// A long-running task.
/// </returns>
private async Task ProcessTopicSubscriptionChangesChannel(CancellationToken cancellationToken) {
while (!cancellationToken.IsCancellationRequested) {
try {
if (!await _topicSubscriptionChangesChannel.Reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false)) {
break;
}
}
catch (OperationCanceledException) {
break;
}
catch (ChannelClosedException) {
break;
}
while (_topicSubscriptionChangesChannel.Reader.TryRead(out var change)) {
if (cancellationToken.IsCancellationRequested) {
break;
}
try {
if (change.Added) {
await OnTopicsAdded(change.Topics, cancellationToken).ConfigureAwait(false);
}
else {
await OnTopicsRemoved(change.Topics, cancellationToken).ConfigureAwait(false);
}
if (change.Processed != null) {
change.Processed.TrySetResult(true);
}
}
catch (Exception e) {
if (change.Processed != null) {
change.Processed.TrySetException(e);
}
Logger.LogError(
e,
Resources.Log_ErrorWhileProcessingSubscriptionTopicChange,
change.Topics.Count,
change.Added
? SubscriptionUpdateAction.Subscribe
: SubscriptionUpdateAction.Unsubscribe
);
}
}
}
}
/// <inheritdoc/>
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (_isDisposed) {
return;
}
if (disposing) {
_topicSubscriptionChangesChannel.Writer.TryComplete();
using (_subscriptionLock.WriterLock()) {
_subscriberCount.Clear();
}
}
_isDisposed = true;
}
}
/// <summary>
/// Options for <see cref="EventMessagePushWithTopics"/>.
/// </summary>
public class EventMessagePushWithTopicsOptions : SubscriptionManagerOptions {
/// <summary>
/// A delegate that is invoked when the number of subscribers for a topic changes from zero
/// to one.
/// </summary>
public Func<EventMessagePushWithTopics, IEnumerable<string>, CancellationToken, Task>? OnTopicSubscriptionsAdded { get; set; }
/// <summary>
/// A delegate that is invoked when the number of subscribers for a topic changes from one
/// to zero.
/// </summary>
public Func<EventMessagePushWithTopics, IEnumerable<string>, CancellationToken, Task>? OnTopicSubscriptionsRemoved { get; set; }
/// <summary>
/// A delegate that is invoked to determine if a topic for a subscription matches the
/// topic for a received event message.
/// </summary>
/// <remarks>
/// The first parameter passed to the delegate is the subscription topic, and the second
/// parameter is the topic for the received event message.
/// </remarks>
public Func<string, string?, CancellationToken, ValueTask<bool>>? IsTopicMatch { get; set; }
}
}