| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | |
| | | 6 | | namespace AsyncResponse.Transports.RabbitMQ; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Publishes <see cref="WorkerJobEnvelope"/> messages to a RabbitMQ exchange. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// The publish channel is created lazily and re-created on demand: a transient broker outage when the |
| | | 13 | | /// first job is published no longer permanently breaks the transport (a faulted connect attempt is not |
| | | 14 | | /// cached). The channel is opened with publisher confirmations so <see cref="PublishAsync"/> only completes |
| | | 15 | | /// once the broker has accepted the message. A single channel is shared across concurrent publishers; |
| | | 16 | | /// RabbitMQ.Client v7 tracks each in-flight confirmation independently, so concurrent publishing is safe. |
| | | 17 | | /// </remarks> |
| | | 18 | | public sealed class RabbitMqWorkerTransport : IWorkerTransport, IWorkerTransportInFlightLimit, IAsyncDisposable |
| | | 19 | | { |
| | | 20 | | private readonly RabbitMqAsyncResponseOptions _options; |
| | | 21 | | private readonly IRabbitMqConnectionFactory _connectionFactory; |
| | | 22 | | private readonly TimeSpan _discardedChannelDisposeDelay; |
| | 240 | 23 | | private readonly SemaphoreSlim _connectionGate = new(1, 1); |
| | | 24 | | private IRabbitMqConnection? _connection; |
| | | 25 | | private IRabbitMqChannel? _channel; |
| | | 26 | | private int _disposeGate; |
| | | 27 | | private bool _disposed; |
| | | 28 | | |
| | | 29 | | /// <summary>Runs the RabbitMqWorkerTransport operation.</summary> |
| | | 30 | | public RabbitMqWorkerTransport(IOptions<RabbitMqAsyncResponseOptions> options) |
| | 196 | 31 | | : this(options, new RabbitMqConnectionFactoryAdapter(options.Value)) |
| | | 32 | | { |
| | 196 | 33 | | } |
| | | 34 | | |
| | 240 | 35 | | internal RabbitMqWorkerTransport( |
| | 240 | 36 | | IOptions<RabbitMqAsyncResponseOptions> options, |
| | 240 | 37 | | IRabbitMqConnectionFactory connectionFactory, |
| | 240 | 38 | | TimeSpan? discardedChannelDisposeDelay = null) |
| | | 39 | | { |
| | 240 | 40 | | _options = options.Value; |
| | 240 | 41 | | ValidatePublishOptions(_options); |
| | 226 | 42 | | _connectionFactory = connectionFactory; |
| | | 43 | | // Long enough for a publisher that took the lock-free fast path with the discarded |
| | | 44 | | // channel to have failed its BasicPublish on it and moved on; tests shorten it. |
| | 226 | 45 | | _discardedChannelDisposeDelay = discardedChannelDisposeDelay ?? TimeSpan.FromSeconds(30); |
| | 226 | 46 | | } |
| | | 47 | | |
| | | 48 | | private static void ValidatePublishOptions(RabbitMqAsyncResponseOptions options) |
| | | 49 | | { |
| | 240 | 50 | | _ = RabbitMqOptionsValidator.Required(options.WorkerExchange, nameof(options.WorkerExchange)); |
| | 236 | 51 | | _ = RabbitMqOptionsValidator.Required(options.WorkerQueue, nameof(options.WorkerQueue)); |
| | 232 | 52 | | _ = RabbitMqOptionsValidator.Required(options.WorkerRoutingKey, nameof(options.WorkerRoutingKey)); |
| | 228 | 53 | | RabbitMqOptionsValidator.ValidateConnection(options); |
| | 228 | 54 | | RabbitMqOptionsValidator.ValidateConsumerTimeout(options); |
| | 228 | 55 | | AsyncResponseChannelOptions.EnsureTimerBacked(options.ShutdownTimeout, nameof(RabbitMqAsyncResponseOptions), nam |
| | 226 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// The broker's <c>consumer_timeout</c> as mirrored by |
| | | 60 | | /// <see cref="RabbitMqAsyncResponseOptions.BrokerConsumerTimeout"/>: past it RabbitMQ closes the |
| | | 61 | | /// consumer's channel and requeues the still-unacknowledged delivery while its handler runs. |
| | | 62 | | /// <c>null</c> when that option is <c>null</c>, and when the worker subscriber uses |
| | | 63 | | /// <see cref="RabbitMqAckMode.AckAfterEnqueue"/> — the delivery is acknowledged before its handler |
| | | 64 | | /// starts, so no handler run is ever in flight at the broker. |
| | | 65 | | /// </summary> |
| | | 66 | | public TimeSpan? MaxInFlightDuration |
| | 246 | 67 | | => _options.WorkerSubscriber.AckMode == RabbitMqAckMode.AckAfterEnqueue |
| | 246 | 68 | | ? null |
| | 246 | 69 | | : _options.BrokerConsumerTimeout; |
| | | 70 | | |
| | | 71 | | private async Task<IRabbitMqChannel> GetChannelAsync(CancellationToken cancellationToken) |
| | | 72 | | { |
| | 459 | 73 | | var channel = Volatile.Read(ref _channel); |
| | 459 | 74 | | if (channel is not null && channel.IsOpen) |
| | 230 | 75 | | return channel; |
| | | 76 | | |
| | 229 | 77 | | await _connectionGate.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | | 78 | | try |
| | | 79 | | { |
| | 229 | 80 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 227 | 81 | | if (_channel is not null && _channel.IsOpen) |
| | 0 | 82 | | return _channel; |
| | | 83 | | |
| | | 84 | | // A cached-but-closed channel is treated as absent: a protocol error (404/406 after an |
| | | 85 | | // exchange delete/redeclare) closes the channel without failing the publish that comes |
| | | 86 | | // next, so keeping it would cache a dead channel forever. Deliberately dereferenced |
| | | 87 | | // WITHOUT disposing: a concurrent publisher that took the lock-free fast path above may |
| | | 88 | | // still hold this object, and disposing it under that publisher turns its retryable |
| | | 89 | | // AlreadyClosedException into a use-after-dispose. A closed channel holds no broker |
| | | 90 | | // resources — but the client only forgets it when it is disposed, and this connection |
| | | 91 | | // lives as long as the process, so every protocol-error replacement accumulated one |
| | | 92 | | // more recorded channel on it. Dispose the discarded one best-effort, later, off this |
| | | 93 | | // path: by then any fast-path publisher holding it has long failed on it. |
| | 227 | 94 | | if (_channel is { } discarded) |
| | 6 | 95 | | _ = DisposeDiscardedChannelLaterAsync(discarded, _discardedChannelDisposeDelay); |
| | 227 | 96 | | _channel = null; |
| | | 97 | | |
| | | 98 | | // The connection is replaced only when itself closed: with automatic recovery enabled the |
| | | 99 | | // client object stays open while it reconnects, so this only fires when it is truly dead |
| | | 100 | | // (e.g. AutomaticRecoveryEnabled = false). |
| | 227 | 101 | | if (_connection is not null && !_connection.IsOpen) |
| | | 102 | | { |
| | 2 | 103 | | await DisposeQuietlyAsync(_connection).ConfigureAwait(false); |
| | 2 | 104 | | _connection = null; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // ??= only assigns when the await succeeds, so a failed connect leaves _connection null and |
| | | 108 | | // the next publish retries. A successful connection is reused even if channel/topology setup fails. |
| | 227 | 109 | | _connection ??= await _connectionFactory.CreateConnectionAsync(cancellationToken).ConfigureAwait(false); |
| | 225 | 110 | | var created = await _connection.CreateChannelAsync(publisherConfirmations: true, cancellationToken).Configur |
| | | 111 | | try |
| | | 112 | | { |
| | 225 | 113 | | await RabbitMqTopology.EnsureWorkerAsync(created, _options, cancellationToken).ConfigureAwait(false); |
| | 223 | 114 | | } |
| | 2 | 115 | | catch |
| | | 116 | | { |
| | | 117 | | // Not cached yet, so nothing else ever disposes it: release the channel here or leak it. |
| | 2 | 118 | | await DisposeQuietlyAsync(created).ConfigureAwait(false); |
| | 2 | 119 | | throw; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | // Publish the channel only once it is fully initialized; if anything above threw, _channel stays |
| | | 123 | | // null so a later publish recreates it instead of awaiting a permanently faulted task. |
| | 223 | 124 | | _channel = created; |
| | 223 | 125 | | return created; |
| | | 126 | | } |
| | | 127 | | finally |
| | | 128 | | { |
| | 229 | 129 | | _connectionGate.Release(); |
| | | 130 | | } |
| | 453 | 131 | | } |
| | | 132 | | |
| | | 133 | | private static async Task DisposeDiscardedChannelLaterAsync(IRabbitMqChannel discarded, TimeSpan delay) |
| | | 134 | | { |
| | 6 | 135 | | await Task.Delay(delay).ConfigureAwait(false); |
| | 6 | 136 | | await DisposeQuietlyAsync(discarded).ConfigureAwait(false); |
| | 6 | 137 | | } |
| | | 138 | | |
| | | 139 | | private static async ValueTask DisposeQuietlyAsync(IAsyncDisposable resource) |
| | | 140 | | { |
| | | 141 | | try |
| | | 142 | | { |
| | 10 | 143 | | await resource.DisposeAsync().ConfigureAwait(false); |
| | 10 | 144 | | } |
| | 0 | 145 | | catch |
| | | 146 | | { |
| | | 147 | | // Best effort: the broker side of a dead channel/connection is already gone. |
| | 0 | 148 | | } |
| | 10 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary>Publishes the supplied message.</summary> |
| | | 152 | | public async Task PublishAsync(WorkerJobEnvelope job, CancellationToken cancellationToken = default) |
| | | 153 | | { |
| | 461 | 154 | | ArgumentNullException.ThrowIfNull(job); |
| | | 155 | | |
| | 459 | 156 | | using var activity = AsyncResponseDiagnostics.StartActivity( |
| | 459 | 157 | | "asyncresponse.worker.publish", |
| | 459 | 158 | | ActivityKind.Producer, |
| | 459 | 159 | | job.CorrelationId); |
| | 459 | 160 | | activity?.SetTag("asyncresponse.transport", "rabbitmq"); |
| | 459 | 161 | | activity?.SetTag("messaging.system", "rabbitmq"); |
| | 459 | 162 | | activity?.SetTag("messaging.destination.name", _options.WorkerExchange); |
| | 459 | 163 | | activity?.SetTag("messaging.rabbitmq.routing_key", _options.WorkerRoutingKey); |
| | 459 | 164 | | AsyncResponseDiagnostics.SetReplyTarget(activity, job.ReplyTarget); |
| | 459 | 165 | | AsyncResponseDiagnostics.SetWorker(activity, job.Call); |
| | | 166 | | |
| | | 167 | | try |
| | | 168 | | { |
| | 459 | 169 | | var payload = Encoding.UTF8.GetBytes(AsyncResponseJson.Serialize(job)); |
| | 459 | 170 | | var properties = RabbitMqTopology.CreatePersistentJsonProperties(job.CorrelationId, _options.CorrelationIdHe |
| | 459 | 171 | | var channel = await GetChannelAsync(cancellationToken).ConfigureAwait(false); |
| | 453 | 172 | | await channel.BasicPublishAsync( |
| | 453 | 173 | | _options.WorkerExchange, |
| | 453 | 174 | | _options.WorkerRoutingKey, |
| | 453 | 175 | | properties, |
| | 453 | 176 | | payload, |
| | 453 | 177 | | cancellationToken).ConfigureAwait(false); |
| | 451 | 178 | | activity?.SetTag("messaging.message.id", properties.MessageId); |
| | 451 | 179 | | } |
| | 8 | 180 | | catch (Exception ex) |
| | | 181 | | { |
| | 8 | 182 | | AsyncResponseDiagnostics.SetError(activity, ex); |
| | 8 | 183 | | throw; |
| | | 184 | | } |
| | 451 | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary>Releases resources held by this instance.</summary> |
| | | 188 | | public async ValueTask DisposeAsync() |
| | | 189 | | { |
| | 400 | 190 | | if (Interlocked.Exchange(ref _disposeGate, 1) != 0) |
| | 196 | 191 | | return; |
| | | 192 | | |
| | 204 | 193 | | await _connectionGate.WaitAsync().ConfigureAwait(false); |
| | | 194 | | try |
| | | 195 | | { |
| | 204 | 196 | | _disposed = true; |
| | | 197 | | |
| | 204 | 198 | | if (_channel is not null) |
| | | 199 | | { |
| | 199 | 200 | | using var cts = new CancellationTokenSource(_options.ShutdownTimeout); |
| | | 201 | | try |
| | | 202 | | { |
| | 199 | 203 | | await _channel.CloseAsync(cts.Token).ConfigureAwait(false); |
| | 197 | 204 | | } |
| | 2 | 205 | | catch |
| | | 206 | | { |
| | | 207 | | // Best effort: the channel may already be closed by broker-side shutdown. |
| | 2 | 208 | | } |
| | | 209 | | |
| | 199 | 210 | | await _channel.DisposeAsync().ConfigureAwait(false); |
| | 199 | 211 | | } |
| | | 212 | | |
| | 204 | 213 | | if (_connection is not null) |
| | | 214 | | { |
| | 199 | 215 | | using var cts = new CancellationTokenSource(_options.ShutdownTimeout); |
| | | 216 | | try |
| | | 217 | | { |
| | 199 | 218 | | await _connection.CloseAsync(_options.ShutdownTimeout, cts.Token).ConfigureAwait(false); |
| | 197 | 219 | | } |
| | 2 | 220 | | catch |
| | | 221 | | { |
| | | 222 | | // Best effort. |
| | 2 | 223 | | } |
| | | 224 | | |
| | 199 | 225 | | await _connection.DisposeAsync().ConfigureAwait(false); |
| | 199 | 226 | | } |
| | 204 | 227 | | } |
| | | 228 | | finally |
| | | 229 | | { |
| | | 230 | | // Release, never Dispose: SemaphoreSlim.Dispose does not complete pending WaitAsync |
| | | 231 | | // waiters, so disposing here would strand publishers parked on the gate forever (and |
| | | 232 | | // the first woken waiter's finally would throw trying to Release a disposed |
| | | 233 | | // semaphore, never handing the permit on). Released, each parked waiter wakes in |
| | | 234 | | // turn and observes _disposed; the gate holds no unmanaged resources, so leaving it |
| | | 235 | | // undisposed leaks nothing. |
| | 204 | 236 | | _connectionGate.Release(); |
| | | 237 | | } |
| | 400 | 238 | | } |
| | | 239 | | } |