| | | 1 | | using System.Buffers.Binary; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using ClientManager.Shared.Configuration; |
| | | 4 | | using RabbitMQ.Client; |
| | | 5 | | using RabbitMQ.Client.Events; |
| | | 6 | | |
| | | 7 | | namespace ClientManager.Shared.Messaging; |
| | | 8 | | |
| | 1 | 9 | | public class MessageBrokerFactory(RabbitMQConnectionConfiguration rabbitMQConnectionConfiguration) : IMessageBrokerFacto |
| | | 10 | | { |
| | | 11 | | IConnection? _connection; |
| | 1 | 12 | | readonly ConcurrentDictionary<string, IChannel> _channels = []; |
| | | 13 | | const string exchangeName = "client-manager"; |
| | 1 | 14 | | readonly LinkedList<ulong> outstandingConfirms = new(); |
| | 1 | 15 | | readonly SemaphoreSlim semaphore = new(1, 1); |
| | 1 | 16 | | readonly TaskCompletionSource<bool> allMessagesConfirmedTcs = new(TaskCreationOptions.RunContinuationsAsynchronously |
| | | 17 | | const int MESSAGE_COUNT = 10000; |
| | | 18 | | int confirmedCount = 0; |
| | | 19 | | |
| | 1 | 20 | | readonly ConnectionFactory _connectionFactory = |
| | 1 | 21 | | new() |
| | 1 | 22 | | { |
| | 1 | 23 | | Uri = new Uri(rabbitMQConnectionConfiguration.Url), |
| | 1 | 24 | | Port = rabbitMQConnectionConfiguration.AmqpPort, |
| | 1 | 25 | | VirtualHost = rabbitMQConnectionConfiguration.VirtualHost, |
| | 1 | 26 | | UserName = rabbitMQConnectionConfiguration.Username, |
| | 1 | 27 | | Password = rabbitMQConnectionConfiguration.Password |
| | 1 | 28 | | }; |
| | | 29 | | |
| | | 30 | | public async ValueTask<IConnection> GetConnectionAsync() |
| | | 31 | | { |
| | 2 | 32 | | if (_connection != null && _connection.IsOpen) |
| | 1 | 33 | | return _connection; |
| | | 34 | | |
| | 1 | 35 | | _connection = await _connectionFactory.CreateConnectionAsync(); |
| | 1 | 36 | | return _connection; |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async ValueTask<IChannel> GetOrCreateChannelAsync(string channelName, CreateChannelOptions? options = null) |
| | | 40 | | { |
| | 2 | 41 | | _connection = await GetConnectionAsync(); |
| | | 42 | | |
| | 2 | 43 | | if (!_channels.TryGetValue(channelName, out var channel)) |
| | | 44 | | { |
| | 2 | 45 | | channel = await _connection.CreateChannelAsync(options); |
| | 2 | 46 | | _channels.AddOrUpdate(channelName, channel, (key, oldValue) => channel); |
| | | 47 | | } |
| | | 48 | | |
| | 2 | 49 | | return channel; |
| | 2 | 50 | | } |
| | | 51 | | |
| | | 52 | | public async ValueTask<IChannel> GetPublishChannelAsync(string exchange, CreateChannelOptions? options = null) |
| | | 53 | | { |
| | 4 | 54 | | exchange = string.IsNullOrWhiteSpace(exchange) ? exchangeName : exchange; |
| | | 55 | | |
| | 4 | 56 | | if (!_channels.TryGetValue(exchange, out IChannel? existing) || existing is null) |
| | | 57 | | { |
| | 1 | 58 | | options ??= new CreateChannelOptions( |
| | 1 | 59 | | publisherConfirmationsEnabled: true, |
| | 1 | 60 | | publisherConfirmationTrackingEnabled: true, |
| | 1 | 61 | | outstandingPublisherConfirmationsRateLimiter: new ThrottlingRateLimiter(1000) |
| | 1 | 62 | | ); |
| | 1 | 63 | | var channel = await GetOrCreateChannelAsync(exchange, options); |
| | 1 | 64 | | await channel.ExchangeDeclareAsync(exchange, "direct", durable: true, autoDelete: false, arguments: null); |
| | 1 | 65 | | channel.BasicAcksAsync += OnAck; |
| | 1 | 66 | | channel.BasicNacksAsync += OnNack; |
| | 1 | 67 | | channel.BasicReturnAsync += OnBasicReturn; |
| | 1 | 68 | | return channel; |
| | | 69 | | } |
| | | 70 | | |
| | 3 | 71 | | return existing; |
| | 4 | 72 | | } |
| | | 73 | | |
| | | 74 | | public async ValueTask<IChannel> GetConsumeChannelAsync(string queueName, string exchange = "", string routingKey = |
| | | 75 | | { |
| | 0 | 76 | | exchange = string.IsNullOrWhiteSpace(exchange) ? exchangeName : exchange; |
| | 0 | 77 | | routingKey = string.IsNullOrWhiteSpace(routingKey) ? queueName : routingKey; |
| | | 78 | | |
| | 0 | 79 | | if (!_channels.TryGetValue(queueName, out IChannel? existing) || existing is null) |
| | | 80 | | { |
| | 0 | 81 | | var channel = await GetOrCreateChannelAsync(queueName, options); |
| | 0 | 82 | | await channel.ExchangeDeclareAsync(exchange, "direct", durable: true, autoDelete: false, arguments: null); |
| | 0 | 83 | | await channel.QueueDeclareAsync(queueName, durable: true, exclusive: false, autoDelete: false, arguments: nu |
| | 0 | 84 | | await channel.QueueBindAsync(queueName, exchange, routingKey); |
| | 0 | 85 | | return channel; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | return existing; |
| | 0 | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | public ConnectionFactory GetConnectionFactory() => _connectionFactory; |
| | | 92 | | |
| | 4 | 93 | | async Task OnAck(object sender, BasicAckEventArgs args) => await CleanOutstandingConfirms(args.DeliveryTag, args.Mul |
| | | 94 | | |
| | 0 | 95 | | async Task OnNack(object sender, BasicNackEventArgs args) => await CleanOutstandingConfirms(args.DeliveryTag, args.M |
| | | 96 | | |
| | | 97 | | Task OnBasicReturn(object sender, BasicReturnEventArgs args) |
| | | 98 | | { |
| | 0 | 99 | | var props = args.BasicProperties; |
| | 0 | 100 | | ulong sequenceNumber = 0; |
| | | 101 | | |
| | 0 | 102 | | if (props.Headers != null && props.Headers.TryGetValue(Constants.PublishSequenceNumberHeader, out var headerValu |
| | | 103 | | { |
| | 0 | 104 | | sequenceNumber = headerValue switch |
| | 0 | 105 | | { |
| | 0 | 106 | | byte[] bytes => BinaryPrimitives.ReadUInt64BigEndian(bytes), |
| | 0 | 107 | | long longValue => (ulong)longValue, |
| | 0 | 108 | | int intValue => (ulong)intValue, |
| | 0 | 109 | | _ => 0 |
| | 0 | 110 | | }; |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | Console.WriteLine( |
| | 0 | 114 | | $"{DateTime.Now} [WARNING] message has been basic.return-ed: Exchange={args.Exchange}, RoutingKey={args.Rout |
| | 0 | 115 | | ); |
| | 0 | 116 | | return Task.CompletedTask; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | async Task CleanOutstandingConfirms(ulong deliveryTag, bool multiple) |
| | | 120 | | { |
| | 4 | 121 | | await semaphore.WaitAsync(); |
| | | 122 | | try |
| | | 123 | | { |
| | 4 | 124 | | if (multiple) |
| | | 125 | | { |
| | 0 | 126 | | while (outstandingConfirms.First is { } node && node.Value <= deliveryTag) |
| | | 127 | | { |
| | 0 | 128 | | outstandingConfirms.RemoveFirst(); |
| | 0 | 129 | | confirmedCount++; |
| | 0 | 130 | | } |
| | | 131 | | } |
| | | 132 | | else |
| | | 133 | | { |
| | 4 | 134 | | outstandingConfirms.Remove(deliveryTag); |
| | 4 | 135 | | confirmedCount++; |
| | | 136 | | } |
| | 4 | 137 | | } |
| | | 138 | | finally |
| | | 139 | | { |
| | 4 | 140 | | semaphore.Release(); |
| | | 141 | | } |
| | | 142 | | |
| | 4 | 143 | | if (outstandingConfirms.Count == 0 || confirmedCount == MESSAGE_COUNT) |
| | | 144 | | { |
| | 4 | 145 | | allMessagesConfirmedTcs.TrySetResult(true); |
| | | 146 | | } |
| | 4 | 147 | | } |
| | | 148 | | |
| | | 149 | | public async ValueTask<IChannel> DeclareAndBindQueue(string queueName, string exchange = exchangeName, string routin |
| | | 150 | | { |
| | 2 | 151 | | exchange = string.IsNullOrWhiteSpace(exchange) ? exchangeName : exchange; |
| | 2 | 152 | | routingKey = string.IsNullOrWhiteSpace(routingKey) ? queueName : routingKey; |
| | | 153 | | |
| | 2 | 154 | | if (!_channels.TryGetValue(queueName, out IChannel? existing) || existing is null) |
| | | 155 | | { |
| | 1 | 156 | | var channel = await GetOrCreateChannelAsync(queueName, options); |
| | 1 | 157 | | await channel.ExchangeDeclareAsync(exchange, "direct", durable: true, autoDelete: false, arguments: null); |
| | 1 | 158 | | await channel.QueueDeclareAsync(queueName, durable: true, exclusive: false, autoDelete: false, arguments: nu |
| | 1 | 159 | | await channel.QueueBindAsync(queueName, exchange, routingKey); |
| | 1 | 160 | | return channel; |
| | | 161 | | } |
| | | 162 | | |
| | 1 | 163 | | return existing; |
| | 2 | 164 | | } |
| | | 165 | | |
| | | 166 | | public async ValueTask DisposeAsync() |
| | | 167 | | { |
| | 6 | 168 | | foreach (var channel in _channels.Values) |
| | | 169 | | { |
| | 2 | 170 | | await channel.CloseAsync(); |
| | | 171 | | } |
| | 1 | 172 | | _channels.Clear(); |
| | 1 | 173 | | if (_connection != null && _connection.IsOpen) |
| | | 174 | | { |
| | 0 | 175 | | await _connection.CloseAsync(); |
| | | 176 | | } |
| | | 177 | | |
| | 1 | 178 | | GC.SuppressFinalize(this); |
| | 1 | 179 | | } |
| | | 180 | | } |