|
| 1 | +using System.Collections.Concurrent; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using WeihanLi.Common.Helpers; |
| 4 | + |
| 5 | +namespace WeihanLi.Common.Event; |
| 6 | + |
| 7 | +public sealed class AckQueueOptions |
| 8 | +{ |
| 9 | + public TimeSpan AckTimeout { get; set; } = TimeSpan.FromMinutes(1); |
| 10 | + |
| 11 | + public bool AutoRequeue { get; set; } |
| 12 | + |
| 13 | + public TimeSpan RequeuePeriod { get; set; } = TimeSpan.FromMinutes(1); |
| 14 | +} |
| 15 | + |
| 16 | +public sealed class AckQueue : DisposableBase |
| 17 | +{ |
| 18 | + private readonly AckQueueOptions _options; |
| 19 | + private readonly ConcurrentQueue<IEvent> _queue = new(); |
| 20 | + private readonly ConcurrentDictionary<string, IEvent> _unAckedMessages = new(); |
| 21 | + private readonly Timer? _timer; |
| 22 | + |
| 23 | + public AckQueue() : this(new()) { } |
| 24 | + |
| 25 | + public AckQueue(AckQueueOptions options) |
| 26 | + { |
| 27 | + _options = options; |
| 28 | + if (options.AutoRequeue) |
| 29 | + { |
| 30 | + _timer = new Timer(_ => RequeueUnAckedMessages(), null, options.RequeuePeriod, options.RequeuePeriod); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public Task EnqueueAsync<TEvent>(TEvent @event, EventProperties? properties = null) |
| 35 | + { |
| 36 | + properties ??= new EventProperties(); |
| 37 | + if (string.IsNullOrEmpty(properties.EventId)) |
| 38 | + { |
| 39 | + properties.EventId = Guid.NewGuid().ToString(); |
| 40 | + } |
| 41 | + |
| 42 | + if (properties.EventAt == default) |
| 43 | + { |
| 44 | + properties.EventAt = DateTimeOffset.Now; |
| 45 | + } |
| 46 | + |
| 47 | + var internalEvent = new EventWrapper<TEvent> |
| 48 | + { |
| 49 | + Data = @event, |
| 50 | + Properties = properties |
| 51 | + }; |
| 52 | + |
| 53 | + _queue.Enqueue(internalEvent); |
| 54 | + return Task.CompletedTask; |
| 55 | + } |
| 56 | + |
| 57 | + public Task<IEvent<TEvent>?> DequeueAsync<TEvent>() |
| 58 | + { |
| 59 | + if (_queue.TryDequeue(out var eventWrapper)) |
| 60 | + { |
| 61 | + _unAckedMessages.TryAdd(eventWrapper.Properties.EventId, eventWrapper); |
| 62 | + return Task.FromResult((IEvent<TEvent>?)eventWrapper); |
| 63 | + } |
| 64 | + |
| 65 | + return Task.FromResult<IEvent<TEvent>?>(null); |
| 66 | + } |
| 67 | + |
| 68 | + public Task AckMessageAsync(string eventId) |
| 69 | + { |
| 70 | + _unAckedMessages.TryRemove(eventId, out _); |
| 71 | + return Task.CompletedTask; |
| 72 | + } |
| 73 | + |
| 74 | + public void RequeueUnAckedMessages() |
| 75 | + { |
| 76 | + foreach (var message in _unAckedMessages) |
| 77 | + { |
| 78 | + if (DateTimeOffset.Now - message.Value.Properties.EventAt > _options.AckTimeout) |
| 79 | + { |
| 80 | + if (_unAckedMessages.TryRemove(message.Key, out var eventWrapper) |
| 81 | + && eventWrapper != null) |
| 82 | + { |
| 83 | + _queue.Enqueue(eventWrapper); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public async IAsyncEnumerable<IEvent> ReadAllAsync( |
| 90 | + [EnumeratorCancellation] CancellationToken cancellationToken = default) |
| 91 | + { |
| 92 | + while (!cancellationToken.IsCancellationRequested) |
| 93 | + { |
| 94 | + while (_queue.TryDequeue(out var eventWrapper)) |
| 95 | + { |
| 96 | + _unAckedMessages.TryAdd(eventWrapper.Properties.EventId, eventWrapper); |
| 97 | + yield return eventWrapper; |
| 98 | + } |
| 99 | + |
| 100 | + await Task.Delay(200, cancellationToken); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + protected override void Dispose(bool disposing) |
| 105 | + { |
| 106 | + _timer?.Dispose(); |
| 107 | + base.Dispose(disposing); |
| 108 | + } |
| 109 | +} |
0 commit comments