| | | 1 | | using StackExchange.Redis; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Channels.Redis; |
| | | 4 | | |
| | | 5 | | /// <summary>A live pub/sub subscription; disposing it unsubscribes.</summary> |
| | | 6 | | internal interface IRedisChannelSubscription : IAsyncDisposable; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Async-capable subscribe seam over StackExchange.Redis pub/sub. The channel consumes this instead |
| | | 10 | | /// of <see cref="ISubscriber.Subscribe(RedisChannel, Action{RedisChannel, RedisValue}, CommandFlags)"/>'s |
| | | 11 | | /// synchronous callback — whose handlers the SDK runs on pool threads with no ordering — so |
| | | 12 | | /// messages reach the channel one at a time, in order, off any Redis reader thread. Also the |
| | | 13 | | /// unit-test seam: <see cref="ChannelMessageQueue"/> is sealed with no public constructor, so |
| | | 14 | | /// tests fake this interface rather than the queue. |
| | | 15 | | /// <para> |
| | | 16 | | /// The handler must not wait for downstream capacity: the SDK queue behind this seam is |
| | | 17 | | /// unbounded, so a handler parked on admission does not backpressure the publisher (Redis |
| | | 18 | | /// pub/sub has none), it only lets that queue grow. The channel admits non-blockingly and faults |
| | | 19 | | /// the wait as indeterminate when its bounded buffer is full. |
| | | 20 | | /// </para> |
| | | 21 | | /// </summary> |
| | | 22 | | internal interface IRedisChannelSubscriber |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Subscribes to <paramref name="channel"/>, invoking <paramref name="onMessage"/> for each |
| | | 26 | | /// message sequentially (a message's task is awaited before the next is delivered). |
| | | 27 | | /// </summary> |
| | | 28 | | Task<IRedisChannelSubscription> SubscribeAsync(RedisChannel channel, Func<RedisChannel, RedisValue, Task> onMessage) |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Production <see cref="IRedisChannelSubscriber"/> over <see cref="ISubscriber"/>: a |
| | | 33 | | /// <see cref="ChannelMessageQueue"/> per subscription, whose <c>OnMessage(Func<…, Task>)</c> |
| | | 34 | | /// loop awaits the handler — preserving per-channel ordering off the reader thread. The queue |
| | | 35 | | /// itself is unbounded (an SDK detail), which is why the channel's handler never waits in it. |
| | | 36 | | /// </summary> |
| | 438 | 37 | | internal sealed class RedisChannelMessageQueueSubscriber(ISubscriber _subscriber) : IRedisChannelSubscriber |
| | | 38 | | { |
| | | 39 | | /// <summary>Runs the SubscribeAsync operation.</summary> |
| | | 40 | | public async Task<IRedisChannelSubscription> SubscribeAsync(RedisChannel channel, Func<RedisChannel, RedisValue, Tas |
| | | 41 | | { |
| | 363 | 42 | | var queue = await _subscriber.SubscribeAsync(channel).ConfigureAwait(false); |
| | 835 | 43 | | queue.OnMessage(message => onMessage(message.Channel, message.Message)); |
| | 363 | 44 | | return new Subscription(queue); |
| | 363 | 45 | | } |
| | | 46 | | |
| | 363 | 47 | | private sealed class Subscription(ChannelMessageQueue _queue) : IRedisChannelSubscription |
| | | 48 | | { |
| | | 49 | | /// <summary>Releases resources held by this instance.</summary> |
| | 363 | 50 | | public ValueTask DisposeAsync() => new(_queue.UnsubscribeAsync()); |
| | | 51 | | } |
| | | 52 | | } |