| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.SQS; |
| | | 4 | | |
| | 207 | 5 | | internal sealed class SqsReplyTargetProvider( |
| | 207 | 6 | | IOptions<SqsAsyncResponseOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 7 | | { |
| | | 8 | | /// <summary>Gets the configured reply target.</summary> |
| | | 9 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 10 | | { |
| | 12 | 11 | | var options = _options.Value; |
| | 12 | 12 | | SqsOptionsValidator.ValidateCommon(options); |
| | 12 | 13 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 12 | 14 | | ? options.DefaultReplyTargetName |
| | 12 | 15 | | : name; |
| | | 16 | | |
| | 12 | 17 | | var target = ResolveTarget(options, targetName); |
| | 10 | 18 | | var queue = SqsOptionsValidator.Required( |
| | 10 | 19 | | target.Queue, |
| | 10 | 20 | | $"{nameof(SqsReplyTargetOptions)}.{nameof(SqsReplyTargetOptions.Queue)}"); |
| | | 21 | | |
| | | 22 | | // A NAMED target must not be the worker queue or a derived dead-letter queue |
| | | 23 | | // (DB-transport parity): its responses would be consumed as worker jobs (or sit in a |
| | | 24 | | // redrive DLQ), while the waiter times out. |
| | 10 | 25 | | var deadLetterCollision = !string.IsNullOrWhiteSpace(options.DeadLetterQueueSuffix) |
| | 10 | 26 | | && (StringComparer.Ordinal.Equals(queue, SqsQueueAddress.DeriveDeadLetterQueueName(options.WorkerQueue, opti |
| | 10 | 27 | | || StringComparer.Ordinal.Equals(queue, SqsQueueAddress.DeriveDeadLetterQueueName(options.ResponseQueue, |
| | 10 | 28 | | if (StringComparer.Ordinal.Equals(queue, options.WorkerQueue) || deadLetterCollision) |
| | | 29 | | { |
| | 6 | 30 | | throw new InvalidOperationException( |
| | 6 | 31 | | $"SQS async-response reply target '{targetName}' uses queue '{queue}', which collides with " + |
| | 6 | 32 | | $"{nameof(SqsAsyncResponseOptions.WorkerQueue)} or a derived dead-letter queue; " + |
| | 6 | 33 | | "its responses would be consumed as worker jobs (or mixed into dead letters)."); |
| | | 34 | | } |
| | | 35 | | |
| | 4 | 36 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 4 | 37 | | { |
| | 4 | 38 | | ["queue"] = queue, |
| | 4 | 39 | | ["correlationIdAttribute"] = options.CorrelationIdAttribute |
| | 4 | 40 | | }; |
| | | 41 | | |
| | 4 | 42 | | return new AsyncResponseReplyTarget |
| | 4 | 43 | | { |
| | 4 | 44 | | Name = targetName, |
| | 4 | 45 | | Transport = SqsAsyncResponseOptions.TransportName, |
| | 4 | 46 | | Address = queue, |
| | 4 | 47 | | Properties = properties |
| | 4 | 48 | | }; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | private static SqsReplyTargetOptions ResolveTarget( |
| | | 52 | | SqsAsyncResponseOptions options, |
| | | 53 | | string targetName) |
| | | 54 | | { |
| | 12 | 55 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 8 | 56 | | return configured; |
| | | 57 | | |
| | 4 | 58 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | 2 | 59 | | return new SqsReplyTargetOptions { Queue = options.ResponseQueue }; |
| | | 60 | | |
| | 2 | 61 | | throw new InvalidOperationException( |
| | 2 | 62 | | $"SQS async-response reply target '{targetName}' is not configured. " + |
| | 2 | 63 | | $"Configure {nameof(SqsAsyncResponseOptions.ResponseQueue)} for the default target " + |
| | 2 | 64 | | $"or add a named target with {nameof(SqsAsyncResponseOptions.AddReplyTarget)}."); |
| | | 65 | | } |
| | | 66 | | } |