| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.RabbitMQ; |
| | | 4 | | |
| | 211 | 5 | | internal sealed class RabbitMqReplyTargetProvider( |
| | 211 | 6 | | IOptions<RabbitMqAsyncResponseOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 7 | | { |
| | | 8 | | /// <summary>Gets the configured reply target.</summary> |
| | | 9 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 10 | | { |
| | 16 | 11 | | var options = _options.Value; |
| | | 12 | | // Options-validator parity with the other transports' providers: a hand-off address must |
| | | 13 | | // come from a configuration that passes the transport's own checks. |
| | 16 | 14 | | RabbitMqOptionsValidator.ValidateConnection(options); |
| | 16 | 15 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 16 | 16 | | ? options.DefaultReplyTargetName |
| | 16 | 17 | | : name; |
| | | 18 | | |
| | 16 | 19 | | var target = ResolveTarget(options, targetName); |
| | 12 | 20 | | var exchange = RabbitMqOptionsValidator.Required( |
| | 12 | 21 | | target.Exchange, |
| | 12 | 22 | | $"{nameof(RabbitMqReplyTargetOptions)}.{nameof(RabbitMqReplyTargetOptions.Exchange)}"); |
| | 12 | 23 | | var routingKey = RabbitMqOptionsValidator.Required( |
| | 12 | 24 | | target.RoutingKey, |
| | 12 | 25 | | $"{nameof(RabbitMqReplyTargetOptions)}.{nameof(RabbitMqReplyTargetOptions.RoutingKey)}"); |
| | 12 | 26 | | var queue = target.Queue ?? options.ResponseQueue; |
| | | 27 | | |
| | | 28 | | // A NAMED target must not route into the worker or dead-letter side (DB-transport parity): |
| | | 29 | | // the worker publish pair delivers its responses as worker jobs, the dead-letter exchange |
| | | 30 | | // mixes them into buried traffic, and a declared queue equal to the worker/dead-letter |
| | | 31 | | // queue does the same one hop later — while the waiter times out. |
| | 12 | 32 | | var routesToWorker = StringComparer.Ordinal.Equals(exchange, options.WorkerExchange) |
| | 12 | 33 | | && StringComparer.Ordinal.Equals(routingKey, options.WorkerRoutingKey); |
| | 12 | 34 | | var routesToDeadLetter = !string.IsNullOrWhiteSpace(options.DeadLetterExchange) |
| | 12 | 35 | | && StringComparer.Ordinal.Equals(exchange, options.DeadLetterExchange); |
| | 12 | 36 | | var queueCollides = !string.IsNullOrWhiteSpace(queue) |
| | 12 | 37 | | && (StringComparer.Ordinal.Equals(queue, options.WorkerQueue) |
| | 12 | 38 | | || (!string.IsNullOrWhiteSpace(options.DeadLetterQueue) && StringComparer.Ordinal.Equals(queue, options. |
| | 12 | 39 | | if (routesToWorker || routesToDeadLetter || queueCollides) |
| | | 40 | | { |
| | 4 | 41 | | throw new InvalidOperationException( |
| | 4 | 42 | | $"RabbitMQ async-response reply target '{targetName}' routes to '{exchange}:{routingKey}'" + |
| | 4 | 43 | | $"{(string.IsNullOrWhiteSpace(queue) ? "" : $" (queue '{queue}')")}, which collides with the worker or d |
| | 4 | 44 | | "its responses would be consumed as worker jobs (or mixed into dead letters)."); |
| | | 45 | | } |
| | | 46 | | |
| | 8 | 47 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 8 | 48 | | { |
| | 8 | 49 | | ["exchange"] = exchange, |
| | 8 | 50 | | ["routingKey"] = routingKey |
| | 8 | 51 | | }; |
| | | 52 | | |
| | 8 | 53 | | if (!string.IsNullOrWhiteSpace(queue)) |
| | 6 | 54 | | properties["queue"] = queue; |
| | | 55 | | |
| | 8 | 56 | | return new AsyncResponseReplyTarget |
| | 8 | 57 | | { |
| | 8 | 58 | | Name = targetName, |
| | 8 | 59 | | Transport = RabbitMqAsyncResponseOptions.TransportName, |
| | 8 | 60 | | Address = $"{exchange}:{routingKey}", |
| | 8 | 61 | | Properties = properties |
| | 8 | 62 | | }; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | private static RabbitMqReplyTargetOptions ResolveTarget( |
| | | 66 | | RabbitMqAsyncResponseOptions options, |
| | | 67 | | string targetName) |
| | | 68 | | { |
| | 16 | 69 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 10 | 70 | | return configured; |
| | | 71 | | |
| | 6 | 72 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName) |
| | 6 | 73 | | && !string.IsNullOrWhiteSpace(options.ResponseExchange) |
| | 6 | 74 | | && !string.IsNullOrWhiteSpace(options.ResponseRoutingKey)) |
| | | 75 | | { |
| | 2 | 76 | | return new RabbitMqReplyTargetOptions |
| | 2 | 77 | | { |
| | 2 | 78 | | Exchange = options.ResponseExchange, |
| | 2 | 79 | | RoutingKey = options.ResponseRoutingKey, |
| | 2 | 80 | | Queue = options.ResponseQueue |
| | 2 | 81 | | }; |
| | | 82 | | } |
| | | 83 | | |
| | 4 | 84 | | throw new InvalidOperationException( |
| | 4 | 85 | | $"RabbitMQ async-response reply target '{targetName}' is not configured. " + |
| | 4 | 86 | | $"Configure {nameof(RabbitMqAsyncResponseOptions.ResponseExchange)} and " + |
| | 4 | 87 | | $"{nameof(RabbitMqAsyncResponseOptions.ResponseRoutingKey)} for the default target " + |
| | 4 | 88 | | $"or add a named target with {nameof(RabbitMqAsyncResponseOptions.AddReplyTarget)}."); |
| | | 89 | | } |
| | | 90 | | } |