| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.Redis; |
| | | 4 | | |
| | 3 | 5 | | internal sealed class RedisReplyTargetProvider( |
| | 3 | 6 | | IOptions<RedisAsyncResponseTransportOptions> _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 | | RedisTransportOptionsValidator.ValidateCommon(options); |
| | 3 | 13 | | var schema = new RedisTransportKeySchema(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 responseStream = RedisTransportOptionsValidator.Required( |
| | 3 | 20 | | target.ResponseStream, |
| | 3 | 21 | | $"{nameof(RedisReplyTargetOptions)}.{nameof(RedisReplyTargetOptions.ResponseStream)}"); |
| | 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 | | ["stream"] = responseStream, |
| | 3 | 27 | | ["consumerGroup"] = consumerGroup, |
| | 3 | 28 | | ["payloadField"] = options.PayloadField, |
| | 3 | 29 | | ["correlationIdField"] = options.CorrelationIdField |
| | 3 | 30 | | }; |
| | | 31 | | |
| | 3 | 32 | | return new AsyncResponseReplyTarget |
| | 3 | 33 | | { |
| | 3 | 34 | | Name = targetName, |
| | 3 | 35 | | Transport = RedisAsyncResponseTransportOptions.TransportName, |
| | 3 | 36 | | Address = responseStream, |
| | 3 | 37 | | Properties = properties |
| | 3 | 38 | | }; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private static RedisReplyTargetOptions ResolveTarget( |
| | | 42 | | RedisAsyncResponseTransportOptions options, |
| | | 43 | | RedisTransportKeySchema schema, |
| | | 44 | | string targetName) |
| | | 45 | | { |
| | 3 | 46 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 3 | 47 | | return configured; |
| | | 48 | | |
| | 3 | 49 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | | 50 | | { |
| | 3 | 51 | | return new RedisReplyTargetOptions |
| | 3 | 52 | | { |
| | 3 | 53 | | ResponseStream = schema.ResponseStream.ToString(), |
| | 3 | 54 | | ConsumerGroup = options.ResponseConsumerGroup |
| | 3 | 55 | | }; |
| | | 56 | | } |
| | | 57 | | |
| | 2 | 58 | | throw new InvalidOperationException( |
| | 2 | 59 | | $"Redis async-response reply target '{targetName}' is not configured. " + |
| | 2 | 60 | | $"Configure {nameof(RedisAsyncResponseTransportOptions.ResponseStream)} for the default target " + |
| | 2 | 61 | | $"or add a named target with {nameof(RedisAsyncResponseTransportOptions.AddReplyTarget)}."); |
| | | 62 | | } |
| | | 63 | | } |