| | | 1 | | using Amazon.SQS; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Net; |
| | | 5 | | using System.Text.Json; |
| | | 6 | | |
| | | 7 | | namespace AsyncResponse.Transports.SQS; |
| | | 8 | | |
| | | 9 | | /// <summary>Publishes <see cref="WorkerJobEnvelope"/> messages to an AWS SQS queue.</summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// The worker queue URL is resolved lazily (a queue configured by name goes through |
| | | 12 | | /// <c>GetQueueUrl</c> once) and cached for the lifetime of the transport; a transient resolution |
| | | 13 | | /// failure on the first publish is not cached, so the next publish retries. When the worker queue |
| | | 14 | | /// is a FIFO queue (name or URL ending in <c>.fifo</c>), the correlation id becomes the |
| | | 15 | | /// <c>MessageGroupId</c> so one flow's jobs stay ordered, and every message carries a unique |
| | | 16 | | /// <c>MessageDeduplicationId</c> so distinct jobs of the same flow are never deduplicated away. |
| | | 17 | | /// </remarks> |
| | | 18 | | public sealed class SqsWorkerTransport : IWorkerTransport, IAsyncDisposable |
| | | 19 | | { |
| | | 20 | | private readonly SqsAsyncResponseOptions _options; |
| | | 21 | | private readonly ISqsClient _client; |
| | | 22 | | private readonly bool _disposeClient; |
| | | 23 | | private readonly bool _isFifoQueue; |
| | 3 | 24 | | private readonly SemaphoreSlim _queueUrlGate = new(1, 1); |
| | | 25 | | private string? _queueUrl; |
| | | 26 | | private int _disposeGate; |
| | | 27 | | private bool _disposed; |
| | | 28 | | |
| | | 29 | | /// <summary>Creates a worker transport backed by a client built from the configured options.</summary> |
| | | 30 | | public SqsWorkerTransport(IOptions<SqsAsyncResponseOptions> options) |
| | 2 | 31 | | : this(options, SqsClientFactory.Create(options.Value), disposeClient: true) |
| | | 32 | | { |
| | 2 | 33 | | } |
| | | 34 | | |
| | | 35 | | internal SqsWorkerTransport( |
| | | 36 | | IOptions<SqsAsyncResponseOptions> options, |
| | | 37 | | ISqsClient client) |
| | 3 | 38 | | : this(options, client, disposeClient: false) |
| | | 39 | | { |
| | 3 | 40 | | } |
| | | 41 | | |
| | 3 | 42 | | private SqsWorkerTransport( |
| | 3 | 43 | | IOptions<SqsAsyncResponseOptions> options, |
| | 3 | 44 | | ISqsClient client, |
| | 3 | 45 | | bool disposeClient) |
| | | 46 | | { |
| | 3 | 47 | | _options = options.Value; |
| | 3 | 48 | | SqsOptionsValidator.ValidateCommon(_options); |
| | 3 | 49 | | _client = client; |
| | 3 | 50 | | _disposeClient = disposeClient; |
| | 3 | 51 | | _isFifoQueue = SqsQueueAddress.IsFifo(_options.WorkerQueue); |
| | 3 | 52 | | } |
| | | 53 | | |
| | | 54 | | private async Task<string> GetQueueUrlAsync(CancellationToken cancellationToken) |
| | | 55 | | { |
| | 3 | 56 | | var queueUrl = Volatile.Read(ref _queueUrl); |
| | 3 | 57 | | if (queueUrl is not null) |
| | 3 | 58 | | return queueUrl; |
| | | 59 | | |
| | 3 | 60 | | await _queueUrlGate.WaitAsync(cancellationToken).ConfigureAwait(false); |
| | | 61 | | try |
| | | 62 | | { |
| | 3 | 63 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 3 | 64 | | if (_queueUrl is not null) |
| | 2 | 65 | | return _queueUrl; |
| | | 66 | | |
| | | 67 | | // Assign only after the await succeeds, so a faulted resolution is not cached and the |
| | | 68 | | // next publish retries instead of reusing a permanently failed lookup. |
| | 3 | 69 | | var resolved = SqsQueueAddress.IsUrl(_options.WorkerQueue) |
| | 3 | 70 | | ? _options.WorkerQueue |
| | 3 | 71 | | : await _client.GetQueueUrlAsync(_options.WorkerQueue, cancellationToken).ConfigureAwait(false); |
| | 3 | 72 | | _queueUrl = resolved; |
| | 3 | 73 | | return resolved; |
| | | 74 | | } |
| | | 75 | | finally |
| | | 76 | | { |
| | 3 | 77 | | _queueUrlGate.Release(); |
| | | 78 | | } |
| | 3 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary>Publishes the supplied worker job.</summary> |
| | | 82 | | public async Task PublishAsync(WorkerJobEnvelope job, CancellationToken cancellationToken = default) |
| | | 83 | | { |
| | 3 | 84 | | ArgumentNullException.ThrowIfNull(job); |
| | | 85 | | |
| | 3 | 86 | | using var activity = AsyncResponseDiagnostics.StartActivity( |
| | 3 | 87 | | "asyncresponse.worker.publish", |
| | 3 | 88 | | ActivityKind.Producer, |
| | 3 | 89 | | job.CorrelationId); |
| | 3 | 90 | | activity?.SetTag("asyncresponse.transport", "aws_sqs"); |
| | 3 | 91 | | activity?.SetTag("messaging.system", "aws_sqs"); |
| | 3 | 92 | | activity?.SetTag("messaging.destination.name", _options.WorkerQueue); |
| | 3 | 93 | | AsyncResponseDiagnostics.SetReplyTarget(activity, job.ReplyTarget); |
| | 3 | 94 | | AsyncResponseDiagnostics.SetWorker(activity, job.Call); |
| | | 95 | | |
| | | 96 | | try |
| | | 97 | | { |
| | 3 | 98 | | var messageAttributes = new Dictionary<string, string>(StringComparer.Ordinal); |
| | 3 | 99 | | if (!string.IsNullOrWhiteSpace(job.CorrelationId)) |
| | 3 | 100 | | messageAttributes[_options.CorrelationIdAttribute] = job.CorrelationId; |
| | | 101 | | |
| | 3 | 102 | | var queueUrl = await GetQueueUrlAsync(cancellationToken).ConfigureAwait(false); |
| | 3 | 103 | | var message = new SqsOutboundMessage( |
| | 3 | 104 | | queueUrl, |
| | 3 | 105 | | AsyncResponseJson.Serialize(job), |
| | 3 | 106 | | string.IsNullOrWhiteSpace(job.CorrelationId) ? null : job.CorrelationId, |
| | 3 | 107 | | MessageGroupId: _isFifoQueue |
| | 3 | 108 | | ? (string.IsNullOrWhiteSpace(job.CorrelationId) ? _options.FifoMessageGroupIdFallback : job.Correlat |
| | 3 | 109 | | : null, |
| | 3 | 110 | | MessageDeduplicationId: _isFifoQueue ? Guid.NewGuid().ToString("N") : null, |
| | 3 | 111 | | messageAttributes); |
| | | 112 | | |
| | 3 | 113 | | var messageId = await SendWithRetryAsync(message, cancellationToken).ConfigureAwait(false); |
| | 3 | 114 | | activity?.SetTag("messaging.message.id", messageId); |
| | 3 | 115 | | } |
| | 2 | 116 | | catch (Exception ex) |
| | | 117 | | { |
| | 2 | 118 | | AsyncResponseDiagnostics.SetError(activity, ex); |
| | 2 | 119 | | throw; |
| | | 120 | | } |
| | 3 | 121 | | } |
| | | 122 | | |
| | | 123 | | private async Task<string> SendWithRetryAsync( |
| | | 124 | | SqsOutboundMessage message, |
| | | 125 | | CancellationToken cancellationToken) |
| | | 126 | | { |
| | 3 | 127 | | for (var attempt = 1; ; attempt++) |
| | | 128 | | { |
| | | 129 | | try |
| | | 130 | | { |
| | 3 | 131 | | return await _client.SendMessageAsync(message, cancellationToken).ConfigureAwait(false); |
| | | 132 | | } |
| | 2 | 133 | | catch (Exception ex) when (IsTransient(ex) && attempt < _options.PublishMaxAttempts && !cancellationToken.Is |
| | | 134 | | { |
| | 2 | 135 | | var delay = AsyncResponseRetry.Backoff(attempt, _options.PublishRetryBaseDelay, _options.PublishRetryMax |
| | 2 | 136 | | await Task.Delay(delay, cancellationToken).ConfigureAwait(false); |
| | | 137 | | } |
| | | 138 | | } |
| | 3 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary>Classifies AWS SQS send failures worth retrying at the transport level.</summary> |
| | | 142 | | internal static bool IsTransient(Exception exception) |
| | 2 | 143 | | => exception is AmazonSQSException sqsException |
| | 2 | 144 | | && (sqsException.Retryable is not null |
| | 2 | 145 | | || sqsException.StatusCode >= HttpStatusCode.InternalServerError |
| | 2 | 146 | | || string.Equals(sqsException.ErrorCode, "RequestThrottled", StringComparison.Ordinal) |
| | 2 | 147 | | || string.Equals(sqsException.ErrorCode, "ThrottlingException", StringComparison.Ordinal)); |
| | | 148 | | |
| | | 149 | | /// <summary>Releases resources held by this instance.</summary> |
| | | 150 | | public async ValueTask DisposeAsync() |
| | | 151 | | { |
| | 3 | 152 | | if (Interlocked.Exchange(ref _disposeGate, 1) != 0) |
| | 3 | 153 | | return; |
| | | 154 | | |
| | 3 | 155 | | await _queueUrlGate.WaitAsync().ConfigureAwait(false); |
| | | 156 | | try |
| | | 157 | | { |
| | 3 | 158 | | _disposed = true; |
| | 3 | 159 | | if (_disposeClient) |
| | 2 | 160 | | await _client.DisposeAsync().ConfigureAwait(false); |
| | 3 | 161 | | } |
| | | 162 | | finally |
| | | 163 | | { |
| | 3 | 164 | | _queueUrlGate.Release(); |
| | 3 | 165 | | _queueUrlGate.Dispose(); |
| | | 166 | | } |
| | 3 | 167 | | } |
| | | 168 | | } |