| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.Redis; |
| | | 4 | | |
| | 208 | 5 | | internal sealed class RedisReplyTargetProvider( |
| | 208 | 6 | | IOptions<RedisAsyncResponseTransportOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 7 | | { |
| | | 8 | | /// <summary>Gets the configured reply target.</summary> |
| | | 9 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 10 | | { |
| | 14 | 11 | | var options = _options.Value; |
| | 14 | 12 | | RedisTransportOptionsValidator.ValidateCommon(options); |
| | 14 | 13 | | var schema = new RedisTransportKeySchema(options); |
| | 14 | 14 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 14 | 15 | | ? options.DefaultReplyTargetName |
| | 14 | 16 | | : name; |
| | | 17 | | |
| | 14 | 18 | | var target = ResolveTarget(options, schema, targetName); |
| | 12 | 19 | | var responseStream = RedisTransportOptionsValidator.Required( |
| | 12 | 20 | | target.ResponseStream, |
| | 12 | 21 | | $"{nameof(RedisReplyTargetOptions)}.{nameof(RedisReplyTargetOptions.ResponseStream)}"); |
| | 12 | 22 | | var consumerGroup = target.ConsumerGroup ?? options.ResponseConsumerGroup; |
| | | 23 | | |
| | | 24 | | // ValidateCommon enforces distinctness for the transport-wide streams; a NAMED target |
| | | 25 | | // must honor the same rule (DB-transport parity) — aimed at the worker or dead-letter |
| | | 26 | | // stream, its responses are consumed as worker jobs (or mixed into dead letters) while |
| | | 27 | | // the waiter times out. |
| | 12 | 28 | | if (StringComparer.Ordinal.Equals(responseStream, schema.WorkerStream.ToString()) |
| | 12 | 29 | | || StringComparer.Ordinal.Equals(responseStream, schema.DeadLetterStream.ToString())) |
| | | 30 | | { |
| | 4 | 31 | | throw new InvalidOperationException( |
| | 4 | 32 | | $"Redis async-response reply target '{targetName}' uses stream '{responseStream}', which collides with " |
| | 4 | 33 | | $"{nameof(RedisAsyncResponseTransportOptions.WorkerStream)} or {nameof(RedisAsyncResponseTransportOption |
| | 4 | 34 | | "its responses would be consumed as worker jobs (or mixed into dead letters)."); |
| | | 35 | | } |
| | | 36 | | |
| | 8 | 37 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 8 | 38 | | { |
| | 8 | 39 | | ["stream"] = responseStream, |
| | 8 | 40 | | ["consumerGroup"] = consumerGroup, |
| | 8 | 41 | | ["payloadField"] = options.PayloadField, |
| | 8 | 42 | | ["correlationIdField"] = options.CorrelationIdField |
| | 8 | 43 | | }; |
| | | 44 | | |
| | 8 | 45 | | return new AsyncResponseReplyTarget |
| | 8 | 46 | | { |
| | 8 | 47 | | Name = targetName, |
| | 8 | 48 | | Transport = RedisAsyncResponseTransportOptions.TransportName, |
| | 8 | 49 | | Address = responseStream, |
| | 8 | 50 | | Properties = properties |
| | 8 | 51 | | }; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private static RedisReplyTargetOptions ResolveTarget( |
| | | 55 | | RedisAsyncResponseTransportOptions options, |
| | | 56 | | RedisTransportKeySchema schema, |
| | | 57 | | string targetName) |
| | | 58 | | { |
| | 14 | 59 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 8 | 60 | | return configured; |
| | | 61 | | |
| | 6 | 62 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | | 63 | | { |
| | 4 | 64 | | return new RedisReplyTargetOptions |
| | 4 | 65 | | { |
| | 4 | 66 | | ResponseStream = schema.ResponseStream.ToString(), |
| | 4 | 67 | | ConsumerGroup = options.ResponseConsumerGroup |
| | 4 | 68 | | }; |
| | | 69 | | } |
| | | 70 | | |
| | 2 | 71 | | throw new InvalidOperationException( |
| | 2 | 72 | | $"Redis async-response reply target '{targetName}' is not configured. " + |
| | 2 | 73 | | $"Configure {nameof(RedisAsyncResponseTransportOptions.ResponseStream)} for the default target " + |
| | 2 | 74 | | $"or add a named target with {nameof(RedisAsyncResponseTransportOptions.AddReplyTarget)}."); |
| | | 75 | | } |
| | | 76 | | } |