| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.MongoDB; |
| | | 4 | | |
| | 205 | 5 | | internal sealed class MongoDbReplyTargetProvider( |
| | 205 | 6 | | IOptions<MongoDbAsyncResponseTransportOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 7 | | { |
| | | 8 | | /// <inheritdoc /> |
| | | 9 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 10 | | { |
| | 10 | 11 | | var options = _options.Value; |
| | 10 | 12 | | MongoDbTransportOptionsValidator.ValidateCommon(options); |
| | 10 | 13 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 10 | 14 | | ? options.DefaultReplyTargetName |
| | 10 | 15 | | : name; |
| | | 16 | | |
| | 10 | 17 | | var target = ResolveTarget(options, targetName); |
| | 8 | 18 | | var responseQueue = MongoDbTransportOptionsValidator.Required( |
| | 8 | 19 | | target.ResponseQueue, |
| | 8 | 20 | | $"{nameof(MongoDbReplyTargetOptions)}.{nameof(MongoDbReplyTargetOptions.ResponseQueue)}"); |
| | | 21 | | |
| | | 22 | | // ValidateCommon enforces three-way distinctness for the transport-wide queues because all |
| | | 23 | | // logical queues share one collection; a NAMED target reaches that same collection by |
| | | 24 | | // another route and must honor the same rule — a target aimed at the worker (or |
| | | 25 | | // dead-letter) queue lands responses as documents the worker subscriber claims, NAKs to |
| | | 26 | | // the cap, and dead-letters, while the waiter times out. Matching the transport-wide |
| | | 27 | | // ResponseQueue is fine: that is literally the default target's destination. |
| | 8 | 28 | | if (StringComparer.Ordinal.Equals(responseQueue, options.WorkerQueue) |
| | 8 | 29 | | || StringComparer.Ordinal.Equals(responseQueue, options.DeadLetterQueue)) |
| | | 30 | | { |
| | 4 | 31 | | throw new InvalidOperationException( |
| | 4 | 32 | | $"MongoDB async-response reply target '{targetName}' uses queue '{responseQueue}', which collides with " |
| | 4 | 33 | | $"{nameof(MongoDbAsyncResponseTransportOptions.WorkerQueue)} or {nameof(MongoDbAsyncResponseTransportOpt |
| | 4 | 34 | | "all queues share one collection, so the target's responses would be consumed as worker jobs (or buried |
| | | 35 | | } |
| | | 36 | | |
| | 4 | 37 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 4 | 38 | | { |
| | 4 | 39 | | ["collection"] = options.MessageCollection, |
| | 4 | 40 | | ["queue"] = responseQueue, |
| | 4 | 41 | | ["correlationIdHeader"] = options.CorrelationIdHeader |
| | 4 | 42 | | }; |
| | 4 | 43 | | if (!string.IsNullOrWhiteSpace(options.DatabaseName)) |
| | 2 | 44 | | properties["database"] = options.DatabaseName!; |
| | | 45 | | |
| | 4 | 46 | | return new AsyncResponseReplyTarget |
| | 4 | 47 | | { |
| | 4 | 48 | | Name = targetName, |
| | 4 | 49 | | Transport = MongoDbAsyncResponseTransportOptions.TransportName, |
| | 4 | 50 | | Address = responseQueue, |
| | 4 | 51 | | Properties = properties |
| | 4 | 52 | | }; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | private static MongoDbReplyTargetOptions ResolveTarget( |
| | | 56 | | MongoDbAsyncResponseTransportOptions options, |
| | | 57 | | string targetName) |
| | | 58 | | { |
| | 10 | 59 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 6 | 60 | | return configured; |
| | | 61 | | |
| | 4 | 62 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | 2 | 63 | | return new MongoDbReplyTargetOptions { ResponseQueue = options.ResponseQueue }; |
| | | 64 | | |
| | 2 | 65 | | throw new InvalidOperationException( |
| | 2 | 66 | | $"MongoDB async-response reply target '{targetName}' is not configured. " + |
| | 2 | 67 | | $"Configure {nameof(MongoDbAsyncResponseTransportOptions.ResponseQueue)} for the default target " + |
| | 2 | 68 | | $"or add a named target with {nameof(MongoDbAsyncResponseTransportOptions.AddReplyTarget)}."); |
| | | 69 | | } |
| | | 70 | | } |