/
ChannelMessageQueue.cs
380 lines (338 loc) · 13.7 KB
/
ChannelMessageQueue.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
#if NETCOREAPP3_1
using System.Reflection;
#endif
namespace StackExchange.Redis
{
/// <summary>
/// Represents a message that is broadcast via publish/subscribe.
/// </summary>
public readonly struct ChannelMessage
{
// this is *smaller* than storing a RedisChannel for the subscribed channel
private readonly ChannelMessageQueue _queue;
/// <summary>
/// The Channel:Message string representation.
/// </summary>
public override string ToString() => ((string?)Channel) + ":" + ((string?)Message);
/// <inheritdoc/>
public override int GetHashCode() => Channel.GetHashCode() ^ Message.GetHashCode();
/// <inheritdoc/>
public override bool Equals(object? obj) => obj is ChannelMessage cm
&& cm.Channel == Channel && cm.Message == Message;
internal ChannelMessage(ChannelMessageQueue queue, in RedisChannel channel, in RedisValue value)
{
_queue = queue;
Channel = channel;
Message = value;
}
/// <summary>
/// The channel that the subscription was created from.
/// </summary>
public RedisChannel SubscriptionChannel => _queue.Channel;
/// <summary>
/// The channel that the message was broadcast to.
/// </summary>
public RedisChannel Channel { get; }
/// <summary>
/// The value that was broadcast.
/// </summary>
public RedisValue Message { get; }
/// <summary>
/// Checks if 2 messages are .Equal()
/// </summary>
public static bool operator ==(ChannelMessage left, ChannelMessage right) => left.Equals(right);
/// <summary>
/// Checks if 2 messages are not .Equal()
/// </summary>
public static bool operator !=(ChannelMessage left, ChannelMessage right) => !left.Equals(right);
}
/// <summary>
/// Represents a message queue of ordered pub/sub notifications.
/// </summary>
/// <remarks>
/// To create a ChannelMessageQueue, use <see cref="ISubscriber.Subscribe(RedisChannel, CommandFlags)"/>
/// or <see cref="ISubscriber.SubscribeAsync(RedisChannel, CommandFlags)"/>.
/// </remarks>
public sealed class ChannelMessageQueue : IAsyncEnumerable<ChannelMessage>
{
private readonly Channel<ChannelMessage> _queue;
/// <summary>
/// The Channel that was subscribed for this queue.
/// </summary>
public RedisChannel Channel { get; }
private RedisSubscriber? _parent;
/// <summary>
/// The string representation of this channel.
/// </summary>
public override string? ToString() => (string?)Channel;
/// <summary>
/// An awaitable task the indicates completion of the queue (including drain of data).
/// </summary>
public Task Completion => _queue.Reader.Completion;
internal ChannelMessageQueue(in RedisChannel redisChannel, RedisSubscriber parent)
{
Channel = redisChannel;
_parent = parent;
_queue = System.Threading.Channels.Channel.CreateUnbounded<ChannelMessage>(s_ChannelOptions);
}
private static readonly UnboundedChannelOptions s_ChannelOptions = new UnboundedChannelOptions
{
SingleWriter = true,
SingleReader = false,
AllowSynchronousContinuations = false,
};
private void Write(in RedisChannel channel, in RedisValue value)
{
var writer = _queue.Writer;
writer.TryWrite(new ChannelMessage(this, channel, value));
}
/// <summary>
/// Consume a message from the channel.
/// </summary>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
public ValueTask<ChannelMessage> ReadAsync(CancellationToken cancellationToken = default)
=> _queue.Reader.ReadAsync(cancellationToken);
/// <summary>
/// Attempt to synchronously consume a message from the channel.
/// </summary>
/// <param name="item">The <see cref="ChannelMessage"/> read from the Channel.</param>
public bool TryRead(out ChannelMessage item) => _queue.Reader.TryRead(out item);
/// <summary>
/// Attempt to query the backlog length of the queue.
/// </summary>
/// <param name="count">The (approximate) count of items in the Channel.</param>
public bool TryGetCount(out int count)
{
// This is specific to netcoreapp3.1, because full framework was out of band and the new prop is present
#if NETCOREAPP3_1
// get this using the reflection
try
{
var prop = _queue.GetType().GetProperty("ItemsCountForDebugger", BindingFlags.Instance | BindingFlags.NonPublic);
if (prop is not null)
{
count = (int)prop.GetValue(_queue)!;
return true;
}
}
catch { }
#else
var reader = _queue.Reader;
if (reader.CanCount)
{
count = reader.Count;
return true;
}
#endif
count = default;
return false;
}
private Delegate? _onMessageHandler;
private void AssertOnMessage(Delegate handler)
{
if (handler == null) throw new ArgumentNullException(nameof(handler));
if (Interlocked.CompareExchange(ref _onMessageHandler, handler, null) != null)
throw new InvalidOperationException("Only a single " + nameof(OnMessage) + " is allowed");
}
/// <summary>
/// Create a message loop that processes messages sequentially.
/// </summary>
/// <param name="handler">The handler to run when receiving a message.</param>
public void OnMessage(Action<ChannelMessage> handler)
{
AssertOnMessage(handler);
ThreadPool.QueueUserWorkItem(
state => ((ChannelMessageQueue)state!).OnMessageSyncImpl().RedisFireAndForget(), this);
}
private async Task OnMessageSyncImpl()
{
var handler = (Action<ChannelMessage>?)_onMessageHandler;
while (!Completion.IsCompleted)
{
ChannelMessage next;
try { if (!TryRead(out next)) next = await ReadAsync().ForAwait(); }
catch (ChannelClosedException) { break; } // expected
catch (Exception ex)
{
_parent?.multiplexer?.OnInternalError(ex);
break;
}
try { handler?.Invoke(next); }
catch { } // matches MessageCompletable
}
}
internal static void Combine(ref ChannelMessageQueue? head, ChannelMessageQueue queue)
{
if (queue != null)
{
// insert at the start of the linked-list
ChannelMessageQueue? old;
do
{
old = Volatile.Read(ref head);
queue._next = old;
} while (Interlocked.CompareExchange(ref head, queue, old) != old);
}
}
/// <summary>
/// Create a message loop that processes messages sequentially.
/// </summary>
/// <param name="handler">The handler to execute when receiving a message.</param>
public void OnMessage(Func<ChannelMessage, Task> handler)
{
AssertOnMessage(handler);
ThreadPool.QueueUserWorkItem(
state => ((ChannelMessageQueue)state!).OnMessageAsyncImpl().RedisFireAndForget(), this);
}
internal static void Remove(ref ChannelMessageQueue? head, ChannelMessageQueue queue)
{
if (queue is null)
{
return;
}
bool found;
do // if we fail due to a conflict, re-do from start
{
var current = Volatile.Read(ref head);
if (current == null) return; // no queue? nothing to do
if (current == queue)
{
found = true;
// found at the head - then we need to change the head
if (Interlocked.CompareExchange(ref head, Volatile.Read(ref current._next), current) == current)
{
return; // success
}
}
else
{
ChannelMessageQueue? previous = current;
current = Volatile.Read(ref previous._next);
found = false;
do
{
if (current == queue)
{
found = true;
// found it, not at the head; remove the node
if (Interlocked.CompareExchange(ref previous._next, Volatile.Read(ref current._next), current) == current)
{
return; // success
}
else
{
break; // exit the inner loop, and repeat the outer loop
}
}
previous = current;
current = Volatile.Read(ref previous!._next);
} while (current != null);
}
} while (found);
}
internal static int Count(ref ChannelMessageQueue? head)
{
var current = Volatile.Read(ref head);
int count = 0;
while (current != null)
{
count++;
current = Volatile.Read(ref current._next);
}
return count;
}
internal static void WriteAll(ref ChannelMessageQueue head, in RedisChannel channel, in RedisValue message)
{
var current = Volatile.Read(ref head);
while (current != null)
{
current.Write(channel, message);
current = Volatile.Read(ref current._next);
}
}
private ChannelMessageQueue? _next;
private async Task OnMessageAsyncImpl()
{
var handler = (Func<ChannelMessage, Task>?)_onMessageHandler;
while (!Completion.IsCompleted)
{
ChannelMessage next;
try { if (!TryRead(out next)) next = await ReadAsync().ForAwait(); }
catch (ChannelClosedException) { break; } // expected
catch (Exception ex)
{
_parent?.multiplexer?.OnInternalError(ex);
break;
}
try
{
var task = handler?.Invoke(next);
if (task != null && task.Status != TaskStatus.RanToCompletion) await task.ForAwait();
}
catch { } // matches MessageCompletable
}
}
internal static void MarkAllCompleted(ref ChannelMessageQueue? head)
{
var current = Interlocked.Exchange(ref head, null);
while (current != null)
{
current.MarkCompleted();
current = Volatile.Read(ref current._next);
}
}
private void MarkCompleted(Exception? error = null)
{
_parent = null;
_queue.Writer.TryComplete(error);
}
internal void UnsubscribeImpl(Exception? error = null, CommandFlags flags = CommandFlags.None)
{
var parent = _parent;
_parent = null;
parent?.UnsubscribeAsync(Channel, null, this, flags);
_queue.Writer.TryComplete(error);
}
internal async Task UnsubscribeAsyncImpl(Exception? error = null, CommandFlags flags = CommandFlags.None)
{
var parent = _parent;
_parent = null;
if (parent != null)
{
await parent.UnsubscribeAsync(Channel, null, this, flags).ForAwait();
}
_queue.Writer.TryComplete(error);
}
/// <summary>
/// Stop receiving messages on this channel.
/// </summary>
/// <param name="flags">The flags to use when unsubscribing.</param>
public void Unsubscribe(CommandFlags flags = CommandFlags.None) => UnsubscribeImpl(null, flags);
/// <summary>
/// Stop receiving messages on this channel.
/// </summary>
/// <param name="flags">The flags to use when unsubscribing.</param>
public Task UnsubscribeAsync(CommandFlags flags = CommandFlags.None) => UnsubscribeAsyncImpl(null, flags);
/// <inheritdoc cref="IAsyncEnumerable{ChannelMessage}.GetAsyncEnumerator(CancellationToken)"/>
#if NETCOREAPP3_0_OR_GREATER
public IAsyncEnumerator<ChannelMessage> GetAsyncEnumerator(CancellationToken cancellationToken = default)
=> _queue.Reader.ReadAllAsync().GetAsyncEnumerator(cancellationToken);
#else
public async IAsyncEnumerator<ChannelMessage> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
while (await _queue.Reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false))
{
while (_queue.Reader.TryRead(out var item))
{
yield return item;
}
}
}
#endif
}
}