| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.Kafka; |
| | | 4 | | |
| | 3 | 5 | | internal sealed class KafkaReplyTargetProvider( |
| | 3 | 6 | | IOptions<KafkaAsyncResponseTransportOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 7 | | { |
| | | 8 | | /// <summary>Gets the configured reply target.</summary> |
| | | 9 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 10 | | { |
| | 3 | 11 | | var options = _options.Value; |
| | 3 | 12 | | KafkaTransportOptionsValidator.ValidateCommon(options); |
| | 3 | 13 | | var schema = new KafkaTransportTopicSchema(options); |
| | 3 | 14 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 3 | 15 | | ? options.DefaultReplyTargetName |
| | 3 | 16 | | : name; |
| | | 17 | | |
| | 3 | 18 | | var target = ResolveTarget(options, schema, targetName); |
| | 3 | 19 | | var responseTopic = KafkaTransportOptionsValidator.Required( |
| | 3 | 20 | | target.ResponseTopic, |
| | 3 | 21 | | $"{nameof(KafkaReplyTargetOptions)}.{nameof(KafkaReplyTargetOptions.ResponseTopic)}"); |
| | 3 | 22 | | var consumerGroup = target.ConsumerGroup ?? options.ResponseConsumerGroup; |
| | | 23 | | |
| | 3 | 24 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 3 | 25 | | { |
| | 3 | 26 | | ["topic"] = responseTopic, |
| | 3 | 27 | | ["consumerGroup"] = consumerGroup, |
| | 3 | 28 | | ["correlationIdHeader"] = options.CorrelationIdHeader |
| | 3 | 29 | | }; |
| | | 30 | | |
| | 3 | 31 | | if (!string.IsNullOrWhiteSpace(options.BootstrapServers)) |
| | 3 | 32 | | properties["bootstrapServers"] = options.BootstrapServers!; |
| | | 33 | | |
| | 3 | 34 | | return new AsyncResponseReplyTarget |
| | 3 | 35 | | { |
| | 3 | 36 | | Name = targetName, |
| | 3 | 37 | | Transport = KafkaAsyncResponseTransportOptions.TransportName, |
| | 3 | 38 | | Address = responseTopic, |
| | 3 | 39 | | Properties = properties |
| | 3 | 40 | | }; |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | private static KafkaReplyTargetOptions ResolveTarget( |
| | | 44 | | KafkaAsyncResponseTransportOptions options, |
| | | 45 | | KafkaTransportTopicSchema schema, |
| | | 46 | | string targetName) |
| | | 47 | | { |
| | 3 | 48 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 3 | 49 | | return configured; |
| | | 50 | | |
| | 3 | 51 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | | 52 | | { |
| | 3 | 53 | | return new KafkaReplyTargetOptions |
| | 3 | 54 | | { |
| | 3 | 55 | | ResponseTopic = schema.ResponseTopic, |
| | 3 | 56 | | ConsumerGroup = options.ResponseConsumerGroup |
| | 3 | 57 | | }; |
| | | 58 | | } |
| | | 59 | | |
| | 2 | 60 | | throw new InvalidOperationException( |
| | 2 | 61 | | $"Kafka async-response reply target '{targetName}' is not configured. " + |
| | 2 | 62 | | $"Configure {nameof(KafkaAsyncResponseTransportOptions.ResponseTopic)} for the default target " + |
| | 2 | 63 | | $"or add a named target with {nameof(KafkaAsyncResponseTransportOptions.AddReplyTarget)}."); |
| | | 64 | | } |
| | | 65 | | } |