| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.NATS; |
| | | 4 | | |
| | 207 | 5 | | internal sealed class NatsReplyTargetProvider( |
| | 207 | 6 | | IOptions<NatsAsyncResponseTransportOptions> _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 | | NatsTransportOptionsValidator.ValidateCommon(options); |
| | 12 | 13 | | var schema = new NatsTransportSubjectSchema(options); |
| | 12 | 14 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 12 | 15 | | ? options.DefaultReplyTargetName |
| | 12 | 16 | | : name; |
| | | 17 | | |
| | 12 | 18 | | var target = ResolveTarget(options, schema, targetName); |
| | 10 | 19 | | var responseSubject = NatsTransportOptionsValidator.Required( |
| | 10 | 20 | | target.ResponseSubject, |
| | 10 | 21 | | $"{nameof(NatsReplyTargetOptions)}.{nameof(NatsReplyTargetOptions.ResponseSubject)}"); |
| | 10 | 22 | | var consumer = target.Consumer ?? options.ResponseConsumer; |
| | | 23 | | |
| | | 24 | | // ValidateCommon enforces distinctness for the transport-wide subjects; a NAMED target |
| | | 25 | | // must honor the same rule (DB-transport parity) — aimed at the worker or dead-letter |
| | | 26 | | // subject, its responses are consumed as worker jobs (or buried as dead letters) while |
| | | 27 | | // the waiter times out. |
| | 10 | 28 | | if (StringComparer.Ordinal.Equals(responseSubject, schema.WorkerSubject) |
| | 10 | 29 | | || StringComparer.Ordinal.Equals(responseSubject, schema.DeadLetterSubject)) |
| | | 30 | | { |
| | 4 | 31 | | throw new InvalidOperationException( |
| | 4 | 32 | | $"NATS async-response reply target '{targetName}' uses subject '{responseSubject}', which collides with |
| | 4 | 33 | | $"{nameof(NatsAsyncResponseTransportOptions.WorkerSubject)} or {nameof(NatsAsyncResponseTransportOptions |
| | 4 | 34 | | "its responses would be consumed as worker jobs (or buried as dead letters)."); |
| | | 35 | | } |
| | | 36 | | |
| | 6 | 37 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 6 | 38 | | { |
| | 6 | 39 | | ["subject"] = responseSubject, |
| | 6 | 40 | | ["consumer"] = consumer, |
| | 6 | 41 | | ["correlationIdHeader"] = options.CorrelationIdHeader |
| | 6 | 42 | | }; |
| | | 43 | | |
| | 6 | 44 | | return new AsyncResponseReplyTarget |
| | 6 | 45 | | { |
| | 6 | 46 | | Name = targetName, |
| | 6 | 47 | | Transport = NatsAsyncResponseTransportOptions.TransportName, |
| | 6 | 48 | | Address = responseSubject, |
| | 6 | 49 | | Properties = properties |
| | 6 | 50 | | }; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | private static NatsReplyTargetOptions ResolveTarget( |
| | | 54 | | NatsAsyncResponseTransportOptions options, |
| | | 55 | | NatsTransportSubjectSchema schema, |
| | | 56 | | string targetName) |
| | | 57 | | { |
| | 12 | 58 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 8 | 59 | | return configured; |
| | | 60 | | |
| | 4 | 61 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | | 62 | | { |
| | 2 | 63 | | return new NatsReplyTargetOptions |
| | 2 | 64 | | { |
| | 2 | 65 | | ResponseSubject = schema.ResponseSubject, |
| | 2 | 66 | | Consumer = options.ResponseConsumer |
| | 2 | 67 | | }; |
| | | 68 | | } |
| | | 69 | | |
| | 2 | 70 | | throw new InvalidOperationException( |
| | 2 | 71 | | $"NATS async-response reply target '{targetName}' is not configured. " + |
| | 2 | 72 | | $"Configure {nameof(NatsAsyncResponseTransportOptions.ResponseSubject)} for the default target " + |
| | 2 | 73 | | $"or add a named target with {nameof(NatsAsyncResponseTransportOptions.AddReplyTarget)}."); |
| | | 74 | | } |
| | | 75 | | } |