|
| 1 | +// This source code is dual-licensed under the Apache License, version |
| 2 | +// 2.0, and the Mozilla Public License, version 2.0. |
| 3 | +// Copyright (c) 2007-2020 VMware, Inc. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Concurrent; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Threading.Tasks.Dataflow; |
| 11 | +using System.Timers; |
| 12 | +using Timer = System.Timers.Timer; |
| 13 | + |
| 14 | +namespace RabbitMQ.Stream.Client.Reliable; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// ConfirmationStatus can be: |
| 18 | +/// </summary> |
| 19 | +public enum ConfirmationStatus : ushort |
| 20 | +{ |
| 21 | + WaitForConfirmation = 0, |
| 22 | + Confirmed = 1, |
| 23 | + TimeoutError = 2, |
| 24 | + StreamNotAvailable = 6, |
| 25 | + InternalError = 15, |
| 26 | + AccessRefused = 16, |
| 27 | + PreconditionFailed = 17, |
| 28 | + PublisherDoesNotExist = 18, |
| 29 | + UndefinedError = 200, |
| 30 | +} |
| 31 | + |
| 32 | +/// <summary> |
| 33 | +/// MessagesConfirmation is a wrapper around the message/s |
| 34 | +/// This class is returned to the user to understand |
| 35 | +/// the message status. |
| 36 | +/// </summary> |
| 37 | +public class MessagesConfirmation |
| 38 | +{ |
| 39 | + public ulong PublishingId { get; internal set; } |
| 40 | + public List<Message> Messages { get; internal init; } |
| 41 | + public DateTime InsertDateTime { get; init; } |
| 42 | + public ConfirmationStatus Status { get; internal set; } |
| 43 | +} |
| 44 | + |
| 45 | +/// <summary> |
| 46 | +/// ConfirmationPipe maintains the status for the sent and received messages. |
| 47 | +/// TPL Action block sends the confirmation to the user in async way |
| 48 | +/// So the send/1 is not blocking. |
| 49 | +/// </summary> |
| 50 | +public class ConfirmationPipe |
| 51 | +{ |
| 52 | + private ActionBlock<Tuple<ConfirmationStatus, ulong>> _waitForConfirmationActionBlock; |
| 53 | + private readonly ConcurrentDictionary<ulong, MessagesConfirmation> _waitForConfirmation = new(); |
| 54 | + private readonly Timer _invalidateTimer = new(); |
| 55 | + private Func<MessagesConfirmation, Task> ConfirmHandler { get; } |
| 56 | + |
| 57 | + public ConfirmationPipe(Func<MessagesConfirmation, Task> confirmHandler) |
| 58 | + { |
| 59 | + ConfirmHandler = confirmHandler; |
| 60 | + } |
| 61 | + |
| 62 | + public void Start() |
| 63 | + { |
| 64 | + _waitForConfirmationActionBlock = new ActionBlock<Tuple<ConfirmationStatus, ulong>>( |
| 65 | + request => |
| 66 | + { |
| 67 | + var (confirmationStatus, publishingId) = request; |
| 68 | + |
| 69 | + _waitForConfirmation.TryRemove(publishingId, out var message); |
| 70 | + if (message == null) |
| 71 | + { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + message.Status = confirmationStatus; |
| 76 | + ConfirmHandler?.Invoke(message); |
| 77 | + }, new ExecutionDataflowBlockOptions |
| 78 | + { |
| 79 | + MaxDegreeOfParallelism = 1, |
| 80 | + // throttling |
| 81 | + BoundedCapacity = 50_000 |
| 82 | + }); |
| 83 | + |
| 84 | + _invalidateTimer.Elapsed += OnTimedEvent; |
| 85 | + _invalidateTimer.Interval = 2000; |
| 86 | + _invalidateTimer.Enabled = true; |
| 87 | + } |
| 88 | + |
| 89 | + public void Stop() |
| 90 | + { |
| 91 | + _invalidateTimer.Enabled = false; |
| 92 | + _waitForConfirmationActionBlock.Complete(); |
| 93 | + } |
| 94 | + |
| 95 | + private async void OnTimedEvent(object? sender, ElapsedEventArgs e) |
| 96 | + { |
| 97 | + { |
| 98 | + foreach (var pair in _waitForConfirmation.Where(pair => |
| 99 | + (DateTime.Now - pair.Value.InsertDateTime).Seconds > 2)) |
| 100 | + { |
| 101 | + await RemoveUnConfirmedMessage(pair.Value.PublishingId, ConfirmationStatus.TimeoutError); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void AddUnConfirmedMessage(ulong publishingId, Message message) |
| 107 | + { |
| 108 | + AddUnConfirmedMessage(publishingId, new List<Message>() { message }); |
| 109 | + } |
| 110 | + |
| 111 | + public void AddUnConfirmedMessage(ulong publishingId, List<Message> messages) |
| 112 | + { |
| 113 | + _waitForConfirmation.TryAdd(publishingId, |
| 114 | + new MessagesConfirmation() |
| 115 | + { |
| 116 | + Messages = messages, |
| 117 | + PublishingId = publishingId, |
| 118 | + InsertDateTime = DateTime.Now |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + public Task RemoveUnConfirmedMessage(ulong publishingId, ConfirmationStatus confirmationStatus) |
| 123 | + { |
| 124 | + return _waitForConfirmationActionBlock.SendAsync( |
| 125 | + Tuple.Create(confirmationStatus, publishingId)); |
| 126 | + } |
| 127 | +} |
0 commit comments