| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.Kafka; |
| | | 4 | | |
| | 207 | 5 | | internal sealed class KafkaReplyTargetProvider( |
| | 207 | 6 | | IOptions<KafkaAsyncResponseTransportOptions> _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 | | KafkaTransportOptionsValidator.ValidateCommon(options); |
| | 12 | 13 | | var schema = new KafkaTransportTopicSchema(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 responseTopic = KafkaTransportOptionsValidator.Required( |
| | 10 | 20 | | target.ResponseTopic, |
| | 10 | 21 | | $"{nameof(KafkaReplyTargetOptions)}.{nameof(KafkaReplyTargetOptions.ResponseTopic)}"); |
| | 10 | 22 | | var consumerGroup = target.ConsumerGroup ?? options.ResponseConsumerGroup; |
| | | 23 | | |
| | | 24 | | // ValidateCommon enforces distinctness for the transport-wide topics; a NAMED target must |
| | | 25 | | // honor the same rule (DB-transport parity) — aimed at the worker topic its responses are |
| | | 26 | | // consumed as worker jobs, and aimed at a derived dead-letter topic they are mixed into |
| | | 27 | | // buried traffic, while the waiter times out. |
| | 10 | 28 | | if (StringComparer.Ordinal.Equals(responseTopic, schema.WorkerTopic) |
| | 10 | 29 | | || StringComparer.Ordinal.Equals(responseTopic, schema.DeadLetterTopicFor(schema.WorkerTopic)) |
| | 10 | 30 | | || StringComparer.Ordinal.Equals(responseTopic, schema.DeadLetterTopicFor(schema.ResponseTopic))) |
| | | 31 | | { |
| | 6 | 32 | | throw new InvalidOperationException( |
| | 6 | 33 | | $"Kafka async-response reply target '{targetName}' uses topic '{responseTopic}', which collides with " + |
| | 6 | 34 | | $"{nameof(KafkaAsyncResponseTransportOptions.WorkerTopic)} or a derived dead-letter topic; " + |
| | 6 | 35 | | "its responses would be consumed as worker jobs (or mixed into dead letters)."); |
| | | 36 | | } |
| | | 37 | | |
| | 4 | 38 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 4 | 39 | | { |
| | 4 | 40 | | ["topic"] = responseTopic, |
| | 4 | 41 | | ["consumerGroup"] = consumerGroup, |
| | 4 | 42 | | ["correlationIdHeader"] = options.CorrelationIdHeader |
| | 4 | 43 | | }; |
| | | 44 | | |
| | 4 | 45 | | if (!string.IsNullOrWhiteSpace(options.BootstrapServers)) |
| | 4 | 46 | | properties["bootstrapServers"] = options.BootstrapServers!; |
| | | 47 | | |
| | 4 | 48 | | return new AsyncResponseReplyTarget |
| | 4 | 49 | | { |
| | 4 | 50 | | Name = targetName, |
| | 4 | 51 | | Transport = KafkaAsyncResponseTransportOptions.TransportName, |
| | 4 | 52 | | Address = responseTopic, |
| | 4 | 53 | | Properties = properties |
| | 4 | 54 | | }; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | private static KafkaReplyTargetOptions ResolveTarget( |
| | | 58 | | KafkaAsyncResponseTransportOptions options, |
| | | 59 | | KafkaTransportTopicSchema schema, |
| | | 60 | | string targetName) |
| | | 61 | | { |
| | 12 | 62 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 8 | 63 | | return configured; |
| | | 64 | | |
| | 4 | 65 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | | 66 | | { |
| | 2 | 67 | | return new KafkaReplyTargetOptions |
| | 2 | 68 | | { |
| | 2 | 69 | | ResponseTopic = schema.ResponseTopic, |
| | 2 | 70 | | ConsumerGroup = options.ResponseConsumerGroup |
| | 2 | 71 | | }; |
| | | 72 | | } |
| | | 73 | | |
| | 2 | 74 | | throw new InvalidOperationException( |
| | 2 | 75 | | $"Kafka async-response reply target '{targetName}' is not configured. " + |
| | 2 | 76 | | $"Configure {nameof(KafkaAsyncResponseTransportOptions.ResponseTopic)} for the default target " + |
| | 2 | 77 | | $"or add a named target with {nameof(KafkaAsyncResponseTransportOptions.AddReplyTarget)}."); |
| | | 78 | | } |
| | | 79 | | } |